\(\\\)

\(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. Ionic3 填坑记录 - java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

    1  错误:Unable to merge dex 执行打包命令时 ionic cordova build android --prod 报如下错误 2 原因 重复引用了同一个包 如上图所示, com ...

  2. Codeforces 479B. Towers 暴力

    纯暴力..... B. Towers time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. mainboard

    MAINBOARD ★ CPU(type, speed, amount, cache, slot or socket, fan) ★ RAM(the most capacity, amount, fr ...

  4. System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。

    错误背景: 操作系统:编程环境:VS2013.  语言:VB.net:  数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...

  5. DataNucleus之JDO操作演示样例

    JDO(Java Data Object )是Java对象持久化的新的规范.也是一个用于存取某种数据仓库中的对象的标准化API. 注意JDO是一种规范,而不是一个产品.而DataNucleus正是实现 ...

  6. android 获取手机信息工具类

    package com.yqy.yqy_listviewheadview; import android.content.Context; import android.telephony.Telep ...

  7. react 引入 百度地图API

    使用 Echarts 的地图的时候,发现报错,说 Bmap api is not loaded 百度地图API没有加载 乍一想,Echarts 用的也是 百度地图 啊,没有引入百度地图,还用个啥,当然 ...

  8. go2基本类型

    /* Go基本类型 布尔型:bool - 长度:1字节 - 取值范围:true, false - 注意事项:不可以用数字代表true或false 整型:int/uint - 根据运行平台可能为32或6 ...

  9. HDU1052Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. flask request 获取json内容2种方式

    # -*- coding: utf-8 -*-from flask import request, jsonify, json, Moduleimport loggingfrom web.utils. ...