///////////////////////////////////////////////////////////////////////////////////////////////////////
tt2767
Disclaimer: This document is subject to the following agreement free reprint-non-commercial-non-derivative-retention Attribution | Creative Commons by-nc-nd 3.0
To view the update and discussion of this article, please click here: http://blog.csdn.net/tt2767
Link is deleted please Baidu: CSDN tt2767
///////////////////////////////////////////////////////////////////////////////////////////////////////
Exercises
Build a three-dimensional array d[n][n][n], the first two dimensions is the coordinates, the third dimension is the blood bar, it means that when the blood bar is HP, whether it passes this. Use the structure to record the number of steps, the state output can be judged.
#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>using namespace STD;Const Long DoublePI =ACOs(0.0) *2.0;Const intN = -;Const intDx[] = {0,1,0,-1};Const intDy[] = {-1,0,1,0};intN,m,k;structp{intx,y,hp,cnt;//hp is a blood bar, each pass barrier-1, CNT is the current number of stepsPintx =0,inty =0,intHP =0,intCNT =0): x (x), Y (y), hp (ph), CNT (CNT) {}};intD[n][n][n];//The ph value of the third dimension at the same time as this coordinateintG[n][n];intBFS ();intMain () {intcase;scanf("%d", &case);memset(g,-1,sizeof(G));//Remember to reset the map while(case--) {scanf("%d%d%d", &m,&n,&k);//k is the maximum value of the blood bar for(inti =0; I <m; i++) {GetChar ();//absorb ' \ n ' for(intj =0; J < N; J + +)scanf("%d", &g[i][j]); }printf("%d\n", BFS ()); }return 0;}intBFS () {memset(d,0,sizeof(d)); P S (0,0K0), G (M-1, N-1K0); d[0][0][K] =1; queue<P>Q Q.push (s); while(!q.empty ()) {p p = q.front (); Q.pop ();if(p.x = = g.x &&p.y = = g.y)returnp.cnt;//Reach the end of return steps if(P.HP >=0)//have blood to keep walking{P tmp; for(inti =0; i<4; i++) {tmp.x = p.x + dx[i]; TMP.Y = p.y + dy[i]; TMP.HP = G[tmp.x][tmp.y]? P.HP-1K//Met 0 o'clock full blood recoverytmp.cnt = p.cnt +1;if(0<= tmp.x && tmp.x < M &&0<= tmp.y && tmp.y <= n && d[tmp.x][tmp.y][tmp.hp] = =0&& tmp.hp >=0) {D[TMP.X][TMP.Y][TMP.HP] =1; Q.push (TMP); } } } }if(Q.empty ())return-1;//did not reach the end point}
Copyright NOTICE: This article for Bo Master original article, allow non-commercial reprint, reprint must note name author (CSDN tt2767) and this blog link: http://blog.csdn.net/tt2767.
uva1660_ patrol Robot (state BFS)