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

來源:互聯網
上載者:User

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

題目地址:Arthur and Walls
這題有一個腦洞,對於當前的點(i,j)並且此點為”*”來說,若存在包含它的2*2正方形中除了它自己外,另外三個點都是”.”,那麼這個點就必須要變成”.”。由於去掉這個點之後會對周圍的8個點造成影響,所以可以用BFS去搜。WA第12組的應該是只考慮了會影響到周圍的4個點了。
代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=40000+10;char mp[2015][2015];int n, m;int jx[]={0,0,1,-1,1,-1,1,-1};int jy[]={1,-1,0,0,1,-1,-1,1};bool check(int x, int y){        if(x<0||x>n||y<0||y>m) return false;        if(mp[x][y]=='.') return true;        return false;}bool Judge(int x, int y){        if(mp[x][y]!='*') return false;        if(check(x+1,y)&&check(x+1,y+1)&&check(x,y+1)) return true;        if(check(x+1,y)&&check(x+1,y-1)&&check(x,y-1)) return true;        if(check(x-1,y)&&check(x-1,y+1)&&check(x,y+1)) return true;        if(check(x,y-1)&&check(x-1,y-1)&&check(x-1,y)) return true;        return false;}queue<pair<int,int> >q;void bfs(){        int i, x, y, a, b;        while(!q.empty()){                x=q.front().first;y=q.front().second;                q.pop();                if(mp[x][y]=='.') continue ;                mp[x][y]='.';                for(i=0;i<8;i++){                        a=x+jx[i];                        b=y+jy[i];                        if(a>=0&&a<n&&b>=0&&b<m&&Judge(a,b)){                                q.push(make_pair(a,b));                        }                }        }}int main(){        int i, j;        while(scanf("%d%d",&n,&m)!=EOF){                for(i=0;i<n;i++){                        scanf("%s",mp[i]);                }                for(i=0;i<n;i++){                        for(j=0;j<m;j++){                                if(Judge(i,j))                                        q.push(make_pair(i,j));                        }                }                bfs();                for(i=0;i<n;i++){                        printf("%s\n",mp[i]);                }        }        return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.