Codeforces Round #292 (Div. 1) - B. Drazil and Tiles
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 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 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.
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".
题意:用一些1*2的瓷砖覆盖m*n的网格(有障碍),问方案是否唯一,是则输出覆盖的方案。(1 ≤ n, m ≤ 2000).
这题像极了某次cf的一道题(判断网格中是否有环),不过那题数据比较小,直接暴力的拓扑排序即可。
而这题为了在o(m*n)内构造出解,需要维护一个保存所有度为1的点的队列,以及一个保存所有点的度数的数组,每次将一个点出队,同时将唯一的那个与该点相邻的点删掉,然后更改删掉的点的相邻的点的度数(常数时间),度为1的话即入队。
当发现队列空的时候,表明找不到度为1的点了,这时要不是全部覆盖完了,就是存在环或者单独的点(这种情况显然是无解或者多解),再循环判断一遍即可。
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
#define MAXN 2010 struct Point
{
int x,y;
};
char G[MAXN][MAXN];
int degree[MAXN][MAXN]={};
int m,n;
bool isin(int x,int y)
{
return x>= && x<m && y>= && y<n;
} int dx[]={,,-, };
int dy[]={,, ,-};
int getDegree(int x,int y)
{
if(G[x][y]=='*')
return ;
int sum=;
for(int i=;i<;i++)
{
int newx=x+dx[i];
int newy=y+dy[i];
sum+=(isin(newx,newy) && G[newx][newy]!='*');
}
return sum;
} int main()
{
cin>>m>>n;
for(int i=;i<m;i++)
scanf("%s",G[i]); queue<Point> Q;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
degree[i][j]=getDegree(i,j);
if(degree[i][j] == )
Q.push(Point{i,j});
} // int dx[]={1,0,-1, 0};
// int dy[]={0,1, 0,-1};
char oldtype[]={'^','<','v','>'};
char newtype[]={'v','>','^','<'};
while(!Q.empty())
{
Point x=Q.front();
Q.pop();
degree[x.x][x.y]=;
for(int i=;i<;i++)
{
int newx=x.x+dx[i];
int newy=x.y+dy[i];
if(isin(newx, newy) && degree[newx][newy])
{
degree[newx][newy]=;
G[x.x][x.y]=oldtype[i];
G[newx][newy]=newtype[i];
// 更新与(newx, newy)相邻的点的度数
for(int j=;j<;j++)
{
int curx=newx+dx[j];
int cury=newy+dy[j];
if(isin(curx, cury) && degree[curx][cury])
{
degree[curx][cury]--;
if(degree[curx][cury]==)
Q.push(Point{curx, cury});
}
}
break;
}
}
} for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(G[i][j]=='.')
{
puts("Not unique");
return ;
}
} for(int i=;i<m;i++)
puts(G[i]); return ;
}
Codeforces Round #292 (Div. 1) - B. Drazil and Tiles的更多相关文章
- 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. 2) D. Drazil and Tiles [拓扑排序 dfs]
传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...
- 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 ...
- #292 (div.2) D.Drazil and Tiles (贪心+bfs)
Description Drazil created a following problem about putting × tiles into an n × m grid: "The ...
- Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...
随机推荐
- unity3d优化IOS
1. using UnityEngine; class GarbageCollectManager : MonoBehaviour { public int frameFreq = 30; ...
- 5 Java学习之 泛型
1. 基本概念 泛型是Java SE 1.5的新特性,泛型的本质是 参数化类型 ,也就是说所操作的 数据类型 被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为 ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- linux下U盘的读取
1.虚拟机vmware右下角,找到大容量存储设备图标,右键->connect(disconect from host):使U盘连接到虚拟机中来. 2.打开终端:fdisk -l [root@lo ...
- Java字符串的10大热点问题,你都懂吗?
转自 威哥干JAVA http://www.codingke.com 下面我为大家总结了10条Java开发者经常会提的关于Java字符串的问题,如果你也是Java初学者,仔细看看吧: 1.如何比较字符 ...
- SQL Server 2005、SQL Server 2008版本比较
SQL Server 2005的版本有SQL Server 2005企业版(Enterprise).SQL Server 2005标准版(Standard) 和SQL Server 2005工作组版( ...
- 动态IP无法获取默认网关,显示0.0.0.0的解决办法
IP地址使用自动获取IP方式,可以获取到IP地址和子网掩码,默认网关无法获取,显示0.0.0.0,通过修复Winsock和LSP可以解决该问题,具体步骤如下:一.修复winsock1.单击开始> ...
- IOS创建开源库步骤,提交cocoa pods官网,别人可以使用
1.打开终端进入某个目录执行 pod lib create BMBlinkButton,按命令步骤执行. 2.目录结构 3.修改BMBlinkButton.podspec文件 4.进入Example ...
- UIGestureRecognizer手势
常用手势: 滑动,轻点,捏合,旋转,拖拽,长按 1.滑动(快速滑动) let swipeUp = UISwipeGestureRecognizer(target: self, action: Sele ...
- C++ Primer 5th 第7章 类
类的基本思想是数据抽象和封装,定义类就是定义一个抽象数据类型. 类中的所有成员必须在类中声明,也即默认定义在类中的成员全部为声明,除非显式的定义成员函数的函数体.成员函数是在类中声明的,定义可以在类内 ...