给一幅
N×M 的平面图G,其中 # 表示不可填方格; . 表示可填方格。现将1×2 或2×1 的小方格填充图G,要求图G可填方格全被填满,且无两小方格相互重叠。如果无法达到要求或有多种解法,输出 “Not unique”,否则输出填好后的图G。
TimeLimit(ms):2000
MemoryLimit(MB):256
N,M∈[1,2000]
二维拓扑排序。
1. 将所有度数为1的点加入队列;
2. 把队列里的一个点
v1 和 与v1 相邻的一个点v2 用1×2 或2×1 方格覆盖,再用v2 更新周围点的度数,若更新后点度数为1,则加入队列;3. 若2中存在一个
v1 无法与一个v2 匹配,或者2结束后,存在一个点没有访问过,则无解或有多解;否则输出解即可。还有,%s比%c快多了
…
TimeComplexity:O(N×M)
MemoryComplexity:O(N×M)
//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uin;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-9
#define MOD 1000000007
#define MAXN 16000010
#define N 2010
#define M
void Notunique(){
printf("Not unique\n");
exit(0);
}
int n,m;
char grid[N][N];
int num_edge,outdegree[N][N];
bool can_move(int x,int y){
return x>=1 && x<=n && y>=1 && y<=m && grid[x][y]==‘.‘;
}
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
bool vis[N][N];
queue<pair<int,int> >q;
struct Ans{
int x1,y1;
int x2,y2;
}ansfri[N*N];
int num_ansfri;
int main(){
scanf("%d %d",&n,&m);
repin(i,1,n){
scanf("%s",grid[i]+1);
}
num_edge=0;
repin(i,1,n){
repin(j,1,m){
if(grid[i][j]!=‘.‘) continue;
int fromx=i,fromy=j,tox,toy;
rep(k,0,4){
tox=fromx+dx[k];
toy=fromy+dy[k];
if(!can_move(tox,toy)) continue;
outdegree[fromx][fromy]+=1;
}
}
}
//二维拓扑排序
repin(i,1,n){
repin(j,1,m){
if(grid[i][j]==‘.‘ && outdegree[i][j]==1){
pair<int,int>p=make_pair(i,j);
q.push(p);
}
}
}
num_ansfri=0;
while(!q.empty()){
pair<int,int>now=q.front();
q.pop();
if(vis[now.first][now.second]) continue;
vis[now.first][now.second]=true;
bool find=false;
rep(i,0,4){
int tox=now.first+dx[i];
int toy=now.second+dy[i];
if(!can_move(tox,toy)) continue;
if(vis[tox][toy]) continue;
vis[tox][toy]=true;
int t=++num_ansfri;
ansfri[t].x1=now.first,ansfri[t].y1=now.second;
ansfri[t].x2=tox,ansfri[t].y2=toy;
rep(j,0,4){
int x1=tox+dx[j];
int y1=toy+dy[j];
outdegree[x1][y1]-=1;
if(outdegree[x1][y1]==1){
pair<int,int>newp=make_pair(x1,y1);
q.push(newp);
}
}
find=true;
break;
}
if(!find) Notunique();
}
repin(i,1,n){
repin(j,1,m){
if(grid[i][j]==‘.‘ && !vis[i][j]){
Notunique();
}
}
}
repin(i,1,num_ansfri){
int x1=ansfri[i].x1,y1=ansfri[i].y1;
int x2=ansfri[i].x2,y2=ansfri[i].y2;
if(x1==x2){
if(y1<y2) grid[x1][y1]=‘<‘,grid[x2][y2]=‘>‘;
else grid[x2][y2]=‘<‘,grid[x1][y1]=‘>‘;
}
else{//y1==y2
if(x1<x2) grid[x1][y1]=‘^‘,grid[x2][y2]=‘v‘;
else grid[x2][y2]=‘^‘,grid[x1][y1]=‘v‘;
}
}
repin(i,1,n){
printf("%s\n",grid[i]+1);
}
}
原文地址:http://blog.csdn.net/uestc_peterpan/article/details/43875195