Problem Description Three Kingdoms is a funny game. Often Liu Bei is weak and has to run away,so in the game Liu Bei has a skill called "Dunzou". This time you are playing the role of Liu Bei.As Cao Cao's power is so strong, there is nothing you can do but escaping. Please select an optimal path to achieve the purpose .
‘A’ : Representative of watchtower, which has an attack range of 2(measured by Manhattan distance),and an attack damage of 1.
‘B’ : Representative of Fort, which has an attack range of 3(measured by Manhattan distance),and an attack damage of 2.
‘C’ : Representative of Flame, which has an attack damage of 3 to those who step onto it.
‘D’ : Representative of Archer, which has anattack range of 2(measured by Manhattan distance), and an attack damage of 4.
‘E’ : Representative of Ordinary soldier, which has anattack range of 1(measured by Manhattan distance), and an attack damage of 5.
‘$’ : Representative of Liu Bei.
‘!’ : Representative of Destination.
'#' : Representative of obstacles
‘.’ : Representative of floor.
Liu Bei can not enter watchtower, forts, Archers, ordinary soldiers,But he can step onto flame or floor.
Some important rules you should note:
1. Liu Bei will not be hurt by the same thing twice.For example,if Liu Bei has been hurt by one watchtower before,then he step into the attack range of some watchtower later,he will not be hurt.
2. When Liu Bei reach the destination,you should first judge the attack damage at the destination then finish the game.
3. You needn’t judge the attack damage at the start position.
Please choose a path which LiuBei would cost the least HP.
Input In the first line there is an integer T, indicates the number of test cases.(T<=60)
In each case,the first line of the input contains two integer n,m(1<=n,m<=50),reperesenting the size of map(n*m).Then follow n lines,each line contain m characters.
There may be some blank lines between each case.
Output For each test case , output “Case d: “ where d is the case number counted from one.If Liu Bei can reach the destination then output the minimum HP LiuBei may lose, otherwise output -1.
Sample Input 1 4 3 .$. ACB ACB .!.
Sample Output Case 1: 6 其实最终的情况非常少,状态压缩一下bfs即可
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i = j; i <= k; i++)
const int N = 1e2 + 10;
char s[N][N];
int n, m, T, ex, ey, bx, by, cas = 0;
int mp[N][N], f[N][N][1 << 5], g[N][N];
int d[4][2] = { 1,0,-1,0,0,1,0,-1 };
int r[5] = { 2,3,0,2,1 };
int h[1 << 5];
struct point
{
int x, y, z;
point(int x = 0, int y = 0, int z = 0) :x(x), y(y), z(z) {};
};
int bfs()
{
queue<point> p;
p.push(point(bx, by, 0));
f[bx][by][0] = 0;
while (!p.empty())
{
point q = p.front(); p.pop();
rep(i, 0, 3)
{
int x = q.x + d[i][0], y = q.y + d[i][1], z = q.z | g[x][y];
if (!mp[x][y]) continue;
if (f[x][y][z] == -1)
{
f[x][y][z] = 0;
p.push(point(x, y, z));
}
}
}
int res = -1;
rep(i, 0, 31)
{
if (res == -1 && !f[ex][ey][i]) res=h[i];
else res = f[ex][ey][i] == -1 ? res : min(res, h[i]);
}
return res;
}
int main()
{
rep(i, 0, 31)
{
h[i] = 0;
rep(j, 0, 4) if (i&(1 << j)) h[i] += j + 1;
}
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
memset(mp, 0, sizeof(mp));
memset(f, -1, sizeof(f));
memset(g, 0, sizeof(g));
rep(i, 1, n)
{
scanf("%s", s[i] + 1);
rep(j, 1, m)
{
if (s[i][j] == '!') s[i][j] = '.', ex = i, ey = j;
if (s[i][j] == '$') bx = i, by = j;
mp[i][j] = s[i][j] == 'C' || s[i][j] == '.';
}
}
rep(i, 1, n) rep(j, 1, m)
{
if (!mp[i][j]) continue;
rep(k, 0, 4)
{
rep(ii, i - r[k], i + r[k]) rep(jj, j + abs(ii - i) - r[k], j + r[k] - abs(ii - i))
if (ii > 0 && ii <= n&&jj > 0 && jj <= m&&s[ii][jj] == 'A' + k) g[i][j] |= 1 << k;
}
}
printf("Case %d: %d\n", ++cas, bfs());
}
return 0;
}
标签:HDU,Bei,res,Three,Kingdoms,damage,Liu,attack,Representative From: https://blog.51cto.com/u_15870896/5838889