POJ3414 Pots倒水问题
题目内容
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5822 Accepted: 2451 Special Judge
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
1. FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
2. DROP(i) empty the pot i to the drain;
3. POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Source
Northeastern Europe 2002, Western Subregion
依旧BFS(最近按照POJ训练计划做的所以BFS文章比较多……^_^)。
题目大意就是经典的倒水问题了,给你2个空瓶子,一个容量为A,另一个容量为B.
你可以进行下列3种操作:
1.把一个瓶子从水龙头装满水。
2.将一个瓶子里的水倒到另一个瓶子里(如果能倒满就必须倒满,不能洒水),可能会有剩余。
3.直接倒空一个瓶子(倒到垃圾堆^_^)。
最后输出最少步数及详细过程。
搜索时需要注意的点就是两个瓶子互倒的情况,需要分开处理倒水的瓶子有无剩余水。
我使用字符串类来存储过程信息,因此最后花费内存很大,可以用数组优化。
编译环境:Linux ubuntu 2.6.35-30-generic,g++ 4.4.5,KDevelop IDE
应用:STL queue标准队列。G++编译0MS 720K
View Code
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<string>
5 #include<queue>
6 using namespace std;
7 struct po
8 {
9 long a,b,step;
10 string mv;
11 po()
12 {
13 mv="";
14 step=0;
15 a=0;
16 b=0;
17 }
18 };
19 bool been[101][101];
20 queue<po> q;
21 long A,B,C;
22 bool check(po x)
23 {
24 if(x.a==C||x.b==C)
25 return true;
26 return false;
27 }
28 po bfs(po st)
29 {
30 //printf("Start: A:%ld B:%ld C:%ld\n",st.a,st.b,C);
31 q.push(st);
32 po now,t;
33 if(st.a==C||st.b==C)
34 return st;
35 while(!q.empty())
36 {
37 now=q.front();
38 q.pop();
39 if(now.a<A&& !been[A][now.b]) //装满a瓶
40 {
41 t=now;
42 t.mv+="F1";
43 t.a=A;
44 t.step++;
45 been[t.a][t.b]=true;
46 if(check(t))
47 return t;
48 q.push(t);
49 }
50 if(now.b<B&& !been[now.a][B]) //装满B瓶
51 {
52 t=now;
53 t.mv+="F2";
54 t.b=B;
55 t.step++;
56 been[t.a][t.b]=true;
57 if(check(t))
58 return t;
59 q.push(t);
60 }
61 //缺少了倒水的瓶子里剩余水的情况
62 if(now.a<A&&now.b<=(A-now.a)&&now.b>0&& !been[now.a+now.b][0])
63 //b给a倒水,没有剩余
64 {
65 t=now;
66 t.mv+="P21";
67 t.step++;
68 t.a+=t.b;
69 t.b=0;
70 been[t.a][t.b]=true;
71 if(check(t))
72 return t;
73 q.push(t);
74 }
75 else //注意只能嵌套一个if
76 if(now.a<A&&now.b>(A-now.a)&& !been[A][now.b-(A-now.a)]) //b给a倒水,有剩余
77 {
78 t=now;
79 t.mv+="P21";
80 t.step++;
81 t.b-=(A-t.a);
82 t.a=A;
83 been[t.a][t.b]=true;
84 if(check(t))
85 return t;
86 q.push(t);
87 }
88 if(now.b<B&&now.a<=(B-now.b)&&now.a>0&& !been[0][now.a+now.b])
89 //a给b倒水,无剩余
90 {
91 t=now;
92 t.mv+="P12";
93 t.step++;
94 t.b+=t.a;
95 t.a=0;
96 been[t.a][t.b]=true;
97 if(check(t))
98 return t;
99 q.push(t);
100 }
101 else
102 if(now.b<B&&now.a>(B-now.b)&& !been[now.a-(B-now.b)][B]) //a倒给b,有剩余
103 {
104 t=now;
105 t.mv+="P12";
106 t.step++;
107 t.a-=(B-t.b);
108 t.b=B;
109 been[t.a][t.b]=true;
110 if(check(t))
111 return t;
112 q.push(t);
113 }
114 //相互倒水完毕~
115 if(now.a>0&& !been[0][now.b])
116 {
117 t=now;
118 t.mv+="D1";
119 t.a=0;
120 t.step++;
121 been[t.a][t.b]=true;
122 if(check(t))
123 return t;
124 q.push(t);
125 }
126 if(now.b>0&& !been[now.a][0])
127 {
128 t=now;
129 t.mv+="D2";
130 t.b=0;
131 t.step++;
132 been[t.a][t.b]=true;
133 if(check(t))
134 return t;
135 q.push(t);
136 }
137 }
138 po ans;
139 ans.step=-1;
140 return ans;
141 }
142 int main(void)
143 {
144 scanf("%ld%ld%ld",&A,&B,&C);
145 memset(been,false,sizeof(been));
146 po start;
147 start.a=start.b=start.step=0;
148 po ans=bfs(start);
149 if(ans.step==-1)
150 printf("impossible\n");
151 else
152 {
153 printf("%ld\n",ans.step);
154 for(long i=0;i!=(long)ans.mv.size();i++)
155 {
156 if(ans.mv.at(i)=='F')
157 {
158 printf("FILL(");
159 i++;
160 printf("%c)\n",ans.mv.at(i));
161 }
162 else
163 if(ans.mv.at(i)=='P')
164 {
165 printf("POUR(");
166 i++;
167 printf("%c",ans.mv.at(i));
168 i++;
169 printf(",%c)\n",ans.mv.at(i));
170 }
171 else
172 {
173 printf("DROP(");
174 i++;
175 printf("%c)\n",ans.mv.at(i));
176 }
177 }
178 }
179 return 0;
180 }
posted on 2011-10-30 21:15 SilVeRyELF 阅读(392) 评论(0) 收藏 举报

浙公网安备 33010602011771号