本文主要是介绍poj1979,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简单的dfs
#include <iostream>
#include <cstring>using namespace std;const int MAX_V = 25;int w, h, res;
char maze[MAX_V][MAX_V];
bool used[MAX_V][MAX_V];int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};struct node {int x;int y;
}nodes[MAX_V], start;int dfs(node v) {used[v.x][v.y] = true;for(int i=0; i<4; i++) {node n = {v.x + dx[i],v.y + dy[i]};if(!used[n.x][n.y] && n.x < h && 0 <= n.x && n.y < w && 0 <= n.y && maze[n.x][n.y] == '.') {res++;dfs(n);}}return res;
}int main() {freopen("in.txt", "r", stdin);while(~scanf("%d%d", &w, &h)) {if(w==0 && h==0) break;getchar();memset(used, 0, sizeof(used));for(int i=0; i<h; i++) {for(int j=0; j<w; j++) {scanf("%c", &maze[i][j]);if(maze[i][j] == '@') {start.x = i;start.y = j; }if(maze[i][j] == '.')loc++;}getchar();}res = 1;printf("%d\n", dfs(start));}fclose(stdin);return 0;
}
这篇关于poj1979的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!