#include <stdio.h>#include<string.h>#include<queue>using namespacestd;intSX, SY, ex =3, EY =4;intdir[8][2] = {1,1,1, -1, -1, -1, -1,1,0, -1,1,0, -1,0,0,1};Charx1, y1, x2, y2;structnode{intx; inty; intpx; intpy; intD; intstep; intvisit;}; Node susake[ $][ $];intmap[ $][ $], sum;Charpath[ $][ -];voidBFsintAintb) {memset (Susake,0,sizeof(Susake)); Queue<Node>Q; Node cur; susake[sx][sy].x=A; Susake[sx][sy].y=b; SUSAKE[SX][SY].PX=A; susake[sx][sy].py=b; Susake[sx][sy].step=0; Susake[sx][sy].visit=1; Q.push (Susake[sx][sy]); if(SX = = Ex && sy = =ey) {printf ("%d\n", sum); return ; } while(!Q.empty ()) {cur=Q.front (); Q.pop (); if(cur.x = = Ex && cur.y = =ey) {Sum=Cur.step; printf ("%d\n", sum); inttx = susake[ex][ey].px, Ty =susake[ex][ey].py; inti =0; memset (Path,0,sizeof(path)); if(TX < ex && Ty < ey) strcpy (path[i++],"RD"); if(tx = = ex && ty < ey) strcpy (path[i++],"R"); if(TX < ex && ty = = ey) strcpy (path[i++],"D"); if(tx = = ex && ty > Ey) strcpy (path[i++],"L"); if(Tx > Ex && ty = = ey) strcpy (path[i++],"U"); if(TX < ex && ty > Ey) strcpy (path[i++],"LD"); if(Tx > Ex && ty > Ey) strcpy (path[i++],"LU"); if(Tx > Ex && ty < ey) strcpy (path[i++],"RU"); //printf ("%d%d%d%d\n", TX, Ty, ex, EY);ex = TX; EY =Ty; while(tx! = SX | | | Ty! =Sy) {TX=susake[tx][ty].px; Ty=susake[tx][ty].py; //printf ("%d%d%d%d\n", TX, Ty, ex, EY); if(TX < ex && Ty < ey) strcpy (path[i++],"RD"); if(tx = = ex && ty < ey) strcpy (path[i++],"R"); if(TX < ex && ty = = ey) strcpy (path[i++],"D"); if(tx = = ex && ty > Ey) strcpy (path[i++],"L"); if(Tx > Ex && ty = = ey) strcpy (path[i++],"U"); if(TX < ex && ty > Ey) strcpy (path[i++],"LD"); if(Tx > Ex && ty > Ey) strcpy (path[i++],"LU"); if(Tx > Ex && ty < ey) strcpy (path[i++],"RU"); //printf ("%d%d%d%d\n", TX, Ty, ex, EY);ex = TX; EY =Ty; } for(intj = i-1; J >=0; j--) printf ("%s\n", Path[j]); Break; } for(inti =0; I <8; i++) { inttx = cur.x + dir[i][0]; intty = cur.y + dir[i][1]; if(Susake[tx][ty].visit = =0&&Map[tx][ty]) {susake[tx][ty].x=TX; Susake[tx][ty].y=Ty; Susake[tx][ty].step= Cur.step +1; Susake[tx][ty].visit=1; //Save PREVIOUSSUSAKE[TX][TY].PX =cur.x; susake[tx][ty].py=Cur.y; SUSAKE[TX][TY].D=i; Q.push (Susake[tx][ty]); } } }}intMainintargcChar*argv[]) {memset (map,0,sizeof(map)); for(inti =1; I <=8; i++) for(intj =1; J <=8; J + +) Map[i][j] =1; scanf ("%c%c%*c%c%c", &x1, &y1, &X2, &y2); Sum=0; SX= $-Y1; SY = x1- the; Ex= $-Y2; EY = x2- the; //printf ("%d%d%d%d\n", SX, SY, EX, EY);BFS (SX, SY); return 0;}
"Breadth-First search"