hdu 2579 Dating with girls(2)

时间:2024-01-20 08:10:33

题目连接

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.
hdu 2579 Dating with girls(2)

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。。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::queue;
using std::vector;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
const int INF = ~0u >> ;
typedef unsigned long long ull;
char maze[N][N];
bool vis[N][N][];
int r, c, k, Sx, Sy, Dx, Dy;
const int dx[] = { -, , , }, dy[] = { , , -, };
struct Node {
int x, y, s;
Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
};
queue<Node> que;
void bfs() {
cls(vis, );
while (!que.empty()) que.pop();
que.push(Node(Sx, Sy, ));
vis[Sx][Sy][] = ;
while (!que.empty()) {
Node tp = que.front(); que.pop();
if (tp.x == Dx && tp.y == Dy) { printf("%d\n", tp.s); return; }
rep(i, ) {
int nx = dx[i] + tp.x, ny = dy[i] + tp.y, ns = tp.s + ;
if (nx < || nx >= r || ny < || ny >= c || vis[nx][ny][ns % k]) continue;
if (maze[nx][ny] == '#' && ns % k != ) continue;
vis[nx][ny][ns % k] = ;
que.push(Node(nx, ny, ns));
}
}
puts("Please give me another chance!");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
scanf("%d %d %d", &r, &c, &k);
rep(i, r) {
scanf("%s", maze[i]);
rep(j, c) {
if (maze[i][j] == 'Y') Sx = i, Sy = j;
else if (maze[i][j] == 'G') Dx = i, Dy = j;
}
}
bfs();
}
return ;
}