大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案.

一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉.

3 3

**.

.**

...

正解是bfs, 一个点被扩散当且仅当它所在的某个2*2块中只有它为'*'.

#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m;
char s[N][N];
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
queue<pii> q;
int chk(int x, int y) {
if (x<1||x>n||y<1||y>m||s[x][y]=='.') return 0;
if (s[x-1][y]=='.'&&s[x-1][y-1]=='.'&&s[x][y-1]=='.') return 1;
if (s[x-1][y]=='.'&&s[x-1][y+1]=='.'&&s[x][y+1]=='.') return 1;
if (s[x][y-1]=='.'&&s[x+1][y-1]=='.'&&s[x+1][y]=='.') return 1;
if (s[x][y+1]=='.'&&s[x+1][y]=='.'&&s[x+1][y+1]=='.') return 1;
return 0;
} int main() {
scanf("%d%d", &n, &m);
REP(i,1,n) scanf("%s", s[i]+1);
REP(i,1,n) REP(j,1,m) if (chk(i,j)) q.push(pii(i,j));
while (q.size()) {
auto t = q.front(); q.pop();
if (!chk(t.x,t.y)) continue;
s[t.x][t.y] = '.';
REP(i,0,7) {
int x=t.x+dx[i],y=t.y+dy[i];
if (chk(x,y)) q.push(pii(x,y));
}
}
REP(i,1,n) puts(s[i]+1);
}

Arthur and Walls CodeForces - 525D (bfs)的更多相关文章

  1. Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]

    传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...

  2. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  3. Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)

    D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  4. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  5. Jumping on Walls CodeForces - 198B

    Jumping on Walls CodeForces - 198B 应该是一个隐式图的bfs,或者叫dp. 先是一个TLE的O(nklogn) #include<cstdio> #inc ...

  6. Arthur and Table CodeForces - 557C

    Arthur and Table CodeForces - 557C 首先,按长度排序. 长度为p的桌腿有a[p]个. 要使得长度为p的桌腿为最长,那么要按照代价从小到大砍掉sum{长度不到p的腿的数 ...

  7. CodeForces 525D Arthur and Walls

    广搜.看了官方题解才会的..... 定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块. 首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳 ...

  8. codeforces D - Arthur and Walls

    这题说的是给了一个矩阵,必须让.连接起来的图形变成矩形,2000*2000的矩形,那么我们就可以知道了,只要是存在一个有点的区域应该尽量将他削为矩形,那么将这个图形进行缩放,最后我们知道只要存在一个2 ...

  9. cf 525D.Arthur and Walls

    判断2*2的正方形,是不是3个"."1个"*"然后暴力bfs就好.(这种处理也是挺神奇的2333%%题解) #include<bits/stdc++.h& ...

随机推荐

  1. zookeeper注册与发现

    pom.xml添加如下引用: <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId ...

  2. Delphi中PointerMath指令

    Type   Switch   Syntax   {$POINTERMATH ON} or {$POINTERMATH OFF}   Default   {$POINTERMATH OFF}   Sc ...

  3. Entity framework 绑定到Datagridview的添加删除修改

    Entity framework 绑定到Datagridview的添加删除修改 using System; using System.Collections.Generic; using System ...

  4. layer —— 一个简单的jQuery弹出层插件

    layer的使用 4.24更新:注意:layer现在有旧版1.8.5版本和新版本3.0版本的,对应引入的JQ也要不同,相对应的JQ引入1.1和3.1,否则JQ会出问题 4.21更新: 解答4-19的问 ...

  5. DataGridView上下键事件获取到的是上次停留行的内容

    DataGridView上下键事件 在DataGridView中,通过上下键将选中行的内容返回, 问题: 通过上边的方法总是获取到上次停留行的内容,不是当前选中行的内容. winform的项目,使用C ...

  6. MMIO和PIO

    1.概念 内存映射I/O(MMIO)[统一编址]和端口映射I/O(PMIO)[独立/单独编址]是两种互为补充的I/O方法,用于设备驱动程序和设备通信,即在CPU和外部设备之间. (1)在MMIO中,内 ...

  7. linux磁盘命令-lsblk显现磁盘阵列分组

    linux磁盘命令-lsblk显现磁盘阵列分组 lsblk(list block devices)能列出系统上所有的磁盘. lsblk [-dfimpt] [device] 选项与参数: -d :仅列 ...

  8. JS,JQuery小知识

    http://blog.163.com/wumingli456@126/blog/static/28896414201112252456459/?suggestedreading&wumii

  9. 翻译 | Placing Search in Context The Concept Revisited

    翻译 | Placing Search in Context The Concept Revisited 原文 摘要 [1] Keyword-based search engines are in w ...

  10. C#线程同步(4)- 通知&EventWaitHandle一家

    文章原始出处 http://xxinside.blogbus.com/logs/47523285.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...