Hdu_1548 a strange lift
Question. BFS + priority queue. The minimum number of operations from one location to another.
Code List:
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct edge{ int x; int t; friend bool operator<(edge a,edge b){ return a.t>b.t; }};int N,A,B;int n[205];int vis[205];void bfs(){ priority_queue<edge>q; edge p,w; while(q.size()) q.pop(); p.x=A; p.t=0; q.push(p); memset(vis,0x5f,sizeof(vis)); vis[A]=0; int first=1; while(q.size()) { p=q.top(); q.pop(); if(p.x==B){ first=0; printf("%d\n",p.t); break; } w.x=p.x+n[p.x]; w.t=p.t+1; if(w.x>=1&&w.x<=N&&w.t<vis[w.x]) { vis[w.x]=w.t; q.push(w); } w.x=p.x-n[p.x]; if(w.x>=1&&w.x<=N&&w.t<vis[w.x]) { vis[w.x]=w.t; q.push(w); } } if(first) printf("-1\n");}int main(){ while(scanf("%d",&N)!=EOF) { if(N==0) break; scanf("%d%d",&A,&B); for(int i=1;i<=N;i++) scanf("%d",&n[i]); bfs(); }return 0;}
Hdu_1728 escaping from the maze
BFS does not need to use a priority queue because the question is only about whether the queue can be reached. The number of turns must be smaller than the current number of turns.
Code List:
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct edge{ int x,y; int pos,turn;};int T,m,n;int k,x1,y1,x2,y2;char s[105][105];int vis[105][105];int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};bool bfs(){ if(x1==x2&&y1==y2) return true; queue<edge>q; while(q.size()) q.pop(); edge p,w; memset(vis,0x5f,sizeof(vis)); vis[y1][x1]=-1; for(int i=0;i<4;i++){ p.x=y1+xy[i][0]; p.y=x1+xy[i][1]; p.pos=i; p.turn=0; if(p.x>=0&&p.x<m&&p.y>=0&&p.y<n&&s[p.x][p.y]!='*') { vis[p.x][p.y]=0; q.push(p); } } while(q.size()){ p=q.front(); q.pop(); if(p.x==y2&&p.y==x2) return true; for(int i=0;i<4;i++){ w.x=p.x+xy[i][0]; w.y=p.y+xy[i][1]; w.pos=i; if(w.pos!=p.pos) w.turn=p.turn+1; else w.turn=p.turn; if(w.x>=0&&w.x<m&&w.y>=0&&w.y<n&&s[w.x][w.y]!='*'&&w.turn<=vis[w.x][w.y]&&w.turn<=k){ vis[w.x][w.y]=w.turn; q.push(w); } } } return false;}int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); for(int i=0;i<m;i++) scanf("%s",s[i]); scanf("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2); x1-=1; y1-=1; x2-=1; y2-=1; if(bfs()) printf("yes\n"); else printf("no\n"); }return 0;}
Hdu_1180 weird stairway
BFS + priority queue. Note that when the stairs arrive, the status of the stairs can be determined by the current time + current orientation.
Code List:
# Include <queue> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; struct edge {int X, Y; int time; friend bool operator <(edge a, edge B) {return. time> B. time ;}}; int M, N; int Sx, Sy; int ex, ey; int vis [25] [25]; char s [25] [25]; int XY [4] [2] = {0,-1}, {-}, {}, {}; void BFS () {priority_queue <edge> q; while (Q. size () Q. pop (); edge P, W; memset (VIS, 0x5f, sizeof (VIS); P. X = SX; p. y = sy; p. time = 0; vis [SX] [sy] = 0; q. push (p); While (Q. size () {P = Q. top (); q. pop (); // cout <p. x <"" <p. Y <"" <p. time <Endl; If (P. X = ex & P. y = ey) {printf ("% d \ n", p. time); break;} For (INT I = 0; I <4; I ++) {W. X = P. X + XY [I] [0]; W. y = P. Y + XY [I] [1]; If (s [W. x] [W. y] = '|') {W. X + = xy [I] [0]; W. Y + = xy [I] [1]; if (I = 1 | I = 3) // I indicates the current orientation {If (P. time % 2 = 0) W. time = P. time + 1; else W. time = P. time + 2;} else {If (P. ti Me % 2 = 0) W. time = P. time + 2; else W. time = P. time + 1 ;}} else if (s [W. x] [W. y] = '-') {W. X + = xy [I] [0]; W. Y + = xy [I] [1]; if (I = 0 | I = 2) {If (P. time % 2 = 0) W. time = P. time + 1; else W. time = P. time + 2;} else {If (P. time % 2 = 0) W. time = P. time + 2; else W. time = P. time + 1 ;}} else W. time = P. time + 1; if (W. x> = 0 & W. x <M & W. y> = 0 & W. Y <n & S [W. x] [W. y]! = '*' & W. time <vis [W. x] [W. y]) {vis [W. x] [W. y] = W. time; q. push (w) ;}}} int main () {While (scanf ("% d", & M, & N )! = EOF) {for (INT I = 0; I <m; I ++) {scanf ("% s", s [I]); For (Int J = 0; j <n; j ++) {If (s [I] [J] = 's') {SX = I; Sy = J ;} if (s [I] [J] = 'T') {EX = I; ey = J ;}} BFS ();} return 0 ;}
Basic Search entry (2)