Test instructions probably:
The robot is going from the upper-left corner (m*n) of a grid (with a range of M and n within a closed interval of 1 to 20) to the lower-right corner (M,n). Some grids in the grid are empty spaces, denoted by 0, other squares are obstacles, denoted by 1. The robot can walk in four directions at a time, but cannot traverse the K ([0,20]) barrier continuously to find the shortest path length. The starting and ending points are guaranteed to be open spaces.
Idea: With BFS search can, because can not continuously through the K barrier, so in the original vis2-dimensional array added 1 dimensions, into a 3-dimensional array, representing the number of layers (barriers) of the wall through.
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <queue>using namespace std; int N,m,k,t;int Map[25][25];bool vis[25][25][25];//adds a dimension z to represent the number of layers of the skipped wall. int dx[4]= {1,0,-1,0};int dy[4]= {0,1,0,-1};struct point{int x, y; int step; int layer; Point (int x=1,int y=1,int step=0,int layer=0): X (x), Y (y), step (step), layer (layer) {}};int ans () {queue<point>q; memset (vis,0,sizeof (VIS)); Point A (1,1,0,0); Point End_point (N,M); Q.push (a); Vis[1][1][0]=true; while (! Q.empty ()) {point Now=q.front (); Q.pop (); if (NOW.X==END_POINT.X&&NOW.Y==END_POINT.Y) return now.step; for (int i=0; i<4; i++) {int x=dx[i]+now.x; int y=dy[i]+now.y; int layer=now.layer; if (Map[x][y]) layer++;//because it only moves one step at a time. And met a wall, so gaga. else layer=0; if (layer<=k&&!vis[x][y][layer]&&x>=1&&y>=1&&x<=n&&y<=m) {vis[x][y][layer]=true; Q.push (Point (X,y,now.step+1,layer)); }}} return-1;} int main () {cin>>t; while (t--) {cin>>n>>m>>k;//input rows and columns and K values! for (int i=1, i<=n; i++) for (int j=1; j<=m; j + +) cin>>map[i][j];//input map cout< ; <ans () <<endl; } return 0;}
Patrol Robot UVa1600 patrol robot