#292 (div.2) D.Drazil and Tiles (贪心+bfs)
2024-10-11 01:51:56
Description
Drazil created a following problem about putting × tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it." But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ". Drazil found that the constraints for this task may be much larger than for the original task! Can you solve this new problem? Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.
Input
The first line contains two integers n and m ( ≤ n, m ≤ ). 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 × tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.
Sample Input
Input
...
.*.
...
Output
Not unique
Input
..**
*...
*.**
....
Output
<>**
*^<>
*v**
<><>
Input
*..*
....
Output
*<>*
<><>
Input
.
Output
Not unique
Input
*
Output
*
Hint
In the first case, there are indeed two solutions: <>^
^*v
v<>
and ^<>
v*^
<>v
so the answer is "Not unique".
Source
用贪心法解决:首先应该着力填充周围只有一个空格的点,随后以它的空格为中心,再不断向外拓展,直到整个界面被填充。
注意在judge函数判断边界和‘.’时应该这样写:return (i>=0 && i<n && j>=0 && j<m && mp[i][j]=='.'); 分开判断一直错。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 2006
#define inf 1e12
int n,m;
char mp[N][N];
struct Node{
int x,y;
};
char change[]="><<>v^^v";
bool judge(int i,int j){
return (i>= && i<n && j>= && j<m && mp[i][j]=='.');
} int nearPoint_num(int x,int y){
int ans=;//ans表示周围的空点
for(int i=;i<;i++){
int tx=x+dirx[i];
int ty=y+diry[i];
if(judge(tx,ty)){
ans++;
}
}
return ans;
} void bfs(){
queue<Node>q;
Node tmp;
Node t1,t2,t3;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(judge(i,j) && nearPoint_num(i,j)==){
tmp.x=i;
tmp.y=j;
//printf("***%d %d\n",tmp.x,tmp.y);
q.push(tmp);
}
}
}
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(judge(t2.x,t2.y)){
mp[t1.x][t1.y]=change[i*];
mp[t2.x][t2.y]=change[i*+];
//printf("%d %d %c\n",t1.x,t1.y,mp[t1.x][t1.y]);
//printf("%d %d %c\n",t2.x,t2.y,mp[t2.x][t2.y]); for(int j=;j<;j++){
t3.x=t2.x+dirx[j];
t3.y=t2.y+diry[j];
if(judge(t3.x,t3.y) && nearPoint_num(t3.x,t3.y)==){
q.push(t3);
}
} } }
} int flag=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]=='.'){
flag=;
break;
}
}
if(flag==){
break;
}
}
if(flag==){
printf("Not unique\n");
}
else{
for(int i=;i<n;i++){
for(int j=;j<m;j++){
printf("%c",mp[i][j]);
}
printf("\n");
}
} }
int main()
{
while(scanf("%d%d",&n,&m)==){
for(int i=;i<n;i++){
scanf("%s",mp[i]);
} bfs(); }
return ;
}
最新文章
- java深入技术九 (注解)
- 跟我一起学JQuery插件开发
- Java比C++好在哪儿?
- SQL语句在数据库中是如何执行的
- hdu 4778
- seajs 学习笔记
- Introduction to REST #Reprinted#
- 51nod 1020 逆序排列 DP
- Dynamics CRM Form表单中通过javascript抓取触发change事件字段的属性名
- radio日志sim卡信号状态分析
- vue_v-for_遍历数组_遍历对象
- Linux交换Esc和Caps
- ELK基础原理
- Centos下堡垒机Jumpserver V3.0环境部署完整记录(1)-安装篇
- Single VIP LLB and SLB config
- 邮件相关协议及JavaMail 包简介
- delphi存取图片
- luogu P3369 【模板】普通平衡树(splay)
- 【JavaScript】2013年人气最高的JavaScript框架排名
- iOS开发中UILocalNotification本地通知实现简单的提醒功能