样例1我输出19(乐) 大佬们帮蒟蒻看看吧(悲) (和@Flandre_495 大犇的题解明明很像甚至直接仔细校对过了还是不行(撅望)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
struct attribute {
bool flo, sword;
int dis, x, y;
};
bool zi[4];
char map[1001][1001];
int n, m, ans, dis[1001][1001][3], sx, sy, fx, fy;
std::priority_queue <attribute> queue;
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
inline bool operator < (attribute x, attribute y) {
return x.dis > y.dis;
}
inline int getNB(attribute x) {
if(x.flo)
return 1;
if(x.sword)
return 2;
return 0;
}
inline void check(attribute x) { //check the value of nb
int nb = getNB(x);
if(x.dis >= dis[x.x][x.y][nb])
return;
dis[x.x][x.y][nb] = x.dis;
queue.push(x);
}
void jianXi(attribute x) {
int nb = getNB(x);
if(zi[nb])
return;
zi[nb] = 1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
if(map[i][j] != 'X')
continue;
attribute y = (attribute){x.flo, x.sword, x.dis + 1, i, j};
check(y);
}
}
int bfs() {
memset(dis, 114514, sizeof(dis));
queue.push((attribute){0, 0, 0, sx, sy});
while(!queue.empty()) {
attribute x = queue.top();
queue.pop();
if(x.x == fx && x.y == fy)
return x.dis;
for(int k = 0; k < 4; k++) {
attribute y;
y.flo = x.flo, y.sword = x.sword, y.x = x.x + dx[k], y.y = x.y + dy[k];
int nb = getNB(y);
if(y.x < 0 || y.y < 0 || y.x > n || y.y > m)
continue;
char judge = map[y.x][y.y];
switch(judge) {
case '1':
y.dis = x.dis + 1;
if(nb == 2)
check(y);
break;
case '0':
y.dis = x.dis + 1;
check(y);
break;
case 'X':
y.dis = x.dis + 1;
check(y);
break;
case '2':
if(nb != 0)
y.dis = x.dis + 1;
else
y.dis = x.dis + 4;
check(y);
break;
case '3':
if(nb != 0)
y.dis = x.dis + 1;
else
y.dis = x.dis + 9;
check(y);
break;
case '4':
y.dis = x.dis + 1, y.flo = 1;
check(y);
break;
case '5':
y.dis = x.dis + 1;
check(y);
if(!y.sword)
y.sword = 1, y.dis += 5;
check(y);
break;
}
}
if(map[x.x][x.y] == 'X')
jianXi(x);
}
return -1;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%s", map[i] + 1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
switch(map[i][j]) {
case 'M':
map[i][j] = '0';
break;
case 'S':
sx = i, sy = j, map[i][j] = '0';
break;
case 'E':
fx = i, fy = j, map[i][j] = '0';
}
ans = bfs();
if(ans != -1)
printf("%d\n", ans);
else
printf("We want to live in the TouHou World forever\n");
return 0;
}