传送门

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. Android开发二维码之坑

    之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...

  2. How to proxy a web site by apache2 in Ubuntu

    Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...

  3. JDBC性能优化篇

    系统性能. 少用Metadata方法     与其它的JDBC方法相比, 由ResultSet对象生成的metadata对象的相对来说是很慢的. 应用程序应该缓存从ResultSet返回的metada ...

  4. MFC技术积累——基于MFC对话框类的那些事儿2

    3. 绘图 3.1 对话框资源编辑 首先通过添加控件的方式来创建一个简单的绘图对话框如图所示,创建步骤为: 第一.在VC++6.0软件环境的灰色空白区域右击,选中Controls,然后会弹出一个控件对 ...

  5. hql语法002

    1. package cn.jbit.hibernatedemo.test; import java.util.Iterator; import java.util.List; import org. ...

  6. 前端基础入门第一阶段-Web前端开发基础环境配置

    Web前端和全栈的定义: A.什么是传统传统web前端:需要把设计师的设计稿,切完图,写标签和样式,实现JS的效果,简而言之即只需要掌握HTML的页面结构,CSS的页面样式,javaScript页面的 ...

  7. c++如何使用全局变量

    在xxxx.h文件中使用extern声明变量: extern int i; 在xxxx.cpp文件中定义变量: int i; 声明和定义都只需一次.

  8. ES6 第三章 变量的解构赋值 具体参照http://es6.ruanyifeng.com

    1.基本用法 let [a, b, c] = [1, 2, 3];左右两边解构格式要保持一致. 2.默认值 let [x, y = 'b'] = ['a']; // x='a', y='b' let ...

  9. Spring-1-IOC

    IOC与DI的区别? IOC:控制反转(Inversion of Control是面向对象的一种设计原则,可以用来降低计算机之间的耦合度,其中最常见的是依赖注入).是实现的目标 DI:是实现IOC的一 ...

  10. 深入理解Spring IoC容器和动态代理机制

    Deployment期间验证 实现一: <bean id="theTargetBean" class="..."/> <bean id=&qu ...