原文链接http://www.cnblogs.com/zhouzhendong/p/8990658.html

题目传送门 - CodeForces 516B

题意

  给出一个$n\times m$的矩形。其中有些位置已经被覆盖。

  现在让你用$1\times 2$的小矩形来覆盖其他地方,小矩形不能重叠。

  如果有多种覆盖方案,或者无法把没被覆盖的地方全部覆盖,那么输出特殊信息。否则输出唯一的方案。

  $n,m\leq 2000$

题解

  乍一看,我还以为是经典的二分图匹配题目。但是由于$n,m$都很大,所以不行。

  注意到题意概括中第$3$行的特殊性。

  我们考虑这样一个算法:

  首先,如果原矩阵中有被四面包围的空地,那么显然无解。

  对于原矩阵中被三面包围的空地,显然只有一种方法可以填充这个空地。

  我们考虑使用一个队列,一开始加入被三面包围的空地,然后在填充的过程中,影响到了其他的空地,并加入新的待填充空地。

  在填充的过程中可以顺便判掉某些无解的情况。

  如果按照上述操作,无法把矩阵填充满,那么就是无解或者多解了。

  至于多解的情况,很显然,任何一块连通空地如果不能找到被三面包围的空地,那么显然有多种方案来填充。

  至于为什么,自己yy。

代码

#include <bits/stdc++.h>
using namespace std;
const int N=2005;
int dx[4]={ 0, 0, 1,-1};
int dy[4]={-1, 1, 0, 0};
int n,m;
char g[N][N];
int head=0,tail=0,x[N*N],y[N*N];
bool flag=1;
void ckadd(int i,int j){
if (g[i][j]!='.')
return;
int cnt=0;
for (int k=0;k<4;k++)
if (g[i+dx[k]][j+dy[k]]=='.')
cnt++;
if (cnt==1)
tail++,x[tail]=i,y[tail]=j;
if (cnt==0)
flag=0;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%s",g[i]+1);
for (int i=1;i<=n;i++)
g[i][0]=g[i][m+1]='*';
for (int i=1;i<=m;i++)
g[0][i]=g[n+1][i]='*';
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
ckadd(i,j);
while (head<tail&&flag){
head++;
int i=x[head],j=y[head];
if (g[i][j]!='.')
continue;
int cnt=0;
for (int k=0;k<4;k++)
if (g[i+dx[k]][j+dy[k]]=='.'){
cnt++;
if (k==0)
g[i][j]='>',g[i+dx[k]][j+dy[k]]='<';
if (k==1)
g[i][j]='<',g[i+dx[k]][j+dy[k]]='>';
if (k==2)
g[i][j]='^',g[i+dx[k]][j+dy[k]]='v';
if (k==3)
g[i][j]='v',g[i+dx[k]][j+dy[k]]='^';
for (int t=0;t<4;t++)
ckadd(i+dx[k]+dx[t],j+dy[k]+dy[t]);
}
if (cnt==0)
flag=0;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (g[i][j]=='.')
flag=0;
if (!flag)
puts("Not unique");
else {
for (int i=1;i<=n;i++,puts(""))
for (int j=1;j<=m;j++)
putchar(g[i][j]);
}
return 0;
}

  

CodeForces 516B Drazil and Tiles 其他的更多相关文章

  1. CodeForces - 516B Drazil and Tiles(bfs)

    https://vjudge.net/problem/CodeForces-516B 题意 在一个n*m图中放1*2或者2*1的长方形,问是否存在唯一的方法填满图中的‘.’ 分析 如果要有唯一的方案, ...

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

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

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

  5. Drazil and Tiles CodeForces - 516B (类拓扑)

    Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a gr ...

  6. 【codeforces 516B】Drazil and Tiles

    题目链接: http://codeforces.com/problemset/problem/516/B 题解: 首先可以得到一个以‘.’为点的无向图,当存在一个点没有边时,无解.然后如果这个图边双联 ...

  7. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

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

  8. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

  9. [ CodeForces 515 D ] Drazil and Tiles

    \(\\\) \(Description\) 给出一个\(N\times M\) 的网格,一些位置是障碍,其他位置是空地,求是否存在一个用 \(1\times 2\)的骨牌铺满空地的方案,以及方案是否 ...

随机推荐

  1. 【原创】运维基础之Docker(5)docker部署airflow

    部署方式:docker+airflow+mysql+LocalExecutor 使用airflow的docker镜像 https://hub.docker.com/r/puckel/docker-ai ...

  2. Go语言从入门到放弃(一) 变量/常量/函数

    HelloWorld 我们先看看一个最简单的HelloWorld代码 package main import "fmt" func main() { fmt.Println(&qu ...

  3. 大数据python词频统计之hdfs分发-cacheArchive

    -cacheArchive也是从hdfs上进分发,但是分发文件是一个压缩包,压缩包内可能会包含多层目录多个文件 1.The_Man_of_Property.txt文件如下(将其上传至hdfs上) ha ...

  4. hadoop常用命令详细解释

    hadoop命令分为2级,在linux命令行中输入hadoop,会提示输入规则 Usage: hadoop [--config confdir] COMMAND where COMMAND is on ...

  5. sleep()和wait()的区别及wait方法的一点注意事项

    一.查看API sleep是Thread类的方法,导致此线程暂停执行指定时间,给其他线程执行机会,但是依然保持着监控状态,过了指定时间会自动恢复,调用sleep方法不会释放锁对象. 当调用sleep方 ...

  6. Connection reset by [server_ip] port 22 (hexo d 部署博客出错)

    问题 在使用 hexo d 部署博客和使用 Git/Github 进行 git push -u origin master 时遇到了以下问题: git -c diff.mnemonicprefix=f ...

  7. iframe与主框架跨域相互访问方法

    iframe 与主框架相互访问方法  http://blog.csdn.net/fdipzone/article/details/17619673/ 1.同域相互访问 假设A.html 与 b.htm ...

  8. HTML5-长按事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  9. 【sqli-labs】Less17

    Less17: POST注入,UPDATE语句,有错误回显 新知识点: 1. update注入方法 参考:http://www.mamicode.com/info-detail-1665678.htm ...

  10. tomcat启动报错:Annotation-specified bean name 'patrolTrailServiceImpl' for bean class [cn.oppo.inventoryService.patrolTrailServiceImpl] con

    注释-为bean类[目录服务patrolTrailServiceImpl]指定的bean名称“patrolTrailServiceImpl”与同名和[目录服务patrolTrailServiceImp ...