Drazil and Tiles(CodeForces 515D)
2015-08-08 15:48
351 查看
Drazil created a following problem about putting 1 × 2 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.
The following n lines describe the grid rows. Character ‘.’ denotes an empty cell, and the character ‘*’ denotes a cell that is occupied.
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.
…
.*.
…
..**
*…
*.**
….
*^<>
*v**
<><>
*..*
….
<><>
.
*
<>^
^*v
v<>
and
^<>
v*^
<>v
so the answer is “Not unique”.
这道题竟然能用拓扑排序来写,真是没想到,之前一味的在搜索(到现在搜索也没搞出来
“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 (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.
Sample test(s)
Input
3 3…
.*.
…
Output
Not uniqueInput
4 4..**
*…
*.**
….
Output
<>***^<>
*v**
<><>
Input
2 4*..*
….
Output
*<>*<><>
Input
1 1.
Output
Not uniqueInput
1 1*
Output
*Note
In the first case, there are indeed two solutions:<>^
^*v
v<>
and
^<>
v*^
<>v
so the answer is “Not unique”.
这道题竟然能用拓扑排序来写,真是没想到,之前一味的在搜索(到现在搜索也没搞出来
代码
#include <iostream> #include <queue> using namespace std; #define X first #define Y second typedef pair<int, int> pii; char map[2010][2010]; int degree[2010][2010]; int n,m; int d[4][2]={-1,0,0,-1,0,1,1,0}; queue<pii> q; void adjust(int x, int y) { for(int i = 0; i < 4; i++) { int xx = x + d[i][0], yy = y + d[i][1]; degree[xx][yy]--; if(degree[xx][yy] == 1) q.push({xx, yy}); } } void adegree() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(map[i][j] != '.') continue; if(j > 1 && map[i][j-1] == '.') degree[i][j]++; if(j < m && map[i][j+1] == '.') degree[i][j]++; if(i > 1 && map[i-1][j] == '.') degree[i][j]++; if(i < n && map[i+1][j] == '.') degree[i][j]++; if(degree[i][j] == 1) q.push({i, j}); } } } void topo() { while(q.size()) { pii qq = q.front(); q.pop(); int x = qq.X, y = qq.Y; if(map[x-1][y] == '.') { map[x][y] = 'v'; map[x-1][y] = '^'; adjust(x-1, y); } else if(map[x+1][y] == '.') { map[x][y] = '^'; map[x+1][y] = 'v'; adjust(x+1, y); } else if(map[x][y-1] == '.') { map[x][y] = '>'; map[x][y-1] = '<'; adjust(x, y-1); } else if(map[x][y+1] == '.') { map[x][y] = '<'; map[x][y+1] = '>'; adjust(x, y+1); } else { if(map[x][y] == '.') { cout<<"Not unique"<<endl; return ; } } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(map[i][j] == '.') { cout<<"Not unique"<<endl; return ; } } } for(int i = 1; i <= n; i++) { cout<<map[i]+1<<endl; } } int main() { cin>>n>>m; for(int i = 1; i <= n; i++) cin>>map[i]+1; adegree(); topo(); return 0; }
相关文章推荐
- Hive Managed Table与 External Table区别
- 通用函数可变参数模板
- Android学习笔记之SlidingMenu
- 周赛-DZY Loves Chessboard
- 手机开发之web方式----启动程序检查更新版本
- Ubuntu录制gif动态图
- Linux C/C++计划Shell命令大杂烩(1)
- 应用spss可靠性分析软件
- Xcode使用git管理版本
- 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
- android之类似卫星菜单,来自定义ViewGroup。。。。。
- win32使用CInternetSession访问url,并且解析jason
- 神经网络入门 第二部分
- IOS删除缓存, caches
- 励精图治---Concurrency---组合对象
- jquery ajax && php foreach array
- Task schedule
- 心得分享:关于对Android的理解,知识点梳理
- Java网络编程概览
- UISegmentedControl方法与属性的总结