题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2579
Dating with girls(2)
Description
If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl‘s location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.
Input
The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c $(1 \leq r , c \leq 100)$, and $k \ (2 \leq k \leq 10).$
The next r line is the map’s description.
Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
Sample Input
1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.
Sample Output
7
bfs。。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 #include<set> 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 using std::find; 14 using std::sort; 15 using std::pair; 16 using std::queue; 17 using std::vector; 18 #define pb(e) push_back(e) 19 #define sz(c) (int)(c).size() 20 #define mp(a, b) make_pair(a, b) 21 #define all(c) (c).begin(), (c).end() 22 #define iter(c) decltype((c).begin()) 23 #define cls(arr,val) memset(arr,val,sizeof(arr)) 24 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 25 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 27 const int N = 110; 28 const int INF = ~0u >> 1; 29 typedef unsigned long long ull; 30 char maze[N][N]; 31 bool vis[N][N][11]; 32 int r, c, k, Sx, Sy, Dx, Dy; 33 const int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 }; 34 struct Node { 35 int x, y, s; 36 Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {} 37 }; 38 queue<Node> que; 39 void bfs() { 40 cls(vis, 0); 41 while (!que.empty()) que.pop(); 42 que.push(Node(Sx, Sy, 0)); 43 vis[Sx][Sy][0] = 1; 44 while (!que.empty()) { 45 Node tp = que.front(); que.pop(); 46 if (tp.x == Dx && tp.y == Dy) { printf("%d\n", tp.s); return; } 47 rep(i, 4) { 48 int nx = dx[i] + tp.x, ny = dy[i] + tp.y, ns = tp.s + 1; 49 if (nx < 0 || nx >= r || ny < 0 || ny >= c || vis[nx][ny][ns % k]) continue; 50 if (maze[nx][ny] == ‘#‘ && ns % k != 0) continue; 51 vis[nx][ny][ns % k] = 1; 52 que.push(Node(nx, ny, ns)); 53 } 54 } 55 puts("Please give me another chance!"); 56 } 57 int main() { 58 #ifdef LOCAL 59 freopen("in.txt", "r", stdin); 60 freopen("out.txt", "w+", stdout); 61 #endif 62 int t; 63 scanf("%d", &t); 64 while (t--) { 65 scanf("%d %d %d", &r, &c, &k); 66 rep(i, r) { 67 scanf("%s", maze[i]); 68 rep(j, c) { 69 if (maze[i][j] == ‘Y‘) Sx = i, Sy = j; 70 else if (maze[i][j] == ‘G‘) Dx = i, Dy = j; 71 } 72 } 73 bfs(); 74 } 75 return 0; 76 }