Main topic: The title of the maze, the conditions are as follows 6:1, the maze is represented by a two-dimensional array, 2, people can not cross the border when walking, can not walk on the wall, 3, when the exit, if the remaining time is exactly 0, the failure; Can be used several times, 6, as long as the reset device is located, the time is automatically reset to 6, wherein, the array, 0 means the wall, 1 means the channel, 2 means the initial position, 3 means the exit, 4 means the bomb reset device, the minimum number of steps needed to get out of the maze, if not before the bomb exploded, Output-1. Approximate idea: The maze problem is the classic BFS problem, first get the initial position, call the queue, make it enqueued, define the direction array, respectively (0,-1), ( -1,0), (0,1), (1,0) represent the next four directions, the current position of the four direction to determine, if you can walk through, The queue. The code is as follows: Slag code do not be intimidated to see the problem think about it to do! Don't be a wuss or a dry man!
1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <iostream>5#include <algorithm>6#include <queue>7#include <vector>8#include <Set>9#include <stack>Ten#include <string> One#include <sstream> A#include <map> -#include <cctype> - using namespacestd; the structnode - { - intX,y,time,step; - }; + inta[Ten][Ten],n,m,b[4][2]={0,-1,0,1,-1,0,1,0},mark; -Queue<node>Q; + intBFS (intXinty) A { atNode Q={x,y,6,0}; - Q.push (Q); - while(!q.empty ()) - { -Node e=Q.front (); - Q.pop (); in for(intI=0;i<4; i++) - { toq.x=e.x+b[i][0],q.y=e.y+b[i][1],q.time=e.time-1, q.step=e.step+1; + if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&q.time>0&&a[q.y][q.x]!=0) - { the if(a[q.y][q.x]==3) * returnQ.step; $ if(a[q.y][q.x]==4)Panax Notoginseng { -Q.time=6; thea[q.y][q.x]=0; + } A Q.push (Q); the } + } - } $ return-1; $ } - intMain () - { the intT,sx,sy; -scanf"%d",&t);Wuyi while(t--) the { -scanf"%d%d",&n,&m); Wu for(intI=0; i<n;i++) - { About for(intj=0; j<m;j++) $ { -scanf"%d",&a[i][j]); - if(a[i][j]==2) - { Asx=J; +sy=i; the } - } $ } the while(!q.empty ()) the Q.pop (); themark=BFS (sx,sy); theprintf"%d\n", Mark); - } in}
Nightmare---bomb time reset