Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]
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.
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.
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.
3 3
...
.*.
...
Not unique
4 4
..**
*...
*.**
....
<>**
*^<>
*v**
<><>
2 4
*..*
....
*<>*
<><>
1 1
.
Not unique
1 1
*
*
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]的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- 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. ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...
- Codeforces Round #292 (Div. 1) C - Drazil and Park
C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...
- 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 ...
- 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 ...
随机推荐
- hihocoder1779 公路收费
思路: 枚举每个点做根即可. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; const l ...
- hihocoder offer收割编程练习赛9 B 水陆距离
思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- poj2677 Tour
题意: 双调欧几里得旅行商问题. 思路: dp.定义dp[i][j](i <= j)为从点j从右向左严格按照x坐标递减顺序走到点1,之后再从点1从左向右严格按照x坐标递增的顺序走到点i,并且在此 ...
- 谈谈对Android中的消息机制的理解
Android中的消息机制主要由Handler.MessageQueue.Looper三个类组成,他们的主要作用是 Handler负责发送.处理Message MessageQueue负责维护Mess ...
- 微信小程序组件解读和分析:一、view(视图容器 )
view组件说明: 视图容器 跟HTML代码中的DIV一样,可以包裹其他的组件,也可以被包裹在其他的组件内部.用起来比较自由随意,没有固定的结构. view组件的用法: 示例项目的wxml ...
- checking for gcc... no
./configure 后显示checking for gcc... nochecking for cc... nochecking for cl.exe... noconfigure.sh:erro ...
- Android内存泄露(全自动篇)
写了可执行文件启动器Launcher.jar及一些批处理,通过它们就可以自动的以一定的时间间隔提取Hprof和进程的内存信息: 一.需要的库 可执行文件启动器:lib\Launcher.jar 注:关 ...
- pickle 两个使用小方法
def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data ...
- PHP中session和cookie的区别
这个话题无论是系统运维还是PHP开发人员面试时会经常遇到,所以这里也进行一些总结和归纳,session和cookie的具体理论网上比较大,大家googel下均可;系统运维注意区分下session(会话 ...
- sysUpload.vue上传组件 的时候 看进度的时候 不要mock 注释掉 // if (process.env.NODE_ENV !== 'production') require('@/mock')
上传组件 的时候 看进度的时候 不要mock 注释掉 // if (process.env.NODE_ENV !== 'production') require('@/mock') <!-- * ...