poj Red and Black(dfs)

来源:互联网 发布:指南数据库 编辑:程序博客网 时间:2024/06/07 21:55

题目描述:求最多能到达多少个位置

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <queue>#include <stdlib.h>using namespace std;int const maxn = 25;int const dx[] = {0,0, 1,-1};int const dy[] = {1,-1, 0,0};char MP[maxn][maxn];bool vis[maxn][maxn];int n, m, res;bool isIn(int x, int y){    if(x >= 0 && x < m && y >= 0 && y < n)        return true;    return false;}bool isCan(int x, int y){    if(!vis[x][y]&&MP[x][y]!='#')        return true;    return false;}bool isOk(int x, int y){    if(isIn(x, y)&&isCan(x, y))        return true;    return false;}void dfs(int x, int y){    int nx, ny;    for(int i = 0; i < 4; i ++)    {        nx = x + dx[i];        ny = y + dy[i];        if(isOk(nx, ny))        {            res++;            vis[nx][ny] = true;            dfs(nx,ny);        }    }}void init(){    res = 0;    memset(vis, false, sizeof(vis));}int main(){    while(cin>>n>>m)    {        if(!n&&!m)break;        init();        int x, y;        bool fg = false;        for(int i = 0; i < m; i++)        {            scanf("%s",MP[i]);            if(!fg)                for(int j = 0; j < n; j++)                    if(MP[i][j] == '@')                    {                        x = i, y = j;                        vis[x][y] = true;                        break;                    }        }        dfs(x, y);        printf("%d\n",res+1);    }    return 0;}
0 0