hdu-2579

  • 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579

  • 题目大意: 给一个迷宫,迷宫中的障碍物在以k的倍数这么多部的时候会消失,求到达终点的时间

  • 思路: 因为k最大为10,那么多开一维来进行标记即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using namespace std;
const int kMaxN = 100;
struct {
int x, y;
int step;
Point(int a, int b, int s = 0) : x(a), y(b), step(s){}
};
int kT, kN, kM;
char kMap[kMaxN][kMaxN];
int kVis[kMaxN][kMaxN][10];
int kSX, kSY, kEX, kEY, kK;
const int kDx[4] = {0, 1, -1, 0};
const int kDy[4] = {1, 0, 0, -1};
void Input(){
for(int i = 0; i < kN; ++i){
scanf("%s", kMap[i]);
for(int j = 0; j < kM; ++j){
if(kMap[i][j] == 'Y'){
kSX = i, kSY = j;
}
if(kMap[i][j] == 'G'){
kEX = i, kEY = j;
}
}
}
}
bool Ok(int nx, int ny, int step){
if(nx >= 0 && nx < kN && ny >= 0 && ny < kM && !kVis[nx][ny][step%kK]){
return true;
}
return false;
}
void Bfs(){
Point p(kSX, kSY);
queue<Point>que;
que.push(p);
memset(kVis, false, sizeof(kVis));
kVis[kSX][kSY][0] = true;
while(!que.empty()){
Point top = que.front();
if(top.x == kEX && top.y == kEY){
cout<<top.step<<endl;
return;
}
que.pop();
for(int i = 0; i < 4; ++i){
int nx = top.x + kDx[i];
int ny = top.y + kDy[i];
int step = top.step + 1;
if(Ok(nx, ny, step)){
if(kMap[nx][ny] == '#' && step % kK == 0 ){
kVis[nx][ny][0] = true;
}
else if(kMap[nx][ny] == '#' && step % kK != 0){
continue;
}
else {
kVis[nx][ny][step % kK] = true;
}
que.push(Point(nx, ny, step));
}
}
}
puts("Please give me another chance!");
}
int main(){
cin>>kT;
while(kT--){
cin>>kN>>kM>>kK;
Input();
Bfs();
}
return 0;
}