poj 1979 Red and Black

题目链接http://poj.org/problem?id=1979

题意:.可以走 #不可以走 @当前位置 求可以到达的位置

分类:DFS

代码:红色部分容易出错

///#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<string.h>

using namespace std;

#define LL __int64

#define PI 3.1415926535898

const LL inf=1000000007;

int n,m;

char ch[30][30];

bool hang[30];
bool lie[30];
bool visit[30][30];

int hx[4]={1,0,-1,0};
int hy[4]={0,1,0,-1};

int temp_x,temp_y;
int st_x,st_y;
int en_x,en_y;

LL ans;

void dfs(int x,int y)
{
    for(int i=0;i<=3;i++)
    {
        temp_x=x+hx[i];
        temp_y=y+hy[i];
        if(temp_x<1||temp_x>n||temp_y<1||temp_y>m)
            continue;
        if(ch[temp_x][temp_y]=='#')
            continue;

        if(!visit[temp_x][temp_y])
        {
            //ans++;
            visit[temp_x][temp_y]=1;

            dfs(temp_x,temp_y);

            //visit[temp_x][temp_y]=0;
        }

    }
    return ;
}

int main()
{
    while(scanf("%d %d",&m,&n)&&(n+m))
    {
        memset(visit,0,sizeof(visit));

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>ch[i][j];
                if(ch[i][j]=='@')
                {
                    st_x=i;
                    st_y=j;
                }
            }
        }

        ans=0;
        visit[st_x][st_y]=1;
        dfs(st_x,st_y);

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(visit[i][j]) ans++;
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}