D. Drazil and Tiles (CF 515D bfs搜寻)
D. Drazil and Tiles (CF 515D bfs搜索)
题意:n*m的图,‘.’表示空格,现在要用1*2的砖去把它填满,可以横向(‘<’,'>')填和竖向('^','v')填。找出基本元素块,(i,j)和它相邻的四个点看成一个基本元素块,如果(i,j)周围的‘.’只有一个那么这个(i,j)处的填法就是固定的,填完(i,j)后看它周围是否有其他点因为填完(i,j)后填法变的唯一,有就入队, 就这样一步一步找到固定填法的(i,j),更新周围的点。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 2005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; typedef pair<int,int>pa; int a[maxn][maxn]; //1--'.' ; 2--'^' ; 3--'>' ; 4--'v' ; 5--'<' char mp[maxn][maxn]; int n,m; bool Isok(int x,int y) { if (x>=1&&x<=n&&y>=1&&y<=m) return true; return false; } int degree(int x,int y,int &dir,int &xx,int &yy)//(xx,yy)记录下和(x,y)配对的点的位置 { int s=0; if (a[x-1][y]==1) {s++;dir=4;xx=x-1;yy=y;} //上 if (a[x][y-1]==1) {s++;dir=3;xx=x;yy=y-1;} //左 if (a[x+1][y]==1) {s++;dir=2;xx=x+1,yy=y;} //下 if (a[x][y+1]==1) {s++;dir=5;xx=x;yy=y+1;} //右 return s; } void bfs() { int i,j; queue<pa>Q; while (!Q.empty()) Q.pop(); pa st; FRE(i,1,n) { FRE(j,1,m) { int dir,xx,yy; if (a[i][j]==1&°ree(i,j,dir,xx,yy)==1) { a[i][j]=dir; a[xx][yy]=dir%4+2; Q.push(make_pair(xx,yy)); } } } while (!Q.empty()) { st=Q.front(); Q.pop(); FRE(i,st.first-1,st.first+1) { FRE(j,st.second-1,st.second+1) { int dir,xx,yy; if (Isok(i,j)&&a[i][j]==1&°ree(i,j,dir,xx,yy)==1) { a[i][j]=dir; a[xx][yy]=dir%4+2; Q.push(make_pair(xx,yy)); } } } } } int main() { int i,j; while (~sff(n,m)) { mem(a,0); FRE(i,1,n) { scanf("%s",mp[i]+1); FRE(j,1,m) { if (mp[i][j]=='.') a[i][j]=1; else a[i][j]=0; } } bfs(); bool Unique=true; FRE(i,1,n) //最后如果还有'.'那就要输出'Not unique' { FRE(j,1,m) { if (a[i][j]==1) { Unique=false; break; } } if (!Unique) break; } if (!Unique) { pf("Not unique\n"); continue; } FRE(i,1,n) { FRE(j,1,m) { if (a[i][j]==0) pf("*"); else if (a[i][j]==2) pf("^"); else if (a[i][j]==3) pf(">"); else if (a[i][j]==4) pf("v"); else if (a[i][j]==5) pf("<"); } pf("\n"); } } return 0; } /* 3 3 ... .*. ... 4 4 ..** *... *.** .... 2 4 *..* .... 1 1 . 1 1 * 10 10 *..**....* ..**.*.... **...***** ..**..*... **...*...* .*.*.*.*.. .***.**.*. ****.**.*. ...*....*. **.**.**.. */