hdu BFS 2952
来源:互联网 发布:单片机数字式温度计 编辑:程序博客网 时间:2024/05/18 01:57
题目很简单,用DFS也可以写。
只是先熟悉一下BFS的思想。
AC代码:
#include<iostream>#include<cstdio>#include<queue>using namespace std;#define MAXN 103struct node{int i, j;};int n, m,ship[MAXN][MAXN],vis[MAXN][MAXN];node a;queue<node> q;void bfs(int i,int j){if (ship[i][j] == 0 || vis[i][j] == 1){return;}vis[i][j] = 1;if (ship[i - 1][j] == 1){a.i = i - 1;a.j = j;q.push(a);}if (ship[i + 1][j] == 1){a.i = i + 1;a.j = j;q.push(a);}if (ship[i][j-1] == 1){a.i = i;a.j = j - 1;q.push(a);}if (ship[i][j + 1] == 1){a.i = i;a.j = j + 1;q.push(a);}}int main(){//freopen("TestDate.txt", "r", stdin);int tn,i,j,vist;char ch;cin >> tn;while (tn--){memset(ship, 0, sizeof(ship));memset(vis, 0, sizeof(vis));cin >> n >> m;for (i = 1; i <= n; i++){for (j = 1; j <= m; j++){cin >> ch;if (ch == '#'){ship[i][j] = 1;}else{ship[i][j] = 0;}}}vist = 0;for (i = 1; i <= n; i++){for (j = 1; j <= m; j++){if (ship[i][j] == 1 && vis[i][j] == 0){bfs(i, j);while (!q.empty()){a = q.front();q.pop();bfs(a.i, a.j);}vist++;}}}cout << vist << endl;}return 0;}
0 0
- HDU-2952 BFS
- hdu BFS 2952
- HDU bfs
- hdu 2952 dfs|bfs|union-find sets
- HDU 2952 Counting Sheep(bfs)
- HDU 2952 Counting Sheep ( DFS + BFS )
- hdu 1175 bfs
- HDU 1072(BFS)
- HDU 1429(BFS )
- hdu 1026(BFS)
- HDU 4039 BFS
- hdu 1312 bfs
- hdu 1253 三维bfs
- hdu 1242 bfs
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- bfs hdu 1026
- hdu-2757 BFS
- 链表问题:如何反转链表?
- 利用Python递归列举目录下的文件并进行有序的排列
- 面向对象程序设计上机练习七(类和对象)
- BGP 配置
- python学习-字符串操作
- hdu BFS 2952
- Outlook2013/2016中的Gmail设置
- hdu 2795 billboard(线段树)
- ServletConfig和ServletContext接口
- Ubuntu16.04下python+PyCharm开发环境的搭建
- [dfs 离线] Hillan模拟赛 C.可持久化的书橱
- ecshop数据库表6(ecs_admin_user)管理表
- Caffe中crop_layer层的理解和使用
- KMP循环节