usaco 1.4 milk3 2008.11.5
{1。刚开始搜索,每种类型的只能倒一次,实际上还可以到很多次
2:用到了哈西
3. DFS 因为牛奶的总量是不变的,所以可以用a,b中的牛奶量做状态,初始状态是(0,0),每次只能有6种选择,a倒b,a倒c,b倒a,b倒c,c倒a,c倒b。用一个数组vis[i][j]判重,s[i]记录c中所有可能值(s[i]=true表示c中可能出现i),如果当前状态是(0,x),那么s[mc -x]=true,最后输出s中所有true的就可以了。这种题目一般都是找出所有状态然后用哈希就ok了}
{
ID:
PROG: milk3
LANG: PASCAL
}
program naruto;
const
maxx=20;
var
s:array[0..maxx,0..maxx]of boolean;
r:array[0..maxx]of boolean;
f1,f2:text;
a,b,c,aa,bb,cc,i:longint;
procedure init;
begin
assign(f1,'milk3.in');
reset(f1);
assign(f2,'milk3.out');
rewrite(f2);
read(f1,a,b,c);
close(f1);
end;
procedure deal(x,y,z:longint);
var
aa,bb,cc:longint;
begin
if x=0 then r[z]:=true;
if x<>0 then
begin
aa:=x;bb:=y;cc:=z;
if aa<=b-bb then
begin
inc(bb,aa);
aa:=0;
end
else
begin
dec(aa,b-bb);
bb:=b;
end;
if not(s[aa,bb]) then
begin
s[aa,bb]:=true;
deal(aa,bb,cc);
end;
aa:=x;bb:=y;cc:=z;
if aa<=c-cc then
begin
inc(cc,aa);
aa:=0;
end
else
begin
dec(aa,c-cc);
cc:=c;
end;
if not(s[aa,bb]) then
begin
s[aa,bb]:=true;
deal(aa,bb,cc);
end;
end;
if y<>0 then
begin
aa:=x;bb:=y;cc:=z;
if bb<=a-aa then
begin
inc(aa,bb);
bb:=0;
end
else
begin
dec(bb,a-aa);aa:=a;
end;
if not(s[aa,bb]) then
begin
s[aa,bb]:=true;
deal(aa,bb,cc);
end;
aa:=x;bb:=y;cc:=z;
if bb<=c-cc then
begin
inc(cc,bb);
bb:=0;
end
else
begin
dec(bb,c-cc);cc:=c;
end;
if not(s[aa,bb]) then
begin
s[aa,bb]:=true;
deal(aa,bb,cc);
end;
end;
if z<>0 then
begin
aa:=x;bb:=y;cc:=z;
if cc<=a-aa then
begin
inc(aa,cc);cc:=0;end
else
begin
dec(cc,a-aa);aa:=a;
end;
if not(s[aa,bb]) then
begin
s[aa,bb]:=true;
deal(aa,bb,cc);
end;
aa:=x;bb:=y;cc:=z;
if cc<=b-bb then
begin
inc(bb,cc);cc:=0;end
else
begin
dec(cc,b-bb);bb:=b;
end;
if not(s[aa,bb]) then
begin
s[aa,bb]:=true;
deal(aa,bb,cc);
end;
end;
end;
{---------main-----------}
begin
init;
fillchar(s,sizeof(s),false);
fillchar(r,sizeof(r),false);
deal(0,0,c);
for i:=0 to c-1 do
if r[i] then write(f2,i,' ');
writeln(f2,c);
close(f2);
end.
data
USER: Xi Zhang []
TASK: milk3
LANG: PASCAL
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 208 KB]
Test 2: TEST OK [0.000 secs, 204 KB]
Test 3: TEST OK [0.000 secs, 208 KB]
Test 4: TEST OK [0.000 secs, 208 KB]
Test 5: TEST OK [0.000 secs, 208 KB]
Test 6: TEST OK [0.000 secs, 204 KB]
Test7: TEST OK [0.011 secs, 208 KB]
Test 8: TEST OK [0.000 secs, 208 KB]
Test 9: TEST OK [0.000 secs, 204 KB]
Test 10: TEST OK [0.000 secs, 204 KB]
All tests OK.
Your program ('milk3') produced all correctanswers! This is your
submission #4 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 -------
2 5 10
------- test 2 -------
20 20 20
------- test 3 -------
5 11 15
------- test 4 -------
2 12 20
------- test 5 -------
19 4 11
------- test 6 -------
5 11 13
------- test 7 -------
3 20 20
------- test 8 -------
7 16 20
------- test 9 -------
20 10 9
------- test 10 -------
7 12 18
Keep up the good work!
Thanks for your submission!