Perfect for a A * problem.
More than the usual maze problem plus a message K indicates the number of obstacles currently passing through.
#include <cstdio>#include<cstring>#include<queue>using namespacestd;Const intMAX = +;intm,n,k;intC[max][max][max];intG[max][max];inttarx,tary;structnode{intg,h; intx, y;//The greater the distance from the end of the subject x+y the closer intK; BOOL operator< (Constnode& RHS)Const{ if(g + H > rhs.g + rhs.h)return true; if(g + H < Rhs.h + RHS.G)return false; if(g > RHS.G)return true; if(G < RHS.G)return false; returnK >RHS.K; } voidh () {h= tarx-x+ tary-y; }};node start;#defineC (n) c[n.x][n.y][n.k]#defineAdd (n) C (n) = TrueintDx[] = {0,1,0,-1};intDy[] = {1,0,-1,0};intBFs () {priority_queue<node>Q; memset (C,false,sizeof(C)); Q.push (start); Add (start); Node cur,nxt; intI,nx,ny,nk; while(Q.size ()) {cur=q.top (); Q.pop (); if(cur.x = = Tarx && cur.y = = tary)returnCUR.G; for(i =0; I <4; i++) {NX= Cur.x +Dx[i]; NY= Cur.y +Dy[i]; if(NX >= m | | | NX <0|| NY >= N | | NY <0)Continue; NK= G[nx][ny]? cur.k+1:0; if(C[nx][ny][nk] | | NK > K)Continue; Nxt.x= NX; Nxt.y = NY; NXT.G = cur.g+1; NXT.K =nk, NXT. H (); Q.push (NXT); Add (NXT); } } return-1;}intMain () {//freopen ("In.txt", "R", stdin); intT; inti,j; scanf ("%d",&T); while(t--) {scanf ("%d%d%d",&m,&n,&k); for(i =0; i < m; i++) for(j =0; J < N; J + +) scanf ("%d", g[i]+j); Tarx= M1; tary = n1; Start.x= Start.y = START.G = START.K =0; //start. H ();printf"%d\n", BFS ()); } return 0;}
UVA Patrol Robert