题目链接:https://www.luogu.org/problem/show?pid=1649
历经千辛万苦,我总算是把这个水题AC了,现在心里总觉得一万只草泥马在奔腾;
这是一道很明显的BFS,然后我也明显的看出来了
但是,我就是WA了很久很久,在调试第一个晚上后,我发现读入是存在空格的,而不是数据问题
然后第二个问题就是,BFS找到的第一个终点不一定就是最优的答案(当然第二个问题是我重新打了一次猛然发现之前没注意的一点)
注意到这两点,其实这道题就没难度了
然后这道题的处理需要注意一个地方就是转弯,有两种方式来处理这个转弯
一:开结构体数组,记录当前点前驱的序号,然后看前驱的坐标和下一个位置的坐标是否有横纵坐标中的一个相等
二:开队列,然后用结构体,结构体里存一个direction(方向),然后定义上下左右分别对应1,2,3,4,如果当前点的方向和下一个拓展的方向相同,如果相同则拓展的方向没转弯
当然如果用数组的话会有一点的麻烦,用队列的话代码难度小一点而且更方便不会超时,我之前用数组也是超过时的,所以还是建议用队列
PS:接下来这个代码在洛谷上是可以过的,因为数据较水
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cmath>
6 #include<cstdlib>
7 #include<queue>
8 #include<string>
9 #define maxn 105
10 using namespace std;
11
12 const int dx[]={0,-1,1,0,0};
13 const int dy[]={0,0,0,-1,1};
14
15 struct node{
16 int x,y,s,d;//坐标,步数,方向
17 };
18
19 queue<node>q;
20 int n,map[maxn][maxn],vis[maxn][maxn];
21 int sx,sy,fx,fy,ans=10002;
22
23 void init()
24 {
25 scanf("%d",&n);
26 for(int i=1;i<=n;i++)
27 for(int j=1;j<=n;j++)
28 {
29 char a;
30 scanf("%c",&a);
31 while(a!='A'&&a!='B'&&a!='x'&&a!='.')
32 {
33 scanf("%c",&a);
34 }
35 if(a=='A'){
36 sx=i;sy=j;
37 }
38 if(a=='B'){
39 fx=i;fy=j;
40 }
41 if(a=='x'){
42 map[i][j]=1;
43 }
44 }
45 }
46
47 int main()
48 {
49 init();
50 vis[sx][sy]=1;
51 q.push((node){sx,sy,0,0});
52 while(!q.empty())
53 {
54 node e=q.front();
55 q.pop();
56 vis[e.x][e.y]=1;
57 for(int i=1;i<=4;i++)
58 {
59 int zx=e.x+dx[i],zy=e.y+dy[i];
60 if(zx<1||zx>n||zy<1||zy>n)continue;
61 if(map[zx][zy]==0&&vis[zx][zy]==0){
62 if(i==e.d||e.d==0){
63 q.push((node){zx,zy,e.s,i});
64 }else {
65 q.push((node){zx,zy,e.s+1,i});
66 }
67 }
68 }
69 if(e.x==fx&&e.y==fy){
70 ans=min(ans,e.s);
71 }
72 }
73 if(ans<=10000)printf("%d",ans);
74 else printf("-1");
75 }
但是仔细斟酌,如果是100*100的图,而没有障碍,那么普通的队列是会爆的,这个可以自己尝试
所有改成优先队列做
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cmath>
6 #include<cstdlib>
7 #include<queue>
8 #include<string>
9 #define maxn 105
10 using namespace std;
11
12 const int dx[]={0,-1,1,0,0};
13 const int dy[]={0,0,0,-1,1};
14
15 struct node{
16 int x,y,s,d;//坐标,步数,方向
17 bool operator< (const node &a)const{
18 return s>a.s;}
19 };
20
21 priority_queue<node>q;
22 int n,map[maxn][maxn],vis[maxn][maxn];
23 int sx,sy,fx,fy,ans=10002;
24
25 void init()
26 {
27 scanf("%d",&n);
28 for(int i=1;i<=n;i++)
29 for(int j=1;j<=n;j++)
30 {
31 char a;
32 scanf(" %c",&a);
33 while(a!='A'&&a!='B'&&a!='x'&&a!='.')
34 {
35 scanf("%c",&a);
36 }
37 if(a=='A'){
38 sx=i;sy=j;
39 }
40 if(a=='B'){
41 fx=i;fy=j;
42 }
43 if(a=='x'){
44 map[i][j]=1;
45 }
46 }
47 }
48
49 int main()
50 {
51 init();
52 vis[sx][sy]=1;
53 q.push((node){sx,sy,0,0});
54 while(!q.empty())
55 {
56 node e=q.top();
57 q.pop();
58 vis[e.x][e.y]=1;
59 for(int i=1;i<=4;i++)
60 {
61 int zx=e.x+dx[i],zy=e.y+dy[i];
62 if(zx<1||zx>n||zy<1||zy>n)continue;
63 if(map[zx][zy]==0&&vis[zx][zy]==0){
64 if(i==e.d||e.d==0){
65 q.push((node){zx,zy,e.s,i});
66 }else {
67 q.push((node){zx,zy,e.s+1,i});
68 }
69 }
70 }
71 if(e.x==fx&&e.y==fy){
72 //if(e.s>=ans)
73 ans=min(ans,e.s);break;
74
75 }
76 }
77 if(ans<=10000)printf("%d",ans);
78 else printf("-1");
79 }
这个代码的区别就是优先队列,和跳出语句,其中跳出的时候要注意特例,可能会存在到终点不用转弯的线上的点的转弯次数相同但是来源到终点却有转弯和不转弯的情况,,所以要特判一个当现在的值会大于等于ans就跳出
特例
。。。。A
。。。。。
。B 。。。
到B左边的点时,转弯一次,到b右边的点时,转弯一次,但是左边点会多转一次,所以要特判一下