POJ 3083 Children of the Candy Corn

以后建坐标系一定要以列为X轴,以行为Y轴。由于开始编码时没清晰认识到其重要性,导致调试非常麻烦。

感觉我的DFS和BFS都写的比较简洁,心里很满意。。。虽然一次就AC了,但是昨晚调到3点钟,今天中午又调试了起码一个小时。

思路还是很清楚的,就是要注意细节,编码阶段越认真后面的阶段越省事。。。

View Code
  1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 using namespace std;
5
6 int map[41][41];
7 int direction[5][2]={{0,0},{0,-1},{1,0},{0,1},{-1,0}};
8
9 #define ONLINE
10
11 void online()
12 {
13 #ifdef ONLINE
14 #else
15 freopen("F:\\t1.txt","r",stdin);
16 freopen("F:\\t2.txt","w",stdout);
17 #endif
18 }
19
20 struct Point
21 {
22 int x;
23 int y;
24 int d;
25 Point(int xx,int yy,int dd):x(xx),y(yy),d(dd)
26 {
27 }
28 Point()
29 {
30 }
31 }s,e;
32
33 int row,column;
34 Point Pqueue[1600];
35 int top,rear;
36
37 void input()
38 {
39 top=rear=0;
40 memset(map,0,sizeof(map));
41 memset(Pqueue,0,sizeof(Pqueue));
42 cin>>column>>row;
43 // cout<<column<<row;
44 for(int i=1;i<=row;++i)
45 for(int j=1;j<=column;++j)
46 {
47 char c;
48 cin>>c;
49 if(c=='#')
50 map[i][j]=1;
51 else if(c=='S')
52 {
53 s.x=j;
54 s.y=i;
55 map[i][j]=1;
56 }
57 else if(c=='E')
58 {
59 e.x=j;
60 e.y=i;
61 }
62 }
63 if(s.x==1)
64 s.d=2;
65 if(s.x==column)
66 s.d=4;
67 if(s.y==1)
68 s.d=3;
69 if(s.y==row)
70 s.d=1;
71 }
72
73
74 int dfs(Point point,int step,int control)
75 {
76 if(point.x==e.x&&point.y==e.y)
77 {
78 return step;
79 }
80 for(int i=1;i<=4;i++)
81 {
82 int nd=(point.d+5+control*i)%4+1;
83 int x=point.x+direction[nd][0];
84 int y=point.y+direction[nd][1];
85 if(map[y][x]!=1&&x>=1&&x<=column&&y>=1&&y<=row)
86 return dfs(Point(x,y,nd),step+1,control);
87 }
88 }
89
90 int bfs(Point point,int level)
91 {
92 if(point.x==e.x&&point.y==e.y)
93 return level;
94 else
95 {
96 for(int i=1;i<=4;i++)
97 {
98 int x=point.x+direction[i][0];
99 int y=point.y+direction[i][1];
100 if(map[y][x]==0&&x>=1&&x<=column&&y>=1&&y<=row)
101 {
102 map[y][x]=level;
103 Pqueue[++top]=Point(x,y,0);
104 }
105 }
106 ++rear;
107 return bfs(Pqueue[rear],map[Pqueue[rear].y][Pqueue[rear].x]+1);
108 }
109 }
110
111 int main()
112 {
113 int cases;
114 online();
115 cin>>cases;
116 while(cases--)
117 {
118 input();
119 int left=dfs(s,1,1);
120 int right=dfs(s,1,-1);
121 int shortest=bfs(s,1);
122 cout<<left<<" "<<right<<" "<<shortest<<endl;
123 }
124
125 return 0;
126 }

原文地址:https://www.cnblogs.com/YipWingTim/p/2222645.html