In fact, it is added a time limit of the detonation, anyway n,m to a very small, directly recorded 3-dimensional state, then very casual.
#include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set># Include <vector> #include <string> #include <queue> #include <deque> #include <bitset># Include <list> #include <cstdlib> #include <climits> #include <cmath> #include <ctime># Include <algorithm> #include <stack> #include <sstream> #include <numeric> #include <fstream > #include <functional>using namespace std; #define MP make_pair#define PB push_backtypedef long Long ll;typedef u nsigned Long Long ull;typedef vector<int> vi;typedef pair<int,int> pii;const int INF = Int_max/3;const Doubl E eps = 1e-8;const LL linf = 1e17;const double dinf = 1e60;const int MAXN = 15;const int WALL = 0;const int EMPTY = 1;cons t int STR = 2;const int TAR = 3;const int RESET = 4;const int dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};int MP[MAXN][MAXN], st[ Maxn][maxn][7];int n,m,sx,sy,ex,ey;void BFs () {queue<int> QX,QY,QT; Qx.push (SX); Qy.push (SY); Qt.push (6); ST[SX][SY][6] = 0; while (!qx.empty ()) {int x = Qx.front (), y = Qy.front (), t = Qt.front (); int nowt = st[x][y][t]; Qx.pop (); Qy.pop (); Qt.pop (); for (int i = 0;i < 4;i++) {int NX = x + dx[i], NY = y + dy[i]; int NT = Mp[nx][ny] = = RESET? 6:t-1; if (t-1 > 0 && mp[nx][ny]! = WALL) {if (nowt + 1 < st[nx][ny][nt]) {st[n X][NY][NT] = nowt + 1; Qx.push (NX); Qy.push (NY); Qt.push (NT); }}}}}int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); Memset (Mp,0,sizeof (MP)); memset (st,0x3f,sizeof (ST)); int inf = St[0][0][0],ans = INF; for (int i = 1;i <= n;i++) {for (int j = 1;j <= m;j++) {scanf ("%d", &mp[i][j]); if (mp[i][j] = = STR) { SX = i; sy = j; } if (mp[i][j] = = TAR) {ex = i; ey = j; }}} BFS (); for (int i = 1;i < 6;i++) ans = min (ans,st[ex][ey][i]); if (ANS >= inf) puts ("-1"); else printf ("%d\n", ans); } return 0;}
HDU 1072 Nightmare BFS