Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
生活随笔
收集整理的這篇文章主要介紹了
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 }?
轉載于:https://www.cnblogs.com/yxg123123/p/6827695.html
總結
以上是生活随笔為你收集整理的Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dagger2 依赖注入
- 下一篇: PYTHON_DACORATOR