码迷,mamicode.com
首页 > 其他好文 > 详细

zoj2110 (DFS)

时间:2015-09-18 18:23:44      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

简单的dfs应用,注意have数组的处理。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<deque>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-10;
const int INF=1000000000;
const int maxn=7+3;

int n,m,T;
char Map[maxn][maxn];
int si,sj;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int have[maxn][maxn];

bool dfs(int x,int y,int t)
{
    int tx,ty;
    have[x][y]=0;
    bool ans=false;
    for(int i=0;i<4;i++)
    {
        tx=x+dx[i];
        ty=y+dy[i];
        if(tx>=0&&tx<n&&ty>=0&&ty<m)
        {
            if(Map[tx][ty]==D&&T==t+1)
            {
                return true;
            }
            else if(Map[tx][ty]==.&&have[tx][ty]==1)
            {
                ans=dfs(tx,ty,t+1);
                if(ans) return true;
            }
        }
    }
    have[x][y]=1;
    return false;
}

int main()
{
    //freopen("in1.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%d%d",&n,&m,&T)==3)
    {
        getchar();
        if(n==0&&m==0&&T==0) break;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&Map[i][j]);
                have[i][j]=1;
                if(Map[i][j]==S)
                {
                    si=i;sj=j;//note the starting point
                }
            }
            getchar();
        }
        /*for(int i=0;i<n;i++)
        {
            puts(Map[i]);
        }*/
        //printf("%d %d\n",si,sj);
        if(dfs(si,sj,0))
        {
            puts("YES");
        }
        else puts("NO");
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

 

zoj2110 (DFS)

标签:

原文地址:http://www.cnblogs.com/zywscq/p/4819985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!