-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDOJ1495非常可乐(BFS).cpp
136 lines (136 loc) · 2.19 KB
/
HDOJ1495非常可乐(BFS).cpp
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
130
131
132
133
134
135
136
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef struct Node
{
int x,y,z,step;
} node;
int vis[105][105],n,m,s,mini;
void bfs(void)
{
queue<node>QAQ;
node start;
start.x=0,start.y=0,start.z=s,start.step=0;
QAQ.push(start);
while(!QAQ.empty()){
node now=QAQ.front();
QAQ.pop();
if(now.x==0&&now.y==now.z||now.y==0&&now.x==now.z){
mini=now.step;
return;
}//ťĽĎྚˎ,˛ťÂúžÍľš
node nex=now;
if(now.y<m){//x->y
nex=now;
nex.y+=nex.x;
if(nex.y>m){
nex.x=nex.y-m;
nex.y=m;
}
else{
nex.x=0;
}
nex.step++;
if(!vis[nex.x][nex.y]){
vis[nex.x][nex.y]=1;
QAQ.push(nex);
}
}
if(now.z<s){//x->z
nex=now;
nex.z+=nex.x;
if(nex.z>s){
nex.x=nex.z-s;
nex.z=s;
}
else{
nex.x=0;
}
nex.step++;
if(!vis[nex.x][nex.y]){
vis[nex.x][nex.y]=1;
QAQ.push(nex);
}
}
if(now.x<n){//y->x
nex=now;
nex.x+=nex.y;
if(nex.x>n){
nex.y=nex.x-n;
nex.x=n;
}
else{
nex.y=0;
}
nex.step++;
if(!vis[nex.x][nex.y]){
vis[nex.x][nex.y]=1;
QAQ.push(nex);
}
}
if(now.z<s){//y->z
nex=now;
nex.z+=nex.y;
if(nex.z>s){
nex.y=nex.z-s;
nex.z=s;
}
else{
nex.y=0;
}
nex.step++;
if(!vis[nex.x][nex.y]){
vis[nex.x][nex.y]=1;
QAQ.push(nex);
}
}
if(now.x<n){//z->x
nex=now;
nex.x+=nex.z;
if(nex.x>n){
nex.z=nex.x-n;
nex.x=n;
}
else{
nex.z=0;
}
nex.step++;
if(!vis[nex.x][nex.y]){
vis[nex.x][nex.y]=1;
QAQ.push(nex);
}
}
if(now.y<m){//z->y
nex=now;
nex.y+=nex.z;
if(nex.y>m){
nex.z=nex.y-m;
nex.y=m;
}
else{
nex.z=0;
}
nex.step++;
if(!vis[nex.x][nex.y]){
vis[nex.x][nex.y]=1;
QAQ.push(nex);
}
}
}
return;
}
int main()
{
while(cin>>s>>n>>m&&n&&m&&s){
mini=0;
memset(vis,0,sizeof(vis));
bfs();
if(mini)
cout<<mini<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}