Topic links
Give a n*m figure with n markings on it, n<=4, then a wall, and an end X. Each step, you can only go to a certain mark, to go in four directions, and then must touch the wall or the boundary of the diagram or another mark to stop. Ask if you can get the first label to the finish line within T-step.
Because there is an upper limit of T. So directly BFS can, feel the idea is not difficult, but write code + debugging spent super long ... But finally 1 A, otherwise will be confused forced.
#include <bits/stdc++.h>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<11#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD =7777777;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };structnode{Vector<pll>A; intstep; Node () {Step=0; }};intCNT, W, H;map<vector<pll>,int>Mp;vector<int> row[ One], col[ One];p ll target;Chars[ One][ One];intBFsConstnode& Init,intt) {Queue<node>Q; Q.push (init); while(!Q.empty ()) {Node TMP=Q.front (); Q.pop (); if(Tmp.step >t)Continue; if(tmp.a[0] ==target) { returnTmp.step; } for(inti =0; I < CNT; i++) { intx = tmp.a[i].fi, y =tmp.a[i].se; intTmpy = * (--Lower_bound (Row[x].begin (), Row[x].end (), y)); for(intj =0; J < CNT; J + +) { if(i = =j)Continue; if(tmp.a[i].fi = = tmp.a[j].fi && tmp.a[i].se >tmp.a[j].se) {Tmpy=Max (Tmpy, tmp.a[j].se); }} tmpy++; Vector<pll>ha (TMP.A); ha[i].se=Tmpy; if(!Mp[ha]) {Node NewNode=tmp; newnode.a[i].se=Tmpy; Newnode.step++; Q.push (NewNode); Mp[ha]=1; } x= tmp.a[i].fi, y =tmp.a[i].se; Tmpy= *Lower_bound (Row[x].begin (), Row[x].end (), y); for(intj =0; J < CNT; J + +) { if(i = =j)Continue; if(tmp.a[i].fi = = tmp.a[j].fi && tmp.a[i].se <tmp.a[j].se) {Tmpy=min (tmpy, tmp.a[j].se); }} tmpy--; Ha=Tmp.a; ha[i].se=Tmpy; if(!Mp[ha]) {Node NewNode=tmp; newnode.a[i].se=Tmpy; Newnode.step++; Mp[ha]=1; Q.push (NewNode); } x= tmp.a[i].fi, y =tmp.a[i].se; intTMPX = * (--Lower_bound (Col[y].begin (), Col[y].end (), x)); for(intj =0; J < CNT; J + +) { if(i = =j)Continue; if(tmp.a[i].se = = tmp.a[j].se && tmp.a[i].fi >tmp.a[j].fi) {tmpx=Max (tmpx, tmp.a[j].fi); }} tmpx++; Ha=Tmp.a; ha[i].fi=tmpx; if(!Mp[ha]) {Node NewNode=tmp; newnode.a[i].fi=tmpx; Newnode.step++; Q.push (NewNode); Mp[ha]=1; } x= tmp.a[i].fi, y =tmp.a[i].se; TMPX= *Lower_bound (Col[y].begin (), Col[y].end (), x); for(intj =0; J < CNT; J + +) { if(i = =j)Continue; if(tmp.a[i].se = = tmp.a[j].se && tmp.a[i].fi <tmp.a[j].fi) {tmpx=min (tmpx, tmp.a[j].fi); }} tmpx--; Ha=Tmp.a; ha[i].fi=tmpx; if(!Mp[ha]) {Node NewNode=tmp; newnode.a[i].fi=tmpx; Newnode.step++; Q.push (NewNode); Mp[ha]=1; } } } return-1;}intMain () {intT; while(cin>>cnt>>w>>h>>t) {mp.clear (); for(inti =0; I < H; i++) {scanf ("%s", S[i]); } for(inti =0; I < H; i++) {row[i].clear (); ROW[I].PB (-1); ROW[I].PB (w); } for(inti =0; i < W; i++) {col[i].clear (); COL[I].PB (-1); COL[I].PB (h); } node init; for(inti =0; I < H; i++) { for(intj =0; J < W; J + +) { if(S[i][j] = ='X') Target=Mk (I, j); if(S[i][j] = ='W') {ROW[I].PB (j); COL[J].PB (i); } } } for(inti =1; I <= CNT; i++) { for(intj =0; J < H; J + +) { for(intK =0; K < W; k++) { if(s[j][k]-'0'==i) {INIT.A.PB (Mk (j, K)); } } } } for(inti =0; I < H; i++) {sort (Row[i].begin (), Row[i].end ()); } for(inti =0; i < W; i++) sort (Col[i].begin (), Col[i].end ()); MP[INIT.A]=1; intAns =BFS (init, T); if(ans = =-1) {puts ("NO Solution"); } Else{cout<<ans<<Endl; } }}
Uvalive 6888 Ricochet Robots BFS