POJ 1979--DFS or BFS(水)
来源:互联网 发布:电商平台常用的js特效 编辑:程序博客网 时间:2024/05/05 03:13
题意:。表示能走,#表示不能走,@为起点,求从起点出发能走的点的数目
分析:简单的BFS或DFS
BFS代码:
#include<iostream>#include<cstring>#include<queue>using namespace std;int n,m;char a[100][100];int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}};int cnt;int sx,sy;struct h{int x,y;};queue<h> q;void bfs(){h tmp;while(!q.empty()){tmp=q.front();q.pop();for(int i=0;i<4;i++){h tmp2;tmp2.x=tmp.x+d[i][0];tmp2.y=tmp.y+d[i][1];if(tmp2.x>=0&&tmp2.x<m&&tmp2.y>=0&&tmp2.y<n&&a[tmp2.x][tmp2.y]!='#'){cnt++;a[tmp2.x][tmp2.y]='#';q.push(tmp2);}}}}int main(){while(cin>>n>>m){if(!n&&!m) break;while(!q.empty()) q.pop();for(int i=0;i<m;i++)for(int j=0;j<n;j++){cin>>a[i][j];if(a[i][j]=='@') sx=i,sy=j;}a[sx][sy]='#';h tmp;tmp.x=sx,tmp.y=sy;cnt=1;q.push(tmp);bfs();cout<<cnt<<endl;}}
DFS代码:
#include<iostream>#include<cstring>using namespace std;int n,m;char a[100][100];int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}};int cnt;int sx,sy;void dfs(int x,int y){if(a[x][y]=='#') return;a[x][y]='#';cnt++;for(int i=0;i<4;i++){int dx=x+d[i][0];int dy=y+d[i][1];if(dx>=0&&dx<m&&dy>=0&&dy<n){dfs(dx,dy);}}}int main(){while(cin>>n>>m){if(!n&&!m) break;for(int i=0;i<m;i++)for(int j=0;j<n;j++){cin>>a[i][j];if(a[i][j]=='@') sx=i,sy=j;}cnt=0;dfs(sx,sy);cout<<cnt<<endl;}}
0 0
- POJ 1979--DFS or BFS(水)
- POJ 1979 Red and Black(BFS or DFS)
- POJ 3984 迷宫问题(BFS or DFS)
- POJ 2386 Lake Counting (DFS or BFS)
- POJ 2386 Lake Counting(dfs or bfs)
- poj 1979DFS&&BFS
- (水)POJ-1979 Red and Black(dfs,bfs)
- (水)POJ-1426 BFS,DFS
- POJ 1979 简单dfs||bfs
- POJ 1979 3669 DFS BFS
- POJ 3083(DFS+BFS)
- POJ 3083 (bfs + dfs)
- Hdu1242(bfs+优先队列or dfs)
- HDU1241 Oil Deposits(dfs or bfs)
- TOJ3100 BFS or DFS
- 二分法+BFS(or DFS)
- hdu 6165(dfs or bfs or tarjan+topsort)
- poj 1979 Red and Black(DFS||BFS)
- 文章标题
- android笔记之activity与broadreceiver通信
- ANDROID基础学习笔记_1_方法的四种绑定方式
- FM增加音量,到安全指数没有弹窗提醒
- Timer和TimerTask
- POJ 1979--DFS or BFS(水)
- LeetCode 62 Unique Paths 解题报告
- Leetcode: Evaluate Reverse Polish Notation
- JavaScript中__proto__与prototype的关系
- lxml的简单
- 九大排序算法再总结
- 【学C++之初】标准化输出小数点后两位
- 玩转图片Base64编码
- 第二章第八题