• Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs


    题目链接:

    http://codeforces.com/contest/525/problem/D
    

    题意

    给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最少的"*",使的每一个"."的联通块都是矩形
    

    题解:

    2*2 的矩形中,如果有一个 '*' 与三个 '.' ,那么这个 '*' 就一定要变成 ‘.' ,然后bfs
    

    代码

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 #define mem(a) memset(a,0,sizeof(a))
     5 #define mp(x,y) make_pair(x,y)
     6 const int INF = 0x3f3f3f3f;
     7 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
     8 inline ll read(){
     9     ll x=0,f=1;char ch=getchar();
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    11     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    12     return x*f;
    13 }
    14 //////////////////////////////////////////////////////////////////////////
    15 const int maxn = 2e3+10;
    16 
    17 char mp[maxn][maxn];
    18 int dx[8] = {0,0,1,-1,1,1,-1,-1};
    19 int dy[8] = {1,-1,0,0,1,-1,1,-1};
    20 int n,m;
    21 
    22 void check(int x,int y){
    23     int cnt = 0;
    24     for(int i=0; i<2; i++)
    25         for(int j=0; j<2; j++){
    26             if(mp[x+i][y+j] == '.')
    27                 cnt++;
    28         }
    29     if(cnt == 3){
    30         for(int i=0; i<2; i++)
    31             for(int j=0; j<2; j++)
    32                 mp[x+i][y+j] = '.';
    33         for(int i=0; i<8; i++){
    34             int tx=x+dx[i],ty=y+dy[i];
    35             if(tx<1 || tx>n || ty<1 || ty>m) continue;
    36             check(tx,ty);
    37         }
    38     }
    39 }
    40 
    41 int main(){
    42     n=read(),m=read();
    43     for(int i=1; i<=n; i++)
    44         for(int j=1; j<=m; j++)
    45             scanf(" %c",&mp[i][j]); 
    46 
    47     for(int i=1; i<=n; i++)
    48         for(int j=1; j<=m; j++)
    49             check(i,j);
    50 
    51     for(int i=1; i<=n; i++){
    52         for(int j=1; j<=m; j++)
    53             printf("%c",mp[i][j]);
    54         puts("");
    55     }
    56 
    57 
    58     return 0;
    59 }
  • 相关阅读:
    Mysql 分页查询sql优化
    观察者模式之spring事件机制
    封装一个按Key排序的Map工具
    SpringBoot java配置类@Configuration 的两种写法
    最基础前端路由实现,事件popstate使用
    mybatis分页插件PageHelper源码浅析
    看看线程特有对象ThreadLocal
    svn提交错误:Commit failed (details follow): Can't create directory
    mac下修复exfat格式外置硬盘
    [twisted] Multiple users
  • 原文地址:https://www.cnblogs.com/yxg123123/p/6827695.html
Copyright © 2020-2023  润新知