hdu2579 Dating with girls(2)

题目的意思就是一个男生在一个迷宫里找一个女生约会,该迷宫内有由'.'所代表的空地,有由'#'代表的障碍物,但是障碍物会在时间为K的整数倍的时候时消失,然后又继续出现。

男生在迷宫里移动只有四个方向(上,下,左,右),但是每一秒男生都必须移动(由于没有弄清楚这个原因,所以之前一直wa),有时男生为了等待障碍物的消失,而会需要在同一个点可能会出现重复走的状况,这时就需要一个三维数组visit[][][]来标记所走的地方,然后其余的就是bfs就能解决了。

 1 # include <cstdio>
 2 # include <cstring>
 3 # include <cstdlib>
 4 # include <iostream>
 5 # include <queue>
 6 using namespace std;
 7 
 8 struct Point{
 9     int x, y;
10     int count;
11 };
12 
13 int n, m, t;
14 int startx, starty;
15 int step[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
16 char map[101][101];
17 int visit[101][101][11];
18 bool judge(Point p)
19 {
20     if(p.x < 0 || p.x >= n || p.y < 0 || p.y >= m)
21     return false;
22     if(map[p.x][p.y] == '#' && p.count % t != 0)
23     return false;
24     return true;
25 }
26 void BFS()
27 {
28     Point p, temp;
29     int i;
30     queue<Point> que;
31     p.x = startx;
32     p.y = starty;
33     p.count = 0;
34     visit[p.x][p.y][p.count] = 0;
35     que.push(p);
36 
37     while(!que.empty())
38     {
39         p = que.front();
40         que.pop();
41         for(i = 0; i < 4; ++i)
42         {
43             temp.x = p.x + step[i][0];
44             temp.y = p.y + step[i][1];
45             temp.count = p.count + 1;
46 
47             if(judge(temp) && visit[temp.x][temp.y][temp.count % t] > temp.count)       //visit[temp.x][temp.y][temp.count % t] > temp.count 是为了判断现在与之前这点在相同时间余数一样的情况下,该点是否可走
48             {
49                 if(map[temp.x][temp.y] == 'G')
50                 {
51                     printf("%d\n", temp.count);
52                     return ;
53                 }
54                 visit[temp.x][temp.y][temp.count % t] = temp.count;
55                 que.push(temp);
56             }
57         }
58     }
59     printf("Please give me another chance!\n");
60 }
61 int main()
62 {
63     int T;
64     //freopen("in.txt", "r", stdin);
65     scanf("%d", &T);
66     while(T--)
67     {
68         int i, j, k;
69         scanf("%d %d %d", &n, &m, &t);
70         for(i = 0; i < n; ++i)
71         {
72             scanf("%s", map[i]);
73             for(j = 0; j < m; ++j)
74             {
75                 if(map[i][j] == 'Y')
76                 {
77                     startx = i;
78                     starty = j;
79                 }
80             }
81         }
82 
83         for(i = 0; i < n; ++i)
84         {
85             for(j = 0; j < m; ++j)
86             {
87                 for(k = 0; k < 11; ++k)
88                 {
89                     visit[i][j][k] = 11111111;
90                 }
91             }
92         }
93         BFS();
94     }
95     return 0;
96 }
View Code

 

posted @ 2013-10-06 11:33  shihuai_2  阅读(210)  评论(0编辑  收藏  举报