poj1979 Red And Black(DFS)
2017-11-03 11:09
357 查看
题目链接
http://poj.org/problem?id=1979
思路
floodfill问题,使用dfs解决
代码
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 20; char maze ; int visit ; int dir[4][2] = {{-1, 0}, {0 ,1}, {1, 0}, {0, -1}}; int nums; int w, h; void dfs(int r, int c){ visit[r][c] = 1; for(int i=0; i<4; i++){ int nextr = r + dir[i][0]; int nextc = c + dir[i][1]; if(nextr>=0 && nextr<h && nextc>=0 && nextc<w && maze[nextr][nextc]!='#' && !visit[nextr][nextc]){ visit[nextr][nextc] = 1; nums++; dfs(nextr, nextc); } } } int main(){ //freopen("poj1979.txt", "r", stdin); int sr, sc; while(cin>>w>>h && w && h){ nums = 1; memset(visit, 0, sizeof(visit)); for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ cin>>maze[i][j]; if(maze[i][j] == '@'){ sr = i; sc = j; } } } dfs(sr, sc); cout << nums <<endl; } return 0; }
相关文章推荐
- POJ 1979-Red and Black(DFS)
- poj 1979 Red and Black(DFS||BFS)
- Poj1979.Red and Black(DFS)
- poj 1979 Red and Black -- dfs
- poj 1979 , zoj 2165 Red and Black【dfs,入门题】
- poj1979 Red and Black(dfs判断联通分量)
- 深度优先搜索(DFS):POJ1979--Red and Black
- POJ 1979 Red and Black(DFS)
- POJ 1979 Red and Black (dfs)
- POJ 1979 Red and Black(dfs)
- poj1979 Red and Black (DFS)
- POJ 1979 Red and Black(BFS or DFS)
- POJ 1979 Red and Black (zoj 2165) DFS
- poj 1979 Red and Black(dfs)
- poj1979 Red and Black(DFS)
- poj 1979 Red and Black ---DFS
- POJ-1979 Red and Black(DFS)
- POJ-1979(DFS) Red and Black
- POJ 1979 Red and Black (zoj 2165) DFS
- POJ 1979 Red and Black(DFS)