HDU1010


又是因为不认真找了好久的错误;

要用到DFS奇偶剪枝



#include <iostream>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


using namespace std;


char map[14][14];
bool vis[14][14];
bool falg = false;
int N,M,T,stx,sty,endx,endy;
int move[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};


void dfs( int x, int y, int t)
{


    if(falg)
     return ;


    if(x == endx && y == endy && t == T)
    {
        falg = true;
        return ;
    }


    if(t>T)
     return ;


    if((abs(endx-x)+abs(endy-y))%2!=(T-t)%2)
        return;


   int i,j,tx,ty;
   for( int i=0 ;i<4; i++)
   {
       tx = x+move[i][0];
       ty = y+move[i][1];


       if(tx >= 0 && tx < N && ty >=0 && ty<M && !vis[tx][ty] && (map[tx][ty] == '.' || map[tx][ty] == 'D'))
       {


           vis[tx][ty] = true;
           dfs(tx,ty,t+1);
           vis[tx][ty] = false;
       }
   }
}


int main()
{
    while(scanf("%d%d%d",&N ,&M, &T)!=EOF&&M+N+T)
    {
        int num = 0;
        falg = false;
        memset(vis,false, sizeof(vis));


        for( int i =0; i<N; i++)
        {
            cin.get();
            for( int j = 0; j<M; j++)
            {
                cin>>map[i][j];


                if(map[i][j] == 'S')
                {
                    stx = i;
                    sty = j;
                }


                else if( map[i][j] == 'D')
                {
                    endx = i;
                    endy = j;
                    num++;
                }
              if(map[i][j] == '.')
                num++;


            }
        }
       //cout<<num<<endl;


    if(stx == endx && sty == endy)
      falg = true;
       if(num >= T)
        dfs(stx,sty,0);
     if(falg)
      cout<<"YES"<<endl;
     else
      cout<<"NO"<<endl;


    }
}

你可能感兴趣的:(HDU1010)