Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
来源:互联网 发布:韩版男装淘宝店有哪些 编辑:程序博客网 时间:2024/05/22 04:39
题目链接:http://codeforces.com/problemset/problem/377/A
题解:
有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然为连通的。把问题反过来,其实就是求tot-k的连通图。dfs:在搜索过的空格中做个标记,同时更新连通个数。
代码如下:
#include<cstdio>//hdu3183 CodeForces 377A dfs#include<cstring>#include<cmath>#include<algorithm>#define LL long longusing namespace std;int n,m,k,sum,vis[505][505],path[505][505];char maze[505][505];int dfs(int i, int j){ if(sum==k) return 1; if(i<1 || i>n || j<1 || j>m) return 0; if(maze[i][j]!='.' || vis[i][j]) return 0; sum++; vis[i][j] = 1; path[i][j] = 1; if(dfs(i-1,j)) return 1; if(dfs(i,j-1)) return 1; if(dfs(i+1,j)) return 1; if(dfs(i,j+1)) return 1;}int main(){ scanf("%d%d%d",&n,&m,&k); k = -k; for(int i = 1; i<=n; i++) { getchar(); for(int j = 1; j<=m; j++) { scanf("%c",&maze[i][j]); if(maze[i][j]=='.') k++; } } int B = 0; memset(vis,0,sizeof(vis)); for(int i = 1; !B && i<=n; i++) for(int j = 1; j<=m; j++) { if(maze[i][j]=='.' && !vis[i][j]) { sum = 0; memset(path,0,sizeof(path)); if(dfs(i,j)) { B = 1;//双重循环,要加多个判断 break; } } } for(int i = 1; i<=n; i++) for(int j = 1; j<=m; j++) { if(maze[i][j]=='.') { if(path[i][j]) putchar('.'); else putchar('X'); } else putchar(maze[i][j]); if(j==m) putchar('\n'); } return 0;}
0 0
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
- Codeforces Round #222 (Div. 1) A. Maze
- Codeforces Round #375 (Div. 2) D 连通块
- Codeforces Round #237 (Div. 2) E. Maze 1D
- Codeforces Round #398 (Div. 2) C. Garland —— DFS
- Codeforces Round #268 (Div. 1)B(dfs)
- Codeforces Round #225 (Div. 1)C(dfs+线段树)
- Codeforces Round #429 (Div. 2)-(DFS)
- 【dfs找环】Codeforces Beta Round #80 (Div. 1 Only)——B. Cthulhu
- Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
- Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
- Codeforces Round #222 (Div. 1)
- Codeforces Round #222 (Div. 1)
- Codeforces Round #168 (Div. 1), problem: (B) Zero Tree, DFS
- Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力
- Codeforces Round #168 (Div. 1) B. Zero Tree(树上DFS)
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
- Ogre编译速度慢的一种解决方法
- 欢迎使用CSDN-markdown编辑器
- mac 下mysql 修改密码
- Linux内核分析
- Activity的四种LaunchMode
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
- 电子发烧友论坛
- VM12秘钥,终生。
- C#学习日记 StringBuilder
- finally个人见解
- Windows cordova build Error: Could not find gradle wrapper within Android SDK. Might need to update
- 05-S3C2440学习之内核(初步)编译、配置过程分析
- 关于微信小程序发起的请求不能使用session的解决办法
- 【Java】图片上传