利用bfs走迷宮是最基礎的方法,記錄訪問過的每個點然後回溯最短路徑的點
首先把問題簡單化一下,只有牆壁和路,代碼比較簡單,只要會bfs基本上看得懂
// 走迷宮記錄路徑
// 概述:有一個N*M大小的迷宮,其中'#'代表牆壁無法通過,'.'代表路,每走一步都將花費1s的時間,求從起點(0,0)到終點(N-1,M-1)的最短路徑,並將路徑打印
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 1000005;
const int MAX_N = 105;
struct Point
{
int x;
int y;
}first, next,pre[MAX_N][MAX_N];
char pic[MAX_N][MAX_N];
int N, M;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int value[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int bfs()
{
queue<Point> que;
que.push(first);
while(que.size())
{
first = que.front();
que.pop();
if(first.x == N - 1 && first.y == M -1)
return value[N - 1][M - 1];
for(int i = 0; i < 4; i++)
{
next.x = first.x + dx[i];
next.y = first.y + dy[i];
if(next.x >= 0 && next.x < N && next.y >= 0 && next.y < M && pic[next.x][next.y] != '#' && vis[next.x][next.y])
{
value[next.x][next.y] = value[first.x][first.y] + 1;
pre[next.x][next.y].x = first.x;
pre[next.x][next.y].y = first.y;
vis[next.x][next.y] = false;
que.push(next);
}
}
}
return INF;
}
void print_pre(int i, int j)
{
if(pre[i][j].x == -1)
return;
print_pre(pre[i][j].x, pre[i][j].y);
printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j], pre[i][j].x, pre[i][j].y, i, j);
}
int main()
{
while(cin >> N >> M)
{
for(int i = 0; i < N; i++)
cin >> pic[i];
first.x = 0;
first.y = 0;
memset(vis, true, sizeof(vis)); // true表示該點未訪問,false表示該點已訪問
vis[0][0] = false;
pre[0][0].x = pre[0][0].y = -1;
int ans = bfs();
if(ans == INF)
printf("sorry,can't out!\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
print_pre(N - 1, M - 1);
}
}
return 0;
}
hangd1026題就是這種類型的題目,只是多了一個條件,路上可能會有怪物,每個怪物有N點生命值(1<=n<=9),要花費n秒殺死這個怪物。
題目鏈接
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 100005;
const int MAX_N = 105;
struct Node
{
int x;
int y;
int num;
bool operator<(const Node &a) const
{
return num > a.num;
}
}first, nextNode;
struct Point
{
int x;
int y;
}pre[MAX_N][MAX_N];
char pic[MAX_N][MAX_N];
int value[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int N, M;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs()
{
priority_queue<Node> pque;
pque.push(first);
while(pque.size())
{
first = pque.top();
pque.pop();
if(first.x == N - 1 && first.y == M - 1)
return value[N - 1][M - 1];
for(int i = 0; i < 4; i++)
{
nextNode.x = first.x + dx[i];
nextNode.y = first.y + dy[i];
if(nextNode.x >= 0 && nextNode.x < N && nextNode.y >= 0 && nextNode.y < M && pic[nextNode.x][nextNode.y] != 'X' && vis[nextNode.x][nextNode.y])
{
if(pic[nextNode.x][nextNode.y] == '.')
{
nextNode.num = first.num + 1;
}
else if(isdigit(pic[nextNode.x][nextNode.y]))
{
nextNode.num = first.num + 1 + (int)(pic[nextNode.x][nextNode.y] - '0');
}
pre[nextNode.x][nextNode.y].x = first.x;
pre[nextNode.x][nextNode.y].y = first.y;
value[nextNode.x][nextNode.y] = nextNode.num;
vis[nextNode.x][nextNode.y] = false;
pque.push(nextNode);
}
}
}
return INF;
}
void print_pre(int i, int j)
{
if(pre[i][j].x == -1)
return;
print_pre(pre[i][j].x, pre[i][j].y);
if(pic[i][j] == '.')
printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j], pre[i][j].x, pre[i][j].y, i, j);
else
{
int t = pic[i][j] - '0';
int kase = value[pre[i][j].x][pre[i][j].y];
printf("%ds:(%d,%d)->(%d,%d)\n",++kase, pre[i][j].x, pre[i][j].y, i, j);
for(int k = 0; k < t; k++)
{
printf("%ds:FIGHT AT (%d,%d)\n", ++kase, i, j);
}
}
}
int main()
{
while(cin >> N >> M)
{
for(int i = 0; i < N; i++)
cin >> pic[i];
memset(vis, true, sizeof(vis));
first.x = 0;
first.y = 0;
first.num = 0;
value[0][0] = 0;
vis[0][0] = false;
pre[0][0].x = pre[0][0].y = -1;
int ans = bfs();
if(ans != INF)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
print_pre(N - 1, M - 1);
}
else
printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return 0;
}