POJ 3009 Curling 2.0

从昨晚11点钟开始编码,2点半睡觉,8点半起床,

编码不用1个小时,调试用了无数的时间。

总之还是一句话,编码之前方方面面考虑清楚,到后面的调试就不会用很多时间了。

继续加油,还是太浪费时间了。。。。

这道题深度优先搜索,要考虑很多细节问题。

View Code
  1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 using namespace std;
5
6 int map[40][40];
7 int weigth,height;
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 }start,goal;
25
26 int mininum;
27
28 Point stop(Point point,int direction)
29 {
30 Point temp;
31 if(direction==1)
32 {
33 while(map[--point.x][point.y]==0);
34 temp.x=point.x+1;
35 temp.y=point.y;
36 return temp;
37 }
38 else if(direction==2)
39 {
40 while(map[point.x][++point.y]==0);
41 temp.x=point.x;
42 temp.y=point.y-1;
43 return temp;
44 }
45 else if(direction==3)
46 {
47 while(map[++point.x][point.y]==0);
48 temp.x=point.x-1;
49 temp.y=point.y;
50 return temp;
51 }
52 else if(direction==4)
53 {
54 while(map[point.x][--point.y]==0);
55 temp.x=point.x;
56 temp.y=point.y+1;
57 return temp;
58 }
59 }
60
61 void dfs(Point point,int step)
62 {
63 if(step>10)
64 return ;
65 if(point.x==goal.x&&point.y==goal.y)
66 {
67 if(step<mininum)
68 mininum=step;
69 return;
70 }
71 Point temp;
72 for(int i=1;i<=4;i++)
73 {
74 temp=stop(point,i);
75 // if(point.x==0||point.x==height+1||point.y==0||point.y==weigth+1)
76 // break;
77 if(i==1)
78 {
79 if(map[temp.x-1][temp.y]==3)
80 {
81 temp.x=temp.x-1;
82 dfs(temp,step+1);
83 return ;
84 }
85 else if(temp.x==1)
86 continue;
87 else if(temp.x!=point.x)
88 {
89 map[temp.x-1][temp.y]=0;
90 dfs(temp,step+1);
91 map[temp.x-1][temp.y]=1;
92 continue;
93 }
94 }
95 if(i==2)
96 {
97 if(map[temp.x][temp.y+1]==3)
98 {
99 temp.y=temp.y+1;
100 dfs(temp,step+1);
101 return ;
102 }
103 else if(temp.y==weigth)
104 continue;
105 else if(temp.y!=point.y)
106 {
107 map[temp.x][temp.y+1]=0;
108 dfs(temp,step+1);
109 map[temp.x][temp.y+1]=1;
110 continue;
111 }
112 }
113 if(i==3)
114 {
115 if(map[temp.x+1][temp.y]==3)
116 {
117 temp.x=temp.x+1;
118 dfs(temp,step+1);
119 return ;
120 }
121 else if(temp.x==height)
122 continue;
123 else if(temp.x!=point.x)
124 {
125 map[temp.x+1][temp.y]=0;
126 dfs(temp,step+1);
127 map[temp.x+1][temp.y]=1;
128 continue;
129 }
130 }
131 if(i==4)
132 {
133 if(map[temp.x][temp.y-1]==3)
134 {
135 temp.y=temp.y-1;
136 dfs(temp,step+1);
137 return ;
138 }
139 else if(temp.y==1)
140 continue;
141 else if(temp.y!=point.y)
142 {
143 map[temp.x][temp.y-1]=0;
144 dfs(temp,step+1);
145 map[temp.x][temp.y-1]=1;
146 continue;
147 }
148 }
149 }
150 return ;
151 }
152
153
154 void init()
155 {
156 cin>>weigth>>height;
157 while(weigth!=0&&height!=0)
158 {
159 memset(map,-1,sizeof(map));
160 mininum=999999;
161 for(int i=1;i<=height;i++)
162 for(int j=1;j<=weigth;j++)
163 {
164 cin>>map[i][j];
165 if(map[i][j]==2)
166 {
167 start.x=i;
168 start.y=j;
169 map[i][j]=0;
170 }
171 if(map[i][j]==3)
172 {
173 goal.x=i;
174 goal.y=j;
175 }
176 }
177 dfs(start,0);
178 if(mininum<11)
179 cout<<mininum<<endl;
180 else
181 cout<<"-1"<<endl;
182 cin>>weigth>>height;
183 }
184
185 }
186
187 int main()
188 {
189 online();
190 init();
191 return 0;
192 }

即使这样,我觉得我所花费的时间还是值得的,

值不值得就在于以后会不会继续坚持,把这些时光所积累的宝贵经验用于实践,提高以后的效率。

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