hdu 2579 Dating with girls(2)

题目连接

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 }

时间: 2024-10-29 19:11:00

hdu 2579 Dating with girls(2)的相关文章

hdu 2579 Dating with girls(2) 【经典三维BFS】

Dating with girls(2)                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2443    Accepted Submission(s): 697 链接:http://acm.hdu.edu.cn/showp

HDU 2579 Dating with girls(2) (BFS)

Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2416    Accepted Submission(s): 690 Problem Description If you have solved the problem Dating with girls(1).I think you can

HDU 2579 Dating with girls(2) BFS 余数判重

对于石头的处理就按照每个位置的时间取k的余数判一下重复就好,其他随意写 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include

HDU 2578 Dating with girls(1) [补7-26]

Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1196 Problem Description Everyone in the HDU knows that the number of boys is larger than the

hdu 2578 Dating with girls(1)

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with

【HDOJ】2579 Dating with girls(2)

简单BFS. 1 /* 2579 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 using namespace std; 8 9 #define MAXN 105 10 11 typedef struct node_t { 12 int x, y, t; 13 node_t()

hdu 2579

V - Dating with girls(2) Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2579 Appoint description:  System Crawler  (2014-11-15) Description If you have solved the problem Dating with girls(1).I

HDU 2579 (记忆化BFS搜索)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有一个关键: 每个点不是只可以走一次.最多可以走k次. 原因是对于一个点,可能是通过障碍点在k的倍数(即余数为0)步到达的,也可能是通过其它途径到达的,但是不是k的倍数(余数为1~k). 这样,判断状态是否重叠的依据就是看在(x,y)点的余数状态了.vis的第三维非常重要. #include "cst

HDU 5145 NPY and girls(莫队算法+乘法逆元)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解] 我们发现每次往里加入一个新的数字或者减去一个新的数字,前后的排列数目是可以通过乘除转移的,所以自然想到用莫队算法处理.因为答案要求取模,所以在用除法的时候要计算逆元. [代码] #include <cstdio> #include <algorithm> #include <