#include<iostream>
#include<queue>
using namespace std;
#define _for(i, a, b) for(int i = (a); i < (b); i ++ )
#define _rep(i, a, b) for(int i = (a); i <= (b); i ++ )
struct state
{
int x, y, hp, nut_hp, t;
state() {};
state(int a, int b, int c, int d, int e) : x(a), y(b), hp(c), nut_hp(d), t(e) {}
}in, out;
const int N = 210;
int g[N][N];
bool st[N][N][15][2];
int m, n, b, nt;
int bfs()
{
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
queue<state> q;
q.push(in);
while(q.size())
{
state t = q.front();
q.pop();
if(st[t.x][t.y][t.hp][t.nut_hp]) continue;
st[t.x][t.y][t.hp][t.nut_hp] = 1;
if(t.x == out.x && t.y == out.y)
return t.t;
_for(i, 0, 4)
{
int x = t.x + dx[i], y = t.y + dy[i];
if(x > 0 && x <= m && y > 0 && y <= n)
{
if(g[x][y] == 1) continue;
else if(!g[x][y]) q.push(state(x, y, t.hp, t.nut_hp, t.t + 1));
else if(g[x][y] == 3)
{
if(t.hp > 1)
q.push(state(x, y, t.hp - 1, t.nut_hp, t.t + 1));
}
else
{
if(!t.nut_hp)
q.push(state(x, y, t.hp, 0, t.t + 1));
else
q.push(state(x, y, t.hp, 0, t.t + 1 + nt));
}
}
}
}
return -1;
}
int main()
{
cin >> m >> n >> b >> nt;
_rep(i, 1, m)
_rep(j, 1, n)
{
char c;
cin >> c;
if(c == '#') g[i][j] = 1;//墙1
else if(c == '*') g[i][j] = 0;//通路0
else if(c == '!') in = state(i, j, b, 1, 0);
else if(c == '+') out = state(i, j, 0, 0, 0);
else if(c == 'o') g[i][j] = 2;//坚果2
else g[i][j] = 3;//地刺3
}
cout << bfs() << endl;
return 0;
}
西工大算法设计与分析 noj1654 僵尸双来了(实验三)
最新推荐文章于 2022-12-06 10:15:00 发布
该博客介绍了一个使用宽度优先搜索(BFS)算法解决机器人在带有障碍物和特殊元素地图上寻路的问题。代码实现中,机器人可以从起点出发,通过空地、避开墙壁,消耗生命值通过地刺,利用坚果恢复生命,最终到达终点。博客内容涵盖了路径搜索、状态转移矩阵和队列的应用。
摘要由CSDN通过智能技术生成