这题太恶心了。。。。。不对。。。是我弱暴了。。。。。我一直以为炸了墙之后墙还有的来着。。。好吧我蛋疼了。。。。
AC代码如下:
// hdu 2128 mnlm 1.0
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include <cctype>
#include<vector>
#include<algorithm>
using namespace std;
#include<queue>
struct NODE
{
int x;
int y;
int b;
int t;
int mb[8][8]; //保存每个位置的最小炸弹数
};
queue <NODE> q;
int xy[4][2] =
{
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
int N;
int M;
int startx, starty; //S的位置
char map[10][10];
int ans; //最小的时间,初始值为-1
void BFS()
{
ans = -1;
while (!q.empty())
{
q.pop();
}
NODE m;
NODE tt;
m.x = startx;
m.y = starty;
m.b = 0;
m.t = 0;
memset(m.mb, -1, sizeof(m.mb));
m.mb[m.x][m.y] = 0;
q.push(m);
while (!q.empty())
{
m = q.front();
q.pop();
if (ans != -1 &&
m.t + 1 >= ans)
{
continue;
}
int i;
for (i = 0; i < 4; i++)
{
tt.x = m.x + xy[i][0];
tt.y = m.y + xy[i][1];
tt.b = -1;
if (tt.x >= 0 && tt.x < N &&
tt.y >= 0 && tt.y < M)
{
if (map[tt.x][tt.y] == 'D')
{
if (ans == -1 ||
m.t + 1 < ans)
{
ans = m.t + 1;
}
continue;
}
if (m.mb[tt.x][tt.y] > -1 ||
map[tt.x][tt.y] == '.') //已经走过的炸弹和WALL都没有了,相当于'.'
{
tt.t = m.t + 1;
tt.b = m.b;
}
else if (map[tt.x][tt.y] == 'X' &&
m.b > 0)
{
tt.t = m.t + 2;
tt.b = m.b - 1;
}
else if (isdigit(map[tt.x][tt.y]))
{
tt.t = m.t + 1;
tt.b = m.b + map[tt.x][tt.y] - '0';
}
if (tt.b > m.mb[tt.x][tt.y])
{
memcpy(tt.mb, m.mb, 64 * sizeof(int));
tt.mb[tt.x][tt.y] = tt.b;
q.push(tt);
}
}
}
}
return;
}
int main()
{
while (1)
{
scanf("%d%d", &N, &M);
if (!N && !M)
{
break;
}
int i;
int j;
for (i = 0; i < N; i++)
{
scanf("%s", map[i]);
for (j = 0; j < M; j++)
{
if (map[i][j] == 'S')
{
startx = i;
starty = j;
}
}
}
BFS();
printf("%d\n", ans);
}
}