杭电1010(dfs + 奇偶剪枝)

题目:

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.
 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES
 
思想:
奇偶剪枝先,然后dfs,详见代码注释。
 
代码:
 1 #include<stdio.h>

 2 #include<string.h>

 3 

 4 int t, flag, startx, starty, endx, endy, cout, visit[8][8], xx[4] = {0, -1, 0, 1}, yy[4] = {-1, 0, 1, 0};

 5 char a[7][7];

 6 

 7 

 8 

 9 int dfs(){

10     int i;

11     visit[startx][starty] = 0;

12     if(cout == t){

13         if(startx == endx && starty == endy){

14             printf("YES\n");

15             flag = 0;            

16         }

17         return 0;

18     }

19     for(i = 0; i < 4 && flag; i ++){

20         startx += xx[i];

21         starty += yy[i];

22         if(visit[startx][starty] == 1){

23             if(a[startx][starty] != 'X'){

24                 //visit[startx][starty] = 0;

25                 cout ++;

26                 dfs();

27                 visit[startx][starty] = 1;//不行后,都要标志成未经过,因为下一个遍历可能遍历到;

28                 cout --;

29 

30             }

31             startx -= xx[i];

32             starty -= yy[i];//这两句是当dfs返回后,将x,y变成上一个的值;

33         }

34         else{

35             startx -= xx[i];

36             starty -= yy[i];

37         }

38         

39     }

40     return 0;

41 }

42 

43 int main(){

44     int n, m, i, j;

45     while(scanf("%d %d %d", &n, &m, &t) && (n || m || t)){

46         getchar();

47         cout = 0;

48         flag = 1;

49         memset(visit, 0, sizeof(visit));

50         for(i = 1; i <= n; i ++){

51             for(j = 1; j <= m; j ++){

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

53                 visit[i][j] = 1;

54                 if(a[i][j] == 'S'){

55                     startx = i;

56                     starty = j;

57                 }

58                 else if(a[i][j] == 'D'){

59                     endx = i;

60                     endy = j;

61                 }

62             }

63             getchar();

64         }

65         //getchar();

66         if((startx + starty + endx + endy) % 2 ==0){

67             if(t % 2 != 0){

68                 printf("NO\n");

69                 continue;

70             }

71         }

72         else{

73             if(t % 2 == 0){

74                 printf("NO\n");

75                 continue;

76             }

77         }

78         dfs();

79         if(flag == 1){

80             printf("NO\n");

81         }

82     }

83     return 0;

84 }
奋进

 

 

你可能感兴趣的:(DFS)