您的位置:首页 > 其它

CF_292_D_ Drazil and Tiles_贪心、dfs

2015-02-18 12:45 369 查看
一步两步,一步两步,一步一步似爪牙,是魔鬼的步伐。

题意:

隔壁老王又想了个题目,在一个n*m的方阵里,*表示有障碍物,.表示空白,要求用1*2的方块填满方阵,且没有方块重叠。如果无法填满或者有多重方法,输出Not unique,否则用<>、^v方式输出填满的方式。

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2000).

The following n lines describe the grid rows. Character '.'
denotes an empty cell, and the character '*' denotes a cell that is occupied.

Output

If there is no solution or the solution is not unique, you should print the string "Not unique".

Otherwise you should print how to cover all empty cells with 1 × 2 tiles. Use characters "<>"
to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

贪心就好,从只有一种方法的点开始,用dfs遍历,一开始用粗犷的遍历法,结果超时,用dfs的话就有顺序很多。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 2010
int n,m;
char a[mxn][mxn];
int mx[]={0,1,0,-1},my[]={1,0,-1,0};
bool okk(int x,int y){
if(x<0||y<0||x>=n||y>=m)	return false;
return a[x][y]=='.';
}
void dfs(int x,int y){
if(!okk(x,y))	return;
int tem=0,rec;
for(int i=0;i<4;++i)	if(okk(x+mx[i],y+my[i])){
++tem;
rec=i;
}
if(!tem)	return;
if(tem==1){
if(rec==0){
a[x][y]='<';
a[x][y+1]='>';
}
else if(rec==1){
a[x][y]='^';
a[x+1][y]='v';
}
else if(rec==2){
a[x][y]='>';
a[x][y-1]='<';
}
else{
a[x][y]='v';
a[x-1][y]='^';
}
for(int i=0;i<4;++i)
dfs(x+mx[rec]+mx[i],y+my[rec]+my[i]);
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;++i)	scanf("%s",a[i]);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
dfs(i,j);
bool ok=true;
for(int i=0;i<n;++i){
for(int j=0;j<m;++j)	if(a[i][j]=='.'){
ok=false;
break;
}
if(!ok)	break;
}
if(!ok)	puts("Not unique");
else for(int i=0;i<n;++i)
printf("%s\n",a[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: