原文链接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. MySQL - 查看慢SQL

    查看MySQL是否启用了查看慢SQL的日志文件 (1) 查看慢SQL日志是否启用 mysql> show variables like 'log_slow_queries'; +-------- ...

  2. /var/run/yum.pid 已被锁定,PID 为 2925 的另一个程序正在运行

    解决办法:直接在终端运行 rm -f /var/run/yum.pid 将该文件删除,然后再次运行yum.

  3. [转]golang中defer的使用规则

    转载于:https://studygolang.com/articles/10167 在golang当中,defer代码块会在函数调用链表中增加一个函数调用.这个函数调用不是普通的函数调用,而是会在函 ...

  4. Java对象之间的深度复制拷贝

    /* * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  5. linux 批量进行:解压缩某一类压缩文件类型的文件

    1: 编写脚本 [oracle@oracle oracle]$ vim unzip.sh ziphome=/u01/app/oracle ziplist=`du -a $ziphome |grep ' ...

  6. SpringBoot图片上传(一)

    简单描述:点击上传文件的图标,上传文件,上传成功后,图标编程上传的图片. 吐槽:文件上传下载这种东西,总是感觉莫名的虚-_-||  也不知道是造了什么孽,(其实就是IO File这一块的知识了解的不太 ...

  7. hdu2121 最小树形图的虚根

    /* 最小树形图的第二题,终于有了一些理解 具体看注释 */ /* 无定根的最小树形图 建立虚root 每次只找最短的那条入边 最小树形图理解: 第一步:寻找最短弧集E:扫一遍所有的边,找到每个点权值 ...

  8. C语言将字符串转json

    示例代码: #include <stdio.h> #include <string.h> #include <stdlib.h> char *strrpc(char ...

  9. 部署MySQL5.7时的权限问题

    本周部署MySQL5.7的时候遇到这样的问题,在初始化的时候,总是失败,并且报错: 2019-01-09T09:47:13.957685Z 0 [ERROR] InnoDB: Operating sy ...

  10. windows下bat批处理执行sql语句__Mysql

    直接上代码: @ECHO OFF SET dbhost=主机名(例如:127.0.0.1)SET dbuser=用户名(例如:root)SET dbpasswd=用户密码(例如:root)SET db ...