uva 816 BFS求最短路的经典问题……

时间:2023-03-09 03:21:19
uva 816 BFS求最短路的经典问题……

一开始情况没有考虑周全,直接WA掉了,

然后用fgets()出现了WA,给改成scanf就AC了

题目不是很难,用心就好……

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std;
const int INF = 0xffffff;
const double Pi = * atan();
const int Maxn = + ;
//int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}};
int dr[] = {-,,,};
int dc[] = {,,,-}; struct Node{
int r;
int c;
int dir;
Node(int rr,int cc,int dd){
r = rr;
c = cc;
dir = dd;
}
Node(){}
};
const char * dirs = "NESW";
const char * turns = "FLR";
bool has_edge[][][][];
int d[][][];
int r1,c1,dir;
int r0,c0;
int r2,c2;
bool flag;
Node p[][][]; int dir_id(char ch){
return strchr(dirs,ch) - dirs;
}
int turn_id(char ch){
return strchr(turns,ch) - turns;
} Node walk(Node & u,int turn){
int dd = u.dir;
if(turn == ){
dd = (dd + ) % ;
}
else if(turn == ){
dd = (dd + ) % ;
}
return Node(u.r + dr[dd],u.c + dc[dd],dd);
} void print_ans(Node u){
vector<Node>nodes;
while(){
nodes.push_back(u);
if(d[u.r][u.c][u.dir] == )
break;
u = p[u.r][u.c][u.dir];
}
nodes.push_back(Node(r0,c0,dir));
int cnt = ;
for(int i = nodes.size()-;i >= ;i--){
if(cnt % == )
cout << " ";
cout << " (" << nodes[i].r << "," << nodes[i].c << ")";
if(++cnt % == )
cout << endl;
}
if(nodes.size() % != )
cout << endl;
} bool inside(int r,int c){
if(r > && r < && c > && c < )
return true;
return false;
} void solve(){
queue<Node>q;
memset(d,-,sizeof(d));
memset(p,,sizeof(p));
Node u(r1,c1,dir);
d[u.r][u.c][u.dir] = ;
q.push(u);
while(!q.empty()){
Node u = q.front();
q.pop();
if(u.r == r2 && u.c == c2){
flag = ;
print_ans(u);
return;
}
for(int i = ;i < ;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] < ){
d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + ;
p[v.r][v.c][v.dir] = u;
q.push(v);
}
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
char str[];
while(~scanf("%s",str) && strcmp(str,"END")){
printf("%s\n",str);
flag = ;
memset(has_edge,,sizeof(has_edge));
char ch;
cin >> r0 >> c0 >> ch >> r2 >> c2;
dir = dir_id(ch);
r1 = r0 + dr[dir];
c1 = c0 + dc[dir];
int r,c;
char str2[]; while(cin >> r){
if(r == ){
break;
}
cin >> c;
while(cin >> str2){
if(str2[] == '*')
break;
int dirID = dir_id(str2[]);
int len = strlen(str2);
for(int i = ;i < len;i++){
int turnID = turn_id(str2[i]);
has_edge[r][c][dirID][turnID] = ;
}
}
}
solve();
if(flag){
cout << " No Solution Possible" << endl;
}
getchar();
}
return ;
}

测试数据:

SAMPLE
N
WL NR *
WLF NR ER *
NL ER *
SL WR NF *
SL WF ELF *
SFR EL * NOSOLUTION
N
WL NR *
NL ER *
SL WR NFR *
SR EL * MyMaze
N MyMaze
N MyMaze
N MyMaze
W
NR *
ER *
WR *
SF * MyMaze
N
WL *
NL *
SF *
NR *
SL *
EL * Circle
N
NR *
ER *
SF *
WR *
SR * Robert Abbott's Atlanta Maze
N
NR WL *
NLR WF EFR *
EFR WFR NL *
ER NL *
SFL WL NFR *
EL SFLR WFRL NFL *
EFLR SF NF WFRL *
SR ELR NF *
WR SL *
EFL SLR WR NF *
EFL SLR WL *
EL SR * END
SAMPLE
(,) (,) (,) (,) (,) (,) (,) (,) (,) (,)
(,) (,) (,) (,) (,)
NOSOLUTION
No Solution Possible
MyMaze
No Solution Possible
MyMaze
No Solution Possible
MyMaze
(,) (,)
MyMaze
(,) (,) (,) (,) (,) (,)
MyMaze
(,) (,) (,) (,) (,) (,) (,) (,)
Circle
(,) (,) (,) (,) (,) (,) (,)
Robert Abbott's Atlanta Maze
(,) (,) (,) (,) (,) (,) (,) (,) (,) (,)
(,) (,) (,) (,) (,) (,) (,) (,) (,) (,)