标签:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> using namespace std; const char*dirs="NESW"; const char*turns="FLR"; int r0,c0,dir,r1,c1,r2,c2,has_edge[20][20][10][6],d[20][20][10]; int dir_id(char c){ return strchr(dirs,c)-dirs; } int turn_id(char c){ return strchr(turns,c)-turns; } int dr[]={-1,0,1,0}; int dc[]={0,1,0,-1}; struct Node{ int r,c,dir; }; Node p[20][20][10]; Node walk(Node u,int turn){ int dir=u.dir; if (turn==1) dir=(dir+3)%4; if (turn==2) dir=(dir+1)%4; Node t; t.r=u.r+dr[dir];t.c=u.c+dc[dir];t.dir=dir; return t; } int init(){ char ch; memset(d,0,sizeof(d)); cin>>r0>>c0>>ch>>r2>>c2; int dir=dir_id(ch); r1=r0+dr[dir];c1=c0+dc[dir]; Node t; t.r=r1;t.c=c1;t.dir=dir; p[r1][c1][dir]=t; int x,y; while (cin>>x&&x){ cin>>y; string temp; while (cin>>temp&&temp!="*"){ dir=dir_id(temp[0]); for (int i=1;i<temp.size();i++){ int turn=turn_id(temp[i]); has_edge[x][y][dir][turn]=1; } } } } int inside(int x,int y){ return (x>0&&y>0)?1:0; } void printf_ans(Node u){ vector<Node> nodes; while (1){ nodes.push_back(u); if (d[u.r][u.c][u.dir]==0) break; u=p[u.r][u.c][u.dir]; } Node t; t.r=r0;t.c=c0;t.dir=dir; nodes.push_back(t); int cnt=0; for (int i=nodes.size()-1;i>=0;i--){ if (cnt%10==0) printf(" "); printf(" (%d,%d)",nodes[i].r,nodes[i].c); if (++cnt %10==0) printf("\n"); } if (nodes.size()%10!=0) printf("\n"); } void solve(){ queue<Node>q; memset(d,-1,sizeof(d)); Node u; u.r=r1;u.c=c1;u.dir=dir; d[u.r][u.c][u.dir]=0; q.push(u); while (!q.empty()){ Node u=q.front();q.pop(); if (u.r==r2&&u.c==c2){ printf_ans(u); return; } for (int i=0;i<3;i++){ Node v=walk(u,i); if (has_edge[u.r][u.c][u.dir][i]&&inside(v.r,v.c)&&d[v.r][v.c][v.dir]<0){ d[v.r][v.c][v.dir]=d[u.r][u.c][u.dir]+1; p[v.r][v.c][v.dir]=u; q.push(v); } } } printf("No Solution Possible\n"); } int main() { string Name; while (cin>>Name&&Name!="END"){ cout<<Name<<endl; init(); solve(); } }
WA代码,至今不知道错哪了........哪位大神若是有时间帮我看看吧,code大部分是刘汝佳第二部书上的
标签:
原文地址:http://www.cnblogs.com/bingolibing/p/3870375.html