我是靠谱客的博主 热心冬天,这篇文章主要介绍Abbott's Revenge UVA - 816(带方向bfs),现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <vector> using namespace std; const int MAXN=11; struct node{ int r,c,dir; node(){} node(int _r,int _c,int _dir){r=_r,c=_c,dir=_dir;} }; int r0,c0,r2,c2; const char* Dir="NESW"; const char* Turn="FLR"; int t_dir(char t) { return strchr(Dir,t)-Dir; } int t_trun(char t) { return strchr(Turn,t)-Turn; } int mx[4]={-1,0,1,0}; int my[4]={0,1,0,-1}; node st; int has_edg[MAXN][MAXN][5][5]; node f[MAXN][MAXN][5]; int d[MAXN][MAXN][5]; bool read() { char str[111],str1[111]; if(scanf("%s%d%d%s%d%d",str,&r0,&c0,str1,&r2,&c2)!=6) return false; printf("%sn",str); int dir1=t_dir(str1[0]); st.r=r0+mx[dir1],st.c=c0+my[dir1],st.dir=dir1; memset(has_edg,0,sizeof(has_edg)); while(1) { int a,b; scanf("%d",&a); if(a==0) break; scanf("%d",&b); char str2[111]; while(scanf("%s",str2)==1) { if(str2[0]=='*') break; for(int i=1;i<strlen(str2);i++) has_edg[a][b][t_dir(str2[0])][t_trun(str2[i])]=1; } } return true; } node walk(node& t,int i) { int dir=t.dir; if(i==1) dir=(dir-1+4)%4; if(i==2) dir=(dir+1)%4; return node(t.r+mx[dir],t.c+my[dir],dir); } void pf(node& t) { vector<node> vt; vt.push_back(t); while(1) { if(t.r==st.r&&t.c==st.c&&t.dir==st.dir) { vt.push_back(node(r0,c0,0)); break; } t=f[t.r][t.c][t.dir]; vt.push_back(t); } int cnt = 0; for(int i = vt.size()-1; i >= 0; i--) { if(cnt % 10 == 0) printf(" "); printf(" (%d,%d)", vt[i].r, vt[i].c); if(++cnt % 10 == 0) printf("n"); } if(vt.size() % 10 != 0) printf("n"); } void BFS() { queue<node> qq; qq.push(st); memset(d,0,sizeof(d)); d[st.r][st.c][st.dir]=1; f[st.r][st.c][st.dir]=st; while(!qq.empty()) { node t=qq.front(); if(t.r==r2&&t.c==c2) {pf(t);return;} qq.pop(); for(int i=0;i<3;i++) { node v=walk(t,i); if(has_edg[t.r][t.c][t.dir][i]&&v.r>=1&&v.r<=9&&v.c>=1&&v.c<=9&&!d[v.r][v.c][v.dir]) { d[v.r][v.c][v.dir]=1; f[v.r][v.c][v.dir]=t; qq.push(v); } } } printf(" No Solution Possiblen"); } int main() { while(read()) { BFS(); } return 0; }

 

最后

以上就是热心冬天最近收集整理的关于Abbott's Revenge UVA - 816(带方向bfs)的全部内容,更多相关Abbott's内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部