传送门

D. Drazil and Tiles
time limit per test 2 seconds
memory limit per test 256 megabytes

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 use1 × 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 solutionwhen 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 with1 × 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 unique
Input
4 4
..**
*...
*.**
....
Output
<>**
*^<>
*v**
<><>
Input
2 4
*..*
....
Output
*<>*
<><>
Input
1 1
.
Output
Not unique
Input
1 1
*
Output
*
Note

In the first case, there are indeed two solutions:

<>^
^*v
v<>

and

^<>
v*^
<>v

so the answer is "Not unique".

题意及题解转自田神:http://blog.csdn.net/tc_to_top/article/details/43876015

题目大意:n*m的矩阵,' . '表示位置空,' * '表示障碍物,问能不能用尖括号填满空的点,使矩阵中所有的尖括号都两两配对,水平配对:<> 竖直配对:^v,若不存在或答案不唯一输出Not unique

题目分析:有趣的题,DFS搜索,策略:先把*的点的数量记下来,每次向四周扩展时先找只有一个方向可扩展的点扩展,因为它的灵活度最小,也就是说在它这的策略是唯一的,每个点都搜一次,最后如果n * m = cnt表示每个点都填满了(包括障碍物)则说明有且只有一解

这题还可以用拓扑排序,而不是dfs,第一份代码是我的拓扑排序,第二份是田神的dfs,结果dfs还要快,晕= =

9954592 2015-02-22 04:53:19 njczy2010 D - Drazil and Tiles GNU C++ Accepted 233 ms 26400 KB 
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 2010
#define M 10005
//#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define ull unsigned long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int m;
char s[N][N];
int r[N][N];
int dirx[]={,,-,}; //d,r,u,l
int diry[]={,,,-};
int tot; typedef struct
{
int x;
int y;
}PP; int calr(int x,int y)
{
r[x][y]=;
int dir;
if(s[x][y]!='.') return -;
int i;
int nx,ny;
for(i=;i<;i++){
nx=x+dirx[i];
ny=y+diry[i];
if(s[nx][ny]=='.'){
r[x][y]++;
dir=i;
}
}
return dir;
} void ini()
{
int i,j;
memset(r,,sizeof(r));
for(i=;i<=n+;i++){
for(j=;j<=m+;j++){
s[i][j]=;
}
}
for(i=;i<=n;i++){
scanf("%s",s[i]+);
}
tot=;
} void solve()
{
queue<PP>q;
int i,j;
PP te,nt,ntt;
int dir;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
if(s[i][j]=='*'){
tot++;continue;
}
calr(i,j);
te.x=i;te.y=j;
if(r[i][j]==){
q.push(te);
}
}
}
while(q.size()>=)
{
te=q.front();
q.pop();
dir=calr(te.x,te.y);
if(r[te.x][te.y]!=) continue;
nt.x=te.x+dirx[ dir ];
nt.y=te.y+diry[ dir ];
tot+=;
if(dir==){
s[te.x][te.y]='^';s[nt.x][nt.y]='v';
}
else if(dir==){
s[te.x][te.y]='v';s[nt.x][nt.y]='^';
}
else if(dir==){
s[te.x][te.y]='<';s[nt.x][nt.y]='>';
}
else if(dir==){
s[te.x][te.y]='>';s[nt.x][nt.y]='<';
}
for(j=;j<;j++){
ntt.x=nt.x+dirx[j];
ntt.y=nt.y+diry[j];
calr(ntt.x,ntt.y);
if(r[ntt.x][ntt.y]==){
q.push(ntt);
}
}
}
} void out()
{
if(tot!=n*m){
printf("Not unique\n");
}
else{
int i;
for(i=;i<=n;i++){
printf("%s\n",s[i]+);
}
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
return ;
}

下面转一下田神的dfs:

9954596 2015-02-22 04:54:37 njczy2010 D - Drazil and Tiles GNU C++ Accepted 155 ms 4056 KB
 #include <cstdio>
#include <cstring>
int const MAX = ;
char s[MAX][MAX];
int n, m, cnt;
int dx[] = {, , , -};
int dy[] = {, -, , }; void dfs(int x, int y)
{
if(x < || x > n || y < || y > m || s[x][y] != '.')
return;
int dir = -, sum = ;
for(int i = ; i < ; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(s[xx][yy] == '.')
{
sum ++;
dir = i;
}
}
if(sum == ) //保证解的唯一性
{
if(dir == )
{
s[x][y] = '<';
s[x][y + ] = '>';
}
else if(dir == )
{
s[x][y] = '>';
s[x][y - ] = '<';
}
else if(dir == )
{
s[x][y] = '^';
s[x + ][y] = 'v';
}
else if(dir == )
{
s[x][y] = 'v';
s[x - ][y] = '^';
}
cnt += ;
for(int i = ; i < ; i++)
dfs(x + dx[dir] + dx[i], y + dy[dir] + dy[i]);
}
} int main()
{
cnt = ;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%s", s[i] + );
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
if(s[i][j] == '*')
cnt ++;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
dfs(i, j);
if(cnt == n * m)
for(int i = ; i <= n; i++)
printf("%s\n", s[i] + );
else
printf("Not unique\n");
}

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]的更多相关文章

  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. 1) B. Drazil and Tiles (类似拓扑)

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

  4. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  5. 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. ...

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

    题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...

  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 Round #285 (Div. 1) A. Misha and Forest 拓扑排序

    题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 L ...

随机推荐

  1. 初次使用引用外部js心得

    在外部引用自己编辑的js时建立链接写在头部中是会出错的,如下图 错误如下: 这是一个是我初学时遇到的一个算是低级错误吧,看到这个错误,我以为的是我引用的js中编辑的代码是不是哪里写错了,但是看了好多遍 ...

  2. layout转Bitmap

    业务需求详细描述:最近产品说要在分享的商品图中添加一些其他图片和文字,然后拼接为一张图片,再分享到微信朋友圈,于是我就一脸懵逼了,但是没办法还是得做额! 然后整理了一下思路,主要有这么两条路线: 自己 ...

  3. 【译】OpenStack Heat基础介绍

    原文:http://blog.scottlowe.org/2014/05/01/an-introduction-to-openstack-heat/ 本文将简要地介绍OpenStack Heat. H ...

  4. mac homebrew安装

    http://book.51cto.com/art/201107/278761.htm 3.2.3 使用 Homebrew 安装 Git Mac OS X 有好几个包管理器,用于管理一些开源软件在 M ...

  5. Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...

  6. Android系统固件定制方式

    target_product.mkAndroid系统在构建关于某种产品的固件时,一般会根据特定于该产品的具体target_product.mk来配置生成整个Android系统./target_prod ...

  7. ZGC,一个超乎想象的垃圾收集器

    Z Garbage Collector,即ZGC,是一个可伸缩的.低延迟的垃圾收集器,主要为了满足如下目标进行设计: 停顿时间不会超过10ms 停顿时间不会随着堆的增大而增大(不管多大的堆都能保持在1 ...

  8. 03pandas

    一.pandas简述 1)pandas是一个开源的,BSD许可的库,为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 2)numpy能够帮助我们处理数值,但是pandas除了处理数 ...

  9. 洛谷——P3939 数颜色(暴力vecotr+二分)

    P3939 数颜色 $vecotr$里二分就是好用,全是$STL$ 颜色数目比较少,可以对每一种颜色弄一个$vector$记录一下,查找$l,r$内颜色数为$x$的兔子数,直接在$G[x]$这个$ve ...

  10. MySQL Utilities管理工具

    前提: 1.安装MySQL Utilities工具 2.复制my_print_defaults命令至/usr/bin下或写入环境变量. 卸载方式: python ./setup.py clean -- ...