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;
}
posted @ 2016-10-25 15:47  StevenLuke  阅读(112)  评论(0编辑  收藏  举报