通路復原 BFS
//====================================================================||
// ||
// ||
// Author : GCA ||
// 6AE7EE02212D47DAD26C32C0FE829006 ||
//====================================================================||
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
#ifdef ONLINE_JUDGE
#define ll "%lld"
#else
#define ll "%I64d"
#endif
typedef unsigned int uint;
typedef long long int Int;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 55
#define PII pair<int,int>
#define PB push_back
#define oo (1<<29)
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
int nopass[10][10][10][10];
void init(){
Set(nopass,0);
}
int bx,by,ex,ey;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int dist[10][10];
struct node{
int x,y;
node(){};
node(int x,int y):x(x),y(y){};
};
void solve(){
node back[10][10];
fill(&dist[0][0],&dist[9][9],oo);
queue<node> q;
q.push(node(bx,by));
dist[bx][by]=0;
while(!q.empty()){
int x=q.front().x;
int y=q.front().y;q.pop();
int d=dist[x][y];
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>5||nx<0||ny>5||ny<0)continue;
if(nopass[x][y][nx][ny])continue;
// debug("%d %d\n",nx,ny);
if(d+1<dist[nx][ny]){
dist[nx][ny]=d+1;
q.push(node(nx,ny));
back[nx][ny]=node(x,y);
}
}
}
char str[1000];
int k=0;
int x=ex;
int y=ey;
while(1){
node n=back[x][y];
int backx=n.x;
int backy=n.y;
// printf("%d %d %d %d\n",backx,backy,x,y);
if(x>backx)str[k++]='E';
else if(backx>x)str[k++]='W';
else if(y>backy)str[k++]='S';
else if(backy>y)str[k++]='N';
//putchar(str[k-1]);
x=backx;y=backy;
if(x==bx&&y==by)break;
}
for(int i=k-1;i>=0;i--){
putchar(str[i]);
}Pln();
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d%d",&bx,&by)&&bx+by){
init();
scanf("%d%d",&ex,&ey);
bx--;by--;ex--;ey--;
for(int i=0;i<3;i++){
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1==x2){
if(y1>y2)swap(y1,y2);
if(x1<1)continue;
for(int j=y1;j<y2;j++){
nopass[x1][j][x1-1][j]=1;
nopass[x1-1][j][x1][j]=1;
// debug("%d %d %d %d\n",x1-1,j,x1,j);
}
}else{
if(x1>x2)swap(x1,x2);
if(y1<1)continue;
for(int j=x1;j<x2;j++){
nopass[j][y1][j][y1-1]=1;
nopass[j][y1-1][j][y1]=1;
// debug("%d %d %d %d\n",j,y1,j,y1-1);
}
}
}
solve();
}
}
留言
張貼留言