Description

  1. Drazil created a following problem about putting  ×  tiles into an n × m grid:
  2.  
  3. "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."
  4.  
  5. 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' ".
  6.  
  7. Drazil found that the constraints for this task may be much larger than for the original task!
  8.  
  9. Can you solve this new problem?
  10.  
  11. 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

  1. The first line contains two integers n and m ( ≤ n, m ≤ ).
  2.  
  3. The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.

Output

  1. If there is no solution or the solution is not unique, you should print the string "Not unique".
  2.  
  3. 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
  1. ...
  2. .*.
  3. ...
Output
  1. Not unique
Input
  1. ..**
  2. *...
  3. *.**
  4. ....
Output
  1. <>**
  2. *^<>
  3. *v**
  4. <><>
Input
  1. *..*
  2. ....
Output
  1. *<>*
  2. <><>
Input
  1. .
Output
  1. Not unique
Input
  1. *
Output
  1. *

Hint

  1. In the first case, there are indeed two solutions:
  2.  
  3. <>^
  4. ^*v
  5. v<>
  6. and
  7.  
  8. ^<>
  9. v*^
  10. <>v
  11. so the answer is "Not unique".

Source

 
用贪心法解决:首先应该着力填充周围只有一个空格的点,随后以它的空格为中心,再不断向外拓展,直到整个界面被填充。
注意在judge函数判断边界和‘.’时应该这样写:return (i>=0 && i<n && j>=0 && j<m && mp[i][j]=='.');  分开判断一直错。
  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. int dirx[]={,,-,};
  17. int diry[]={-,,,};
  18. #define PI acos(-1.0)
  19. #define max(a,b) (a) > (b) ? (a) : (b)
  20. #define min(a,b) (a) < (b) ? (a) : (b)
  21. #define ll long long
  22. #define eps 1e-10
  23. #define MOD 1000000007
  24. #define N 2006
  25. #define inf 1e12
  26. int n,m;
  27. char mp[N][N];
  28. struct Node{
  29. int x,y;
  30. };
  31. char change[]="><<>v^^v";
  32. bool judge(int i,int j){
  33. return (i>= && i<n && j>= && j<m && mp[i][j]=='.');
  34. }
  35.  
  36. int nearPoint_num(int x,int y){
  37. int ans=;//ans表示周围的空点
  38. for(int i=;i<;i++){
  39. int tx=x+dirx[i];
  40. int ty=y+diry[i];
  41. if(judge(tx,ty)){
  42. ans++;
  43. }
  44. }
  45. return ans;
  46. }
  47.  
  48. void bfs(){
  49. queue<Node>q;
  50. Node tmp;
  51. Node t1,t2,t3;
  52. for(int i=;i<n;i++){
  53. for(int j=;j<m;j++){
  54. if(judge(i,j) && nearPoint_num(i,j)==){
  55. tmp.x=i;
  56. tmp.y=j;
  57. //printf("***%d %d\n",tmp.x,tmp.y);
  58. q.push(tmp);
  59. }
  60. }
  61. }
  62. while(!q.empty()){
  63. t1=q.front();
  64. q.pop();
  65. for(int i=;i<;i++){
  66. t2.x=t1.x+dirx[i];
  67. t2.y=t1.y+diry[i];
  68. if(judge(t2.x,t2.y)){
  69. mp[t1.x][t1.y]=change[i*];
  70. mp[t2.x][t2.y]=change[i*+];
  71. //printf("%d %d %c\n",t1.x,t1.y,mp[t1.x][t1.y]);
  72. //printf("%d %d %c\n",t2.x,t2.y,mp[t2.x][t2.y]);
  73.  
  74. for(int j=;j<;j++){
  75. t3.x=t2.x+dirx[j];
  76. t3.y=t2.y+diry[j];
  77. if(judge(t3.x,t3.y) && nearPoint_num(t3.x,t3.y)==){
  78. q.push(t3);
  79. }
  80. }
  81.  
  82. }
  83.  
  84. }
  85. }
  86.  
  87. int flag=;
  88. for(int i=;i<n;i++){
  89. for(int j=;j<m;j++){
  90. if(mp[i][j]=='.'){
  91. flag=;
  92. break;
  93. }
  94. }
  95. if(flag==){
  96. break;
  97. }
  98. }
  99. if(flag==){
  100. printf("Not unique\n");
  101. }
  102. else{
  103. for(int i=;i<n;i++){
  104. for(int j=;j<m;j++){
  105. printf("%c",mp[i][j]);
  106. }
  107. printf("\n");
  108. }
  109. }
  110.  
  111. }
  112. int main()
  113. {
  114. while(scanf("%d%d",&n,&m)==){
  115. for(int i=;i<n;i++){
  116. scanf("%s",mp[i]);
  117. }
  118.  
  119. bfs();
  120.  
  121. }
  122. return ;
  123. }

#292 (div.2) D.Drazil and Tiles (贪心+bfs)的更多相关文章

  1. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  2. Codeforces Round #292 (Div. 1) - B. Drazil and Tiles

    B. Drazil and Tiles   Drazil created a following problem about putting 1 × 2 tiles into an n × m gri ...

  3. Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

    传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...

  4. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

    题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...

  5. CodeForces - 516B Drazil and Tiles(bfs)

    https://vjudge.net/problem/CodeForces-516B 题意 在一个n*m图中放1*2或者2*1的长方形,问是否存在唯一的方法填满图中的‘.’ 分析 如果要有唯一的方案, ...

  6. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  7. Codeforces Round #292 (Div. 1) C - Drazil and Park

    C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...

  8. Codeforces Round #292 (Div. 2) C. Drazil and Factorial 515C

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. Codeforces 631 (Div. 2) E. Drazil Likes Heap 贪心

    https://codeforces.com/contest/1330/problem/E 有一个高度为h的大顶堆:有2h -1个不同的正整数,下标从1到2h−1,1<i<2h, a[i] ...

随机推荐

  1. vi命令笔记

    vim编辑器 文本编辑器,字处理器ASCII nano, sed vi: Visual Interfacevim: VI iMproved 全屏编辑器,模式化编辑器 vim模式:编辑模式(命令模式)输 ...

  2. C#Http编程

    c# 模拟 网页实现12306登陆.自动刷票.自动抢票完全篇(转) 这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童 ...

  3. Bin & Jing in wonderland(概率,组合数学)

    Problem 2103 Bin & Jing in wonderland Accept: 201    Submit: 1048 Time Limit: 1000 mSec    Memor ...

  4. UIImage与UIColor互转

    Objective-C UIColor -> UIImage ? 1 2 3 4 5 6 7 8 9 10 11 - (UIImage*) createImageWithColor: (UICo ...

  5. chart.js制作折线图

    <!DOCTYPE html> <html> <head> <title></title> </head> <script ...

  6. EffectiveC#16--垃圾最小化

    1.申请和释放一个基于堆内存的对象要花上更多的处理器时间. 所以当一个引用类型的局部变量在常规的函数调用中使用的非常频繁时应该把它提升为对象的成员(方法一) 2.当你把一个实现了IDisposable ...

  7. cookie丢失、登陆自动退出问题解决

    cookie保存在客户端或者内存中,不易丢失.但是在某些情况下会被忽略.在项目过程中遇到过跨域丢失的情况.在VS里面运行的程序,产生的cookie默认是没有domain值的,但是给它设定domain值 ...

  8. SqlServer存储过程传入Table参数

    今天是周日,刚好有空闲时间整理一下这些天工作业务中遇到的问题. 有时候我们有这样一个需求,就是在后台中传过来一个IList<类>的泛型集合数据,该集合是某个类的实例集合体,然后将该集合中的 ...

  9. C#winform修改IP,dns

     /// 将IP,DNS设置为自动获取        ///        private void setDHCP()        {            string  _doscmd = & ...

  10. js静态方法

    1.ajax() 方法是属于“函数”本身的,和返回的对象没有关系 2.bark药调用,必须药new Hashiqi()得到对象,且由返回对象才能调用 3.ajax()方法药调用,不需要new对象,直接 ...