传送门

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. canvas基础绘制-绚丽时钟

    效果图: 与canvas基础绘制-绚丽倒计时的代码差异: // var endTime = new Date();//const声明变量,不可修改,必须声明时赋值: // endTime.setTim ...

  2. 【学习笔记】深入理解js原型和闭包(8)——简述【执行上下文】上

    什么是“执行上下文”(也叫做“执行上下文环境”)?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时, ...

  3. 【学习笔记】using namespace std 的作用

    C++编程时几乎每次都敲上using namespace std;但这行代码究竟有什么作用呢? C++标准程序库中的所有标识符都被定义于一个名为std的namespace中. 早些的编码将标准库功能定 ...

  4. css:段落文本两端对齐

    效果图: Css:

  5. windows 下防火墙安全加固,配置规则

    netsh advfirewall firewall: 显示关于防火墙操作的常见命令的帮助信息 netsh advfirewall firewall show rule name=all dir=in ...

  6. Java JDK装配置

     1- 介绍 本文章介绍JAVA开发环境安装是基于:  Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...

  7. HDU 5391 Zball in Tina Town (打表,水)

    题意: Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大1倍.在第二天的时候,它会变大2倍.在第n天的时候,它会变大n倍.zball原来的体积是1.Ti ...

  8. (转)SpringMVC学习(十)——SpringMVC与前台的json数据交互

    http://blog.csdn.net/yerenyuan_pku/article/details/72514022 json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析 ...

  9. DROP TABLE - 删除一个表

    SYNOPSIS DROP TABLE name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP TABLE 从数据库中删除表或视图. 只有其所有 ...

  10. c++ extern

    一.extern关键字的作用 文件中定义的全局变量的可见性扩展到整个程序是在链接完成之后,而在编译阶段,他们的可见性仍局限于各自的文件. 编译器的目光不够长远,编译器没有能够意识到,某个变量符号虽然不 ...