深度优先+奇偶剪枝 杭电1010

#include<stdio.h>

#include<math.h>

char maze[8][8];

int time, m, n, wall, sx, sy, dx, dy, escape;

int dir[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};



void dfs(sx, sy, t){

    int i, temp;

    if(sx > m || sy > n || sx <= 0 || sy <= 0) return ;

    if(sx == dx && sy == dy && t == time) escape = 1;

    if(escape) return ;

    temp = time - t - abs(sx - dx) - abs(sy - dy);

    if(temp < 0 || temp & 1) return;

    //printf("%d\n", escape);

    for(i = 0; i < 4; i++){

        if(maze[sx+dir[i][0]][sy+dir[i][1]] != 'X'){

            maze[sx+dir[i][0]][sy+dir[i][1]] = 'X';

            dfs(sx+dir[i][0], sy+dir[i][1], t + 1);

            maze[sx+dir[i][0]][sy+dir[i][1]] = '.';

        }

    }



    

}



int main(){

    int i, j;

    while(1){

        wall = 0;

        scanf("%d%d%d", &m, &n , &time);

        if(0 == m && 0 == m && 0 == time) break;

        getchar();

        for(i = 1; i <= m; i++){

            

            for(j = 1; j <= n; j++){

                scanf("%c", &maze[i][j]);

                if('S' == maze[i][j]){

                    sx = i;

                    sy = j;

                }else if('D' == maze[i][j]){

                    dx = i;

                    dy = j;

                }else if('X' == maze[i][j]){

                    wall ++;

                }

                

            }//for(j)

            getchar();

        }//for(i)





        escape = 0;

        maze[sx][sy] = 'X';

        dfs(sx, sy, 0);

        if(escape == 1) printf("YES\n");

        else printf("NO\n");

    }



    

    return 0;

}

 

你可能感兴趣的:(杭电)