\(\\\)

\(Description\)


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

骨牌不能放到网格以外,不能重叠,不能覆盖在障碍物上。

  • \(N,M\le 2000\)

\(\\\)

\(Solution\)


巧妙的思路题。

注意到有一些位置的方案是唯一的。如果一个空格的周围四联通部分只有一个空格,那么必定有一个骨牌要放在这两个格子上。同时,这一次放置可能会影响另一个格子的四联通部分的选择方案。

这就像一个拓扑排序。首先记录每一个点四联通的格子里空地的数量,将数量为\(1\)的放进队列里,然后逐个放置,\(check\) 周围是否有新出现的方案唯一的格子。

最后如果所有空地被填满了,方案就唯一,反之多解。

出题人比较良心,无解和多解都输出 "Not unique" ,所以不需要再判断无解。

\(\\\)

\(Code\)


#include<cmath>
#include<queue>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define R register
#define gc getchar
#define N 2010
using namespace std; inline int rd(){
int x=0; bool f=0; char c=gc();
while(!isdigit(c)){if(c=='-')f=1;c=gc();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
return f?-x:x;
} char mp[N][N]; int n,m,cnt,num[N][N],deg[N][N]; int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0}; queue<pair<int,int> > q; inline void inq(int x,int y){
for(R int i=0;i<4;++i){
--deg[x+dx[i]][y+dy[i]];
if(deg[x+dx[i]][y+dy[i]]==1) q.push(make_pair(x+dx[i],y+dy[i]));
}
} int main(){
n=rd(); m=rd();
char c=gc();
for(R int i=1;i<=n;++i)
for(R int j=1;j<=m;++j){
while(c!='.'&&c!='*') c=gc();
mp[i][j]=c; c=gc();
}
for(R int i=1;i<=n;++i)
for(R int j=1;j<=m;++j)
if(mp[i][j]=='.'){
deg[i][j]=(mp[i-1][j]=='.')+(mp[i+1][j]=='.')+(mp[i][j-1]=='.')+(mp[i][j+1]=='.');
if(deg[i][j]==1) q.push(make_pair(i,j));
}
while(!q.empty()){
int x=q.front().first;
int y=q.front().second; q.pop();
if(deg[x][y]!=1) continue;
if(mp[x+1][y]=='.'){
mp[x][y]='^';
mp[x+1][y]='v'; inq(x+1,y);
}
if(mp[x-1][y]=='.'){
mp[x-1][y]='^';
mp[x][y]='v'; inq(x-1,y);
}
if(mp[x][y+1]=='.'){
mp[x][y]='<';
mp[x][y+1]='>'; inq(x,y+1);
}
if(mp[x][y-1]=='.'){
mp[x][y-1]='<';
mp[x][y]='>'; inq(x,y-1);
}
}
for(R int i=1;i<=n;++i)
for(R int j=1;j<=m;++j)
if(mp[i][j]=='.'){puts("Not unique");return 0;}
for(R int i=1;i<=n;++i){
for(R int j=1;j<=m;++j) putchar(mp[i][j]);
puts("");
}
return 0;
}

[ CodeForces 515 D ] Drazil and Tiles的更多相关文章

  1. 【codeforces 515D】Drazil and Tiles

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

  2. 【codeforces 516B】Drazil and Tiles

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

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

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

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

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

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

  7. [codeforces 516]A. Drazil and Factorial

    [codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define  ...

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

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

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

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

随机推荐

  1. Java电商项目-6.实现门户首页数据展示_Redis数据缓存

    目录 项目的Github地址 需求介绍 搭建Redis集群环境 下面先描述单机版redis的安装 下面将进行Redis3主3从集群环境搭建 基于SOA架构, 创建门户ashop-portal-web门 ...

  2. Ubuntu 16.04安装VirtualBox 5.1实现无缝模式

    个人电脑版的虚拟机推荐使用VirtualBox,因为其免费,比起VMware到处要找破解强得多,且最重要的一点是无缝模式,让其感觉不出再用两个操作系统. 下载: wget http://downloa ...

  3. 携程Apollo(阿波罗)配置中心使用Google代码风格文件(在Eclipse使用Google代码风格)(配合阿里巴巴代码规约快速设置)

    Apollo默认使用了Google的代码风格,文件放在这里: https://github.com/ctripcorp/apollo/tree/master/apollo-buildtools/sty ...

  4. SAS编程基础 - 数据获取与数据集操作(1)

    1. 数据来源 SAS数据来源主要有两种:一是通过input语句创建,另外一种方式是通过外部数据文件获取. 1.1 libname 1.2 odbc 1.3 passthrough 1.4 impor ...

  5. xib或者storyboard设置颜色偏问题(与代码设置颜色不一致)

    选中xib中的label,在右边栏的第三个标签页中第三项是User Defined Runtime Attributes 添加一个keyPath,keyPath值为layer.borderWidth, ...

  6. Echarts 如何使用 bmap 的 API

    使用 Echarts 在绘制 Binning on map 的图形时(其实也就是 在地图上绘制热力色块图) 解决因为数据量过大,希望在拖拽加载或者缩放加载的时候,根据可视区域的经纬度范围,来请求相应的 ...

  7. iOS开发——常见BUG——导航控制器中的子控制器设置StatusBar状态失效的问题

    iOS9之前控制StatusBar的两种方式: 第一种方式:全局控制StatusBar 1. 在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppe ...

  8. MySQL InnoDB 快速导入数据

    今天把需要分析的数据导入到数据库中. 数据将近7000万条,在txt文件中存放,共5.75G.采用Load data infile 导入,最后花了18个小时导入.主要做了以下修改: 1. MySQL优 ...

  9. HDU 5979 Convex【计算几何】 (2016ACM/ICPC亚洲区大连站)

    Convex Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  10. 【Silverlight】Bing Maps学习系列(二):通过Bing Maps Silverlight Control如何显示地图(转)

    [Silverlight]Bing Maps学习系列(二):通过Bing Maps Silverlight Control如何显示地图 如本系列第一篇你所介绍的,开发基于Silverlight的Bin ...