題意:給定一個迷宮n*m(1<=n,m<=100)韵卤,求從(0,0)到(n-1,m-1)的最短時間,并且輸出最短時間的路徑沈条。其中數(shù)字‘num',表示在這個格子中有怪物蜡歹,需要消耗num(1<=num<=9)秒的時間去打敗它,才能繼續(xù)前進(jìn)。并且走一步需要消耗1秒的時間澈魄。(’X'表示不能通過這個點,‘,'表示可以通過)
You may assume that the start position and the target position will never be a 'X', and there will never be a monster at the start position.
Sample Input
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
思路:用廣度優(yōu)先搜索+優(yōu)先隊列就可以完成铛漓,難就難在記錄路徑。開一個數(shù)組浓恶,記錄每一步的方向就可以了。輸出的時候包晰,用遞歸還原路徑就可以了。
也是看了題解才懂的伐憾,感覺學(xué)到了很多。
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_N = 110;
int map[MAX_N][MAX_N];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};//上树肃、下瀑罗、左胸嘴、右
int path[MAX_N][MAX_N];//記錄路徑方向斩祭。0、1停忿、2、3分別表示上下左右
int cnt[MAX_N][MAX_N];//相當(dāng)于map的備份
struct node {
int x;
int y;
int time;
node(int x, int y, int time) {//構(gòu)造方法
this->x = x;
this->y = y;
this->time = time;
}
};
//比較方法
struct nodecmp {
bool operator()(const node &a, const node &b) {
return a.time > b.time;
}
};
int bfs(int n, int m) {
memset(path, -1, sizeof(path));
priority_queue<node, vector<node>, nodecmp> pq;
pq.push(node(0, 0, 0) );
map[0][0] = -1;
while (pq.size()) {
node tmp = pq.top();
pq.pop();
if (tmp.x == n - 1 && tmp.y == m - 1) return tmp.time;
for (int i = 0; i < 4; ++i) {
int nx = tmp.x + dir[i][0];
int ny = tmp.y + dir[i][1];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != -1) {
int t = tmp.time + map[nx][ny] + 1;
pq.push(node(nx, ny, t) );
path[nx][ny] = i;//記錄方向
map[nx][ny] = -1;//標(biāo)記為已經(jīng)訪問過
}
}
}
return 0;
}
int tt;
void print(int a, int b) {
int nx = a - dir[path[a][b]][0];
int ny = b - dir[path[a][b]][1];
if (a == 0 && b == 0) return ;
print(nx, ny);
printf("%ds:(%d,%d)->(%d,%d)\n", ++tt, nx, ny, a, b);
while (cnt[a][b]-- > 0) printf("%ds:FIGHT AT (%d,%d)\n", ++tt, a, b);
}
int main () {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
char c;
scanf(" %c", &c);
if (c == 'X') map[i][j] = -1;
else if (c == '.') map[i][j] = 0;
else map[i][j] = c - '0';
cnt[i][j] = map[i][j];
}
int ans = bfs(n, m);
if (ans) {
tt = 0;
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
print(n - 1, m - 1);
}
else printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return 0;
}