4-1
Test instructions is: The starting point for the 2 end is 3 bombs will explode in 6 minutes (when just out of the maze remaining time for 0 will also explode)
When encountering 4 o'clock time can be reset to 6 when walking to 4 o'clock the remaining time is 0 can not reset the time, if you can walk out of the maze within the specified time, the output of the number of steps
Otherwise output-1
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> #define MAX 10using namespace Std;int map[max][max];int vis[max][max];int n,m,t,sum;int x1,x2,y1,y2;struct node{int x,y;int step,time; friend bool Operator < (node A,node b) {return a.step>b.step;}}; void BFs () {int move[4][2]={0,1,0,-1,1,0,-1,0};int i,j;priority_queue<node>q;node beg,end;beg.x=x1;beg.y=y1; beg.step=0;beg.time=6;//is used to record how many steps Q.push (beg), Vis[x1][y1]=1;while (!q.empty ()) {end=q.top (), Q.pop (), before encountering a possible reset time (4); ; if (end.x==x2&&end.y==y2) {printf ("%d\n", end.step); return; }if (end.time==1)//Determine whether to reach the end point when one step is left (because there is no use when the 0 steps are left, even if it is possible to reset the time) continue;for (i=0;i<4;i++) {Beg.x=end.x+move I [0];beg.y=end.y+move[i][1];if (0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m& &map[beg.x][beg.y]!=0) {if (map[beg.x][beg.y]==4)//When encountering 4 {beg.time=6;//step time changed back to 6 map[beg.x][beg.y]=0;} elsebeg.time=end.time-1;//Otherwise walk one step time minus 1 Beg.step=end.step+1;q.push (beg);}}} printf (" -1\n");} int main () {int i,j,k,s;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m), for (i=0;i<n;i++) {for (j=0;j <m;j++) {scanf ("%d", &map[i][j]);}} for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (map[i][j]==2) {x1=i;y1=j;} if (map[i][j]==3) {x2=i;y2=j;}}} Sum=0;memset (vis,0,sizeof (Vis)); BFs ();} return 0;}