最近WS因差1分钟没能AK而深受打击,自(bi)暴(guan)自(xiu)弃(lian),沉迷英雄(shua)联盟(ti)无法自拔,有天WS突发奇想,准备开发一款英雄联盟版的迷宫游戏,游戏规则如下:
英雄盖伦出生在迷宫的某处,盖伦可往前后左右四个方向走动,每走一步用时为1(也可停止不动),盖伦带有技能闪现,使用闪现可以穿过1个厚度的墙(耗时为1,穿完墙落地开始闪现进入CD),闪现技能CD为p,迷宫中随机分布0-3个蓝爸爸,经过蓝爸爸时可以选择打或者不打,击杀蓝爸爸消耗1单位时间并可获得蓝buff(蓝爸爸不会复活),蓝buff持续时间为q,buff持续时间内使用闪现技能CD减半(向下取整,如果我击杀蓝buff时候技能还处于cd状态cd不会减半),多个蓝buff时间可叠加。请写出一个程序求出盖伦走出迷宫所需的最小时间。
ps:buff剩一秒时穿墙后闪现CD按原CD计算。
输入包含多个测试下样例,每个样例的第一行包含4个整数m,n,p,q。分别表示迷宫的长,宽,闪现CD,每个蓝buff持续时间,1<=m,n<=20,1<=p,q<=10。接下来的m行为迷宫分布,#表示墙,.表示路,@表示蓝爸爸,S表示英雄出生点,E表示迷宫出口。
(测试数据保证至少有一条路可以通向出口)
输出一个数字表示盖伦走出迷宫所需的最小时间
1 10 1 5 S.#..#..#E 1 10 2 5 S.#..#..#E 1 10 2 5 S@#..#..#E
6 8 7
思路:主要是不好保存状态,可以用五维数组,分别保存坐标,cd,buff时间,以及记录哪些蓝buff是打过的,最不好保存的是哪些蓝buff打过,由于蓝buff只有打过和没打过两种状态,可以考虑用二进制来保存,每个二进制位的1或0表示该蓝buff是否打过。注意一种特殊情况:S.#@#.#.#.#.#E,我到达蓝buff时,可以先等待几秒,等到CD快没了的时候再打蓝buff。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int maxn = 25;
char mat[maxn][maxn];
bool vis[25][25][12][12][8];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int m,n,p,q;
struct node
{
int x,y,cd,buff,mark,time;
}st,ed;
int check(node a)
{
if (a.x < 0 || a.x >= n || a.y < 0 || a.y >= m) return 0;
return 1;
}
int bfs()
{
memset(vis,false,sizeof(vis));
queue <node> Q;
st.cd = st.buff = st.mark = st.time = 0;
Q.push(st);
vis[st.x][st.y][st.cd][st.buff][st.mark] = true;
node now,next;
while (!Q.empty())
{
now = Q.front();
Q.pop();
if (now.x == ed.x && now.y == ed.y) return now.time;
next = now;
if (mat[now.x][now.y] >= 0 && mat[now.x][now.y] <= 2)//判断是否为蓝buff
{
if ((now.mark & (1 << mat[now.x][now.y])) == 0)//判断该蓝buff是否打过
{
next.mark += 1 << mat[now.x][now.y];//将该蓝buff标记为打过
next.time++;
next.cd = next.cd > 0 ? next.cd - 1 : 0;
next.buff = next.buff > 0 ? next.buff - 1 : 0;
next.buff += q;
if (!vis[next.x][next.y][next.cd][next.buff][next.mark])
{
Q.push(next);
vis[next.x][next.y][next.cd][next.buff][next.mark] = true;
}
}
}
next = now;
if (now.cd > 0 || now.buff > 0)//前面提到的原地等待CD的情况,这里还考虑了
{ //原地消耗蓝buff时间的情况(暂时不知道是否有用)
next.cd = next.cd > 0 ? next.cd - 1 : 0;
next.buff = next.buff > 0 ? next.buff - 1 : 0;
next.time++;
if (!vis[next.x][next.y][next.cd][next.buff][next.mark])
{
Q.push(next);
vis[next.x][next.y][next.cd][next.buff][next.mark] = true;
}
}
for (int i = 0; i < 4; i++)
{
next = now;
next.x += dir[i][0];
next.y += dir[i][1];
if (!check(next)) continue;
if (mat[next.x][next.y] == '#')//使用闪现穿墙
{
if (next.cd > 0) continue;
next.x += dir[i][0];
next.y += dir[i][1];
if (!check(next) || mat[next.x][next.y] == '#') continue;
if (next.buff > 1) next.cd = p/2;//有buff则CD时间减半
else next.cd = p;
next.buff = next.buff > 0 ? next.buff - 1 : 0;
next.time++;
if (!vis[next.x][next.y][next.cd][next.buff][next.mark])
{
Q.push(next);
vis[next.x][next.y][next.cd][next.buff][next.mark] = true;
}
}
else
{
next.cd = next.cd > 0 ? next.cd - 1 : 0;
next.buff = next.buff > 0 ? next.buff - 1 : 0;
next.time++;
if (!vis[next.x][next.y][next.cd][next.buff][next.mark])
{
Q.push(next);
vis[next.x][next.y][next.cd][next.buff][next.mark] = true;
}
}
}
}
return 0;
}
int main()
{
while (scanf("%d%d%d%d",&n,&m,&p,&q) != EOF)
{
for (int i = 0; i < n; i++)
scanf("%s",mat[i]);
int id = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mat[i][j] == 'S')
{
st.x = i;
st.y = j;
}
if (mat[i][j] == 'E')
{
ed.x = i;
ed.y = j;
}
if (mat[i][j] == '@')
{
mat[i][j] = id++;//为每个蓝buff编号
}
}
}
printf("%d\n",bfs());
}
return 0;
}