poj 1164 dfs水题
来源:互联网 发布:高圆圆 张亚东 知乎 编辑:程序博客网 时间:2024/06/06 12:05
题意:给出一个二维房间信息,每个格子的数字表示哪边有墙,用位来表示。求一共有几间屋子(由连通的格子组成),以及最大的房间含有的格子数量。
思路:dfs即可。
#include <cstdio>#include <string>#include <vector>#include <iostream>#include <cstdlib>#include <cmath>#define N 55using namespace std;int n,m;int flag[N][N];int s[N][N],num = 0,res = 0;int dfs(int x,int y){ int tmp = 0; flag[x][y] = num; if(!(s[x][y]&1)&& !flag[x][y-1]) tmp += dfs(x,y-1); if(!(s[x][y]&2)&& !flag[x-1][y]) tmp += dfs(x-1,y); if(!(s[x][y]&4)&& !flag[x][y+1]) tmp += dfs(x,y+1); if(!(s[x][y]&8)&& !flag[x+1][y]) tmp += dfs(x+1,y); return tmp+1;}int main(){ int i,j,k; memset(flag,0,sizeof(flag)); scanf("%d %d",&n,&m); for(i = 1;i<=n;i++) for(j = 1;j<=m;j++) scanf("%d",&s[i][j]); for(i = 1;i<=n;i++) for(j = 1;j<=m;j++) if(!flag[i][j]){ num++; k = dfs(i,j); res = max(res,k); } printf("%d\n%d\n",num,res);}
0 0
- poj 1164 dfs水题
- poj 1164 DFS
- poj 1979 dfs水题
- POJ 2488 DFS水题
- POJ 3256 DFS水题
- poj 1164 The Castle (dfs)
- DFS POJ
- POJ DFS
- USACO 2.1.1&&POJ 1164(dfs)
- poj-1164 The Castle 城堡 DFS计数
- poj 1753 Flip Game(搜索:DFS+水题)
- Poj 1321 棋盘问题 (DFS水题)
- POJ 1979 Red and Black (DFS水题)
- 挑战第二章 poj 3009 水题DFS
- POJ 1979 Red and Black DFS水题
- poj 3083 DFS+DFS+BFS
- POJ 1753 ID+DFS
- poj 1950 数字dfs
- java中ArrayList的使用
- XCode: 如何添加自定义代码片段
- 数据类型
- Android中jni使用
- PHP实现无限极分类
- poj 1164 dfs水题
- [leetcode-236]Lowest Common Ancestor of a Binary Tree(C语言)236
- codeforces 552 C Vanya and Scales
- 利用SVN创建Axure RP 7.0团队项目
- CentOS7快速安装lnmp(linux + nginx + mysql + php)
- 错误: 需要class, interface或enum
- 解决Maven更新后错误Dynamic Web Module 3.0 requires Java 1.6 or newer
- 关于负数取模
- Java DecimalFormat 用法