poj1979 BFS/DFS
来源:互联网 发布:centos服务器测速 编辑:程序博客网 时间:2024/05/16 11:27
#include <iostream> //BFS#include <queue>#include <cstring>using namespace std;struct node{ int x; int y; node(){} node(int xx,int yy):x(xx),y(yy){}};int n,m;char g[20][20];int visit[20][20];int ans;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};void bfs(node start){ queue<node>Q; Q.push(start); visit[start.x][start.y]=1; ++ans; while(!Q.empty()) { node temp=Q.front(); Q.pop(); for(int i=0;i<4;i++) { node np;np.x=temp.x+dir[i][0];np.y=temp.y+dir[i][1]; if(np.x>=0&&np.x<n&&np.y>=0&&np.y<m&&!visit[np.x][np.y]&&g[np.x][np.y]!='#') { visit[np.x][np.y]=1; ++ans; Q.push(np); } } }}int main(){ while(cin>>m>>n&&(m+n)) { memset(visit,0,sizeof(visit)); node start; int cur=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { cin>>g[i][j]; if(g[i][j]=='@') { start.x=i;start.y=j; } } ans=0; bfs(start); cout<<ans<<endl; } return 0;}
DFS:
#include <iostream>#include <queue>#include <cstring>using namespace std;struct node{ int x; int y; node(){} node(int xx,int yy):x(xx),y(yy){}};int n,m;char g[20][20];int visit[20][20];int ans;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};void dfs(node start){ visit[start.x][start.y]=1; ++ans; for(int i=0;i<4;i++) { node np(start.x+dir[i][0],start.y+dir[i][1]); if(np.x>=0&&np.x<n&&np.y>=0&&np.y<m&&!visit[np.x][np.y]&&g[np.x][np.y]!='#') dfs(np); }}int main(){ while(cin>>m>>n&&(m+n)) { memset(visit,0,sizeof(visit)); node start; int cur=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { cin>>g[i][j]; if(g[i][j]=='@') { start.x=i;start.y=j; } } ans=0; dfs(start); cout<<ans<<endl; } return 0;}
0 0
- poj1979【基础bfs/dfs】
- poj1979 BFS/DFS
- poj1979--DFS
- poj1979 DFS
- dfs--poj1979
- POJ1979 DFS
- poj1979 DFS
- POJ1979 DFS
- dfs-poj1979
- POJ1979 dfs
- poj1979 DFS
- poj1979-dfs
- POJ1979 FLIP GAME (DFS&BFS都可以)
- poj1979 简单bfs
- poj1979 dfs水题
- POJ1979迷宫问题DFS
- poj1979 Red and Black dfs
- poj1979 Red and Black(dfs)
- [leetcode] 73. Set Matrix Zeroes
- 在ORACLE 12C 上安装APEX 4.2
- LeetCode--No.299--Bulls and Cows
- Mac下Cornerstone错误异常导致打不开
- LeetCode--No.374--Guess Number Higher or Lower
- poj1979 BFS/DFS
- ubuntu16.04 sogoupinyin No such key ‘Gtk/IMMOdule’
- [leetcode] 162. Find Peak Element
- oracle 中文乱码 和 sqlplus 格式输出
- Fill Countries in Python Basemap
- Maven Oracle jdbc驱动
- LeetCode--No.205--Isomorphic Strings
- iOS 友盟微博分享文字+图片+链接实现
- [leetcode] 279. Perfect Squares