POJ 3083 Children of the Candy Corn bfs和dfs

 
Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8102   Accepted: 3540

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.) 

As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'. 

Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#'). 

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2

8 8

########

#......#

#.####.#

#.####.#

#.####.#

#.####.#

#...#..#

#S#E####

9 5

#########

#.#.#.#.#

S.......E

#.#.#.#.#

#########

Sample Output

37 5 5

17 17 9
 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <queue>

 4 #include <iostream>

 5 using namespace std;

 6 

 7 struct point

 8 {

 9     int x, y, step;

10 };

11 

12 struct point start, end;

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

14 char maze[50][50];

15 bool vis[50][50];

16 

17 int walk(int x, int y, int ex, int ey, int d, int num)

18 {

19     if(x == ex && y == ey)

20         return num;

21     d = (d+3) % 4;

22     while(maze[x+dir[d][0]][y+dir[d][1]] == '#')

23         d = (d+1) % 4;

24     walk(x+dir[d][0], y+dir[d][1], ex, ey, d, num+1);

25 }

26 

27 void bfs()

28 {

29     queue<struct point>q;

30     memset(vis, 0, sizeof(vis));

31     start.step = 1;

32     q.push(start);

33     while(!q.empty())

34     {

35         struct point u = q.front();

36         q.pop();

37         if(maze[u.x][u.y] == 'E')

38         {

39             printf("%d\n", u.step);

40             return;//不知道为什么改成return u.step就不对。求教。

41         }

42         if(maze[u.x-1][u.y] != '#' && !vis[u.x-1][u.y])

43         {

44             vis[u.x-1][u.y] = 1;

45             q.push((struct point){u.x-1, u.y, u.step+1});

46         }

47         if(maze[u.x+1][u.y] != '#' && !vis[u.x+1][u.y])

48         {

49             vis[u.x+1][u.y] = 1;

50             q.push((struct point){u.x+1, u.y, u.step+1});

51         }

52         if(maze[u.x][u.y-1] != '#' && !vis[u.x][u.y-1])

53         {

54             vis[u.x][u.y-1] = 1;

55             q.push((struct point){u.x, u.y-1, u.step+1});

56         }

57         if(maze[u.x][u.y+1] != '#' && !vis[u.x][u.y+1])

58         {

59             vis[u.x][u.y+1] = 1;

60             q.push((struct point){u.x, u.y+1, u.step+1});

61         }

62     }

63 }

64 

65 int main()

66 {

67     int t, n, m;

68     scanf("%d", &t);

69     while(t--)

70     {

71         memset(maze, '#', sizeof(maze));

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

73         for(int i = 1; i <= m; i++)

74         {

75             for(int j = 1; j <= n; j++)

76             {

77                 cin >> maze[i][j];

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

79                     start = (struct point){i, j};

80                 else if(maze[i][j] == 'E')

81                     end = (struct point){i, j};

82             }

83         }

84         printf("%d %d ",

85                 walk(start.x, start.y, end.x, end.y, 1, 1),

86                 walk(end.x, end.y, start.x, start.y, 1, 1)

87                 );

88         bfs();

89     }

90     return 0;

91 }
View Code

 

写了个多线程的交上果断缺少windows.h。。。

  1 #include <windows.h>

  2 #include <stdio.h>

  3 #include <queue>

  4 #include <iostream>

  5 using namespace std;

  6 

  7 struct point

  8 {

  9     int x, y, step;

 10 };

 11 

 12 struct point start, myend;

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

 14 char maze[50][50];

 15 bool vis[50][50];

 16 int bfs_ans;

 17 

 18 int walk(int x, int y, int ex, int ey, int d, int num)

 19 {

 20     if(x == ex && y == ey)

 21         return num;

 22     d = (d+3) % 4;

 23     while(maze[x+dir[d][0]][y+dir[d][1]] == '#')

 24         d = (d+1) % 4;

 25     walk(x+dir[d][0], y+dir[d][1], ex, ey, d, num+1);

 26 }

 27 

 28 void thread_bfs()

 29 {

 30     struct point tmp;

 31     queue<struct point>q;

 32     memset(vis, 0, sizeof(vis));

 33     start.step = 1;

 34     q.push(start);

 35     while(!q.empty())

 36     {

 37         struct point u = q.front();

 38         q.pop();

 39         if(maze[u.x][u.y] == 'E')

 40         {

 41             bfs_ans = u.step;

 42             return;

 43         }

 44         for(int i = 0; i < 4; i++)

 45         {

 46             if(maze[u.x+dir[i][0]][u.y+dir[i][1]] != '#' && !vis[u.x+dir[i][0]][u.y+dir[i][1]])

 47             {

 48                 vis[u.x+dir[i][0]][u.y+dir[i][1]] = 1;

 49                 tmp.x = u.x+dir[i][0];

 50                 tmp.y = u.y+dir[i][1];

 51                 tmp.step = u.step+1;

 52                 q.push(tmp);

 53             }

 54         }

 55     }

 56 }

 57 

 58 int main()

 59 {

 60     DWORD handle_id_bfs;

 61     HANDLE handle_bfs;

 62     int t, n, m;

 63     scanf("%d", &t);

 64     while(t--)

 65     {

 66         memset(maze, '#', sizeof(maze));

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

 68         for(int i = 1; i <= m; i++)

 69         {

 70             for(int j = 1; j <= n; j++)

 71             {

 72                 cin >> maze[i][j];

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

 74                 {

 75                     start.x = i;

 76                     start.y = j;

 77                 }

 78                 else if(maze[i][j] == 'E')

 79                 {

 80                     myend.x = i;

 81                     myend.y = j;

 82                 }

 83             }

 84         }

 85 

 86         handle_bfs = CreateThread(

 87             NULL,

 88             NULL,

 89             (LPTHREAD_START_ROUTINE)&thread_bfs,

 90             NULL,

 91             NULL,

 92             &handle_id_bfs

 93             );

 94 

 95         printf("%d %d ", walk(start.x, start.y, myend.x, myend.y, 1, 1),

 96         walk(myend.x, myend.y, start.x, start.y, 1, 1));

 97         WaitForSingleObject(handle_bfs, INFINITE);

 98         printf("%d\n", bfs_ans);

 99     }

100     return 0;

101 }
View Code

 

你可能感兴趣的:(children)