4.2.1 hdu1242 rescue
A simple BFS has a bit of DP feeling in it. Because playing a guard will delay time, you cannot simply check whether it has searched a grid, if you can get to this point faster from this path, you need to update this point.
# Include <cstdio> # include <string. h ># include <queue> using namespace STD; struct POS {int X, Y, step; pos () {} pos (int A, int B) {x =, y = B ;}} P; char maze [205] [205]; int step [205] [205]; int ax, ay, RX, Ry, n, m; int Delta [2] [4] = {1,-, 0}, {, 1,-1 }}; int BFS () {queue <POS> q; while (! Q. empty () Q. pop (); memset (step,-1, sizeof step); q. push (Pos (RX, ry); Step [RX] [ry] = 0; while (! Q. empty () {pos TP = Q. front (); q. pop (); For (INT I = 0; I <4; I ++) {int Nx = TP. X + Delta [0] [I], NY = TP. Y + Delta [1] [I]; If (Nx = AX & ny = Ay) return step [TP. x] [TP. y] + 1; if (maze [NX] [NY]! = '#') {If (step [NX] [NY] =-1 | step [TP. x] [TP. y] + (maze [NX] [NY] = '. '? 2: 00) <step [NX] [NY]) {q. push (Pos (NX, NY); Step [NX] [NY] = step [TP. x] [TP. y] + (maze [NX] [NY] = '. '?) ;}}} Return-1;} int main () {While (~ Scanf ("% d", & N, & M )! = EOF) {// initialize the maze and the boundary for (INT I = 1; I <= N; I ++) {scanf ("% s ", maze [I] + 1); For (Int J = 1; j <= m; j ++) {If (maze [I] [J] = 'A ') ax = I, ay = J; else if (maze [I] [J] = 'R') RX = I, Ry = J ;}} for (INT I = 0; I <= m + 1; I ++) maze [0] [I] = maze [n + 1] [I] = '#'; for (INT I = 0; I <= n + 1; I ++) maze [I] [0] = maze [I] [M + 1] = '#'; int r = BFS (); If (r =-1) printf ("Poor Angel has to stay in the prison all his life. \ n "); else printf (" % d \ n ", R) ;}return 0 ;}
4.2.2 hdu1548 a strange lift
4.2.3 hdu1372 knight moves
Two BFs with no skills ..
4.2.4 escape from the maze with hdu1728
Every time you follow the new line, you cannot follow the new line. Note: You have not accessed the new point, or the number of turns to this point is less than the previous decision.
# Include <cstdio> # include <string. h ># include <queue> using namespace STD; int cas, n, m, C1, C2, R1, R2, K; char maze [205] [205]; int dir [205] [205]; struct POS {int R, C; pos (int x, int y) {r = x, c = y ;}}; int Dr [] = {1,-, 0}; int DC [] = {, 1,-1}; bool BFS () {If (R2 = R1 & C2 = C1) return true; queue <POS> q; memset (Dir,-1, sizeof DIR); q. push (Pos (R1, C1); dir [R1] [C1] = 0; while (! Q. empty () {pos TP = Q. front (); q. pop (); For (INT I = 0; I <4; I ++) {int Nr = TP. R + Dr [I], NC = TP. c + DC [I]; // follow the new line each time until the new line cannot be followed. Note: (the new point has not been accessed, or the number of times to this point is less than the previous decision) while (NR> = 1 & NC> = 1 & Nr <= M & NC <= N & maze [Nr] [Nc]! = '*' & (Dir [Nr] [Nc] =-1 | dir [TP. r] [TP. c] + 1 <= dir [Nr] [Nc]) {dir [Nr] [Nc] = dir [TP. r] [TP. c] + 1; q. push (Pos (NR, NC); If (dir [R2] [C2]! =-1 & dir [R2] [C2] <= k + 1) return true; NR + = Dr [I]; nC + = Dc [I] ;}} if (dir [R2] [C2] <= k + 1 & dir [R2] [C2]! =-1) return true; return false;} int main () {scanf ("% d", & CAS); While (CAS --) {scanf ("% d", & M, & N); For (INT I = 1; I <= m; I ++) scanf ("% s ", maze [I] + 1); scanf ("% d", & K, & C1, & R1, & C2, & R2 ); printf (BFS ()? "Yes \ n": "No \ n");} return 0 ;}
4.2.5 hdu1252 hike on a graph
It is really hard to understand, that is, A can be up to X only when a-X is the same color as B-C.
Understanding is a simple BFS... mark the positions of three balls with a three-dimensional vis array.
# Include <cstdio> # include <string. h ># include <queue> using namespace STD; struct POS {int x, y, z; pos (int A, int B, int c) {x =, y = B, Z = C ;}}; int N, P1, P2, P3; char map [60] [60]; int vis [60] [60] [60];/* three BFs points, x-> I only applies when color [x] [I] = color [y] [Z] */INT BFS () {memset (VIS, 0, sizeof vis ); queue <POS> q; q. push (Pos (P1, P2, P3); vis [P1] [P2] [P3] = 1; while (! Q. empty () {pos P = Q. front (); q. pop (); If (P. X = P. Y & P. y = P. z) return vis [p. x] [p. y] [p. z]-1; for (INT I = 1; I <= N; I ++) {If (Map [p. x] [p. y] = map [p. z] [I] &! Vis [p. x] [p. y] [I]) {vis [p. x] [p. y] [I] = vis [p. x] [p. y] [p. z] + 1; q. push (Pos (P. x, p. y, I);} If (Map [p. z] [p. x] = map [p. y] [I] &! Vis [p. x] [I] [p. z]) {vis [p. x] [I] [p. z] = vis [p. x] [p. y] [p. z] + 1; q. push (Pos (P. x, I, p. z);} If (Map [p. y] [p. z] = map [p. x] [I] &! Vis [I] [p. y] [p. z]) {vis [I] [p. y] [p. z] = vis [p. x] [p. y] [p. z] + 1; q. push (Pos (I, p. y, P. z); }}return-1;} int main () {While (scanf ("% d", & N), n) {scanf ("% d", & P1, & p2, & P3); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) scanf ("% C", & map [I] [J]); int r = BFS (); if (r =-1) printf ("impossible \ n"); else printf ("% d \ n", R) ;}return 0 ;}
4.3.6 hdu2612 find a way
Find a KFC to minimize the distance between two people and KFC. First, consider it from KFC... TLE.
In another way, we can find the shortest distance from the two people to all the locations on the map, and then enumerate the KFC distance ..
4.3.7 hdu2653 waiting ten thousand years for love
It took a long time to figure out the meaning of this question. For '@', it is necessary to fly up and then fly away. the energy consumed by a '@' is actually two points, for '. 'There is no rule for flying and walking. You can fly up and down and so on ..
The Code has been knocked on for a long time. It may be because of unclear ideas .. in fact, two cases are taken into account at each point .. flight is preferred, because it can quickly reach the devil, as long as the energy is greater than 0, it is possible to fly, as long as you are about to walk out and the location is not '@', you can walk .. in addition, because there may be different energy values at a point, a 3D array is used to mark the state.
#include <cstdio>#include <string.h>#include <queue>using namespace std;struct pos{int r,c,p,s;pos(int w,int x,int y,int z){r=w,c=x,p=y,s=z;} };int n,m,p,t,sr,sc,er,ec;bool vis[82][82][82];int dr[]={0,0,1,-1},dc[]={1,-1,0,0};char maze[82][82];int bfs(){queue<pos> q;memset(vis,0,sizeof vis);q.push(pos(sr,sc,p,0));vis[sr][sc][p]=true;while(!q.empty()){pos tp=q.front();q.pop();//reach in timeif(tp.r==er&&tp.c==ec&&tp.s<=t)return tp.sfor(int i=0;i<4;i++){int nr=tp.r+dr[i],nc=tp.c+dc[i];if(nr<1||nr>n||nc<1||nc>m||maze[nr][nc]=='#')continue;//FLY if(tp.p>0&&!vis[nr][nc][tp.p-1]){q.push(pos(nr,nc,tp.p-1,tp.s+1));vis[nr][nc][tp.p-1]=true;}//WALKif(maze[tp.r][tp.c]=='@'||maze[nr][nc]=='@')continue;//CAN`T WALKif(!vis[nr][nc][tp.p]){q.push(pos(nr,nc,tp.p,tp.s+2));vis[nr][nc][tp.p]=true;}}}return -1; }int main(){int cas=1;while(scanf("%d%d%d%d",&n,&m,&t,&p)!=EOF){for(int i=1;i<=n;i++){scanf("%s",maze[i]+1);for(int j=1;j<=m;j++){if(maze[i][j]=='Y')sr=i,sc=j;if(maze[i][j]=='L')er=i,ec=j;}}int r=bfs();printf("Case %d:\n",cas++);if(r==-1)printf("Poor Yifenfei, he has to wait another ten thousand years.\n");else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",r);}return 0;}
4.2.8 hdu2531 catch him
Compare the water. Use the coordinates in the upper left corner of the defender as the reference coordinate and use this point to access the mark. Each time you make a judgment, you must determine all the parts of the Defender's body .. wa at the beginning .. I found a half-day error. Later I found that it was used to determine whether I had met the defender or caught the defender in a loop .. 2. The last time I met the defender, I may have met the defender .. this should not be captured ..
# Include <cstdio> # include <string. h ># include <queue> using namespace STD; struct POS {int R, C; pos (int A, int B) {r = A, c = B ;}}; char maze [120] [120]; int n, m, STR, STC, FS, FSD [25] [2], step [120] [120]; int Dr [] = {1,-1, 0}, DC [] = {0, 0, 1,-1}; int moveres (int nr, int NC) {If (step [Nr] [Nc]! =-1) return 0; // has been moved for (INT I = 0; I <FS; I ++) {int r = nR-str + FSD [I] [0], c = NC-STC + FSD [I] [1]; if (r <1 | C <1 | r> N | C> m) return 0; // If (maze [r] [c] = 'O') if (maze [r] [c] = 'O') return 0; // The defender} // determine the value separately, first, judge whether the for (INT I = 0; I <FS; I ++) {int r = nR-str + FSD [I] [0], c = NC-STC + FSD [I] [1]; if (maze [r] [c] = 'q') return 2; // catch the callback} return 1; // you can move} int BFS (INT Sr, int SC) {If (Sr = 0) Return-1; memset (step,-1, sizeof step); queue <POS> q; q. push (Pos (Sr, S C); Step [SR] [SC] = 0; while (! Q. empty () {pos TP = Q. front (); q. pop (); For (INT I = 0; I <4; I ++) {int Nr = TP. R + Dr [I], NC = TP. c + DC [I]; int r = moveres (NR, NC); // The result of moving. 0 cannot be, 1 can be moved, 2 can be caught by the sub-guard if (r = 2) return step [TP. r] [TP. c] + 1; if (r = 0) continue; step [Nr] [Nc] = step [TP. r] [TP. c] + 1; q. push (Pos (NR, NC) ;}} return-1 ;}int main () {While (scanf ("% d", & N, & M ), N | M) {STR = 0, STC = 0; // coordinate FS = 0 in the upper left corner of the defender; // The volume of the Defender for (INT I = 1; I <= N; I ++) {scanf ("% s", maze [I] + 1); For (Int J = 1; j <= m; j ++) {If (maze [I] [J] = 'D') {FSD [fs] [0] = I, FSD [fs] [1] = J, FS ++; If (STR = 0) STR = I, STC = J ;}} int r = BFS (STR, STC); If (r =-1) printf ("impossible \ n"); else printf ("% d \ n", R);} return 0 ;}