Portal: Https://uva.onlinejudge.org/external/16/1600.pdf
Multi-State wide search
Online puzzle: To the VIS array plus one-dimensional state, indicating the number of times left to traverse the wall, each encounter wall, the current K minus 1, hit 0, the current K into the original K.
Vis[x][y][z] x, y for coordinates z means K when True indicates that the point of the remaining number of times the wall is K has already appeared
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefstructnode{5 intx, y, step, K;6 } node;7 Const intMAXN = -;8 Const intINF =0x3f3f3f3f;9 Const intCx[] = {-1,1,0,0};Ten Const intCy[] = {0,0, -1,1}; One intN, M, K, ans; A intMAZE[MAXN][MAXN]; - intVIS[MAXN][MAXN][MAXN]; - the voidBFs () { -Queue<node>Q; - while(!q.empty ()) Q.pop (); - node cur; +cur.x = Cur.y =1; -Cur.step =0; +CUR.K =K; A Q.push (cur); atVIS[CUR.X][CUR.Y][CUR.K] =1; - while(!Q.empty ()) { -Cur =Q.front (); - Q.pop (); - intx = cur.x, y =cur.y; - if(x = = N && y = =m) { inAns =Cur.step; - return ; to } + node now; - if(CUR.K >=0){ the for(inti =0; I <4; ++i) { *now.x = x + cx[i], now.y = y +Cy[i]; $Now.step = Cur.step +1;Panax NotoginsengNOW.K = Maze[now.x][now.y]? CUR.K-1: K; - if(Now.x <1|| N < now.x | | Now.y <1|| M < NOW.Y | | VIS[NOW.X][NOW.Y][NOW.K])Continue; the if(NOW.K >=0){ +VIS[NOW.X][NOW.Y][NOW.K] =1; A Q.push (now); the } + } - } $ } $ } - - intMain () { the intT; -CIN >>T;Wuyi while(t--){ theCIN >> n >> M >>K; -memset (Vis,0,sizeof(Vis)); WuMemset (Maze,0,sizeof(Maze)); - for(inti =1; I <= N; ++i) About for(intj =1; J <= M; ++j) $CIN >>Maze[i][j]; -Ans =-1; - BFS (); -cout << ans <<Endl; A } + return 0; the}
UVa Patrol Robot (exercise 6-5)