USACO 2.1.1 The Castle 城堡
此题便是Flood Fill了
{
ID: lan jm
PROG: castle
LANG: PASCAL
}
program castle(input,output);
const
q:array[1..4] of integer=(1,2,4,8);
qx:array[1..4] of integer=(0,-1,0,1);
qy:array[1..4] of integer=(-1,0,1,0);
var
m,n,i,j,k,ans,max,a,b,c,x,y:longint;
f:array[0..51,0..51,1..4] of boolean;
map:array[0..51,0..51] of longint;
size:array[1..2500] of longint;
procedure init;
begin
assign(input,'castle.in');
assign(output,'castle.out');
reset(input);
rewrite(output);
end;
procedure outit;
begin
close(input);
close(output);
end;
procedure bfs(xx,yy:longint); //将相连的房间染上同种颜色
type
data=record
x,y:longint;
end;
var
l,r,k,t:longint;
a:array[0..3000] of data;
begin
l:=1;r:=1;a[1].x:=xx;a[1].y:=yy;t:=1;
while l<=r do
begin
for k:=1 to 4 do
if f[a[l].x,a[l].y,k] then
begin
xx:=a[l].x+qx[k];
yy:=a[l].y+qy[k];
if map[xx,yy]=0 then
begin
map[xx,yy]:=ans;
inc(t);
inc(r);
a[r].x:=xx;
a[r].y:=yy;
end;
end;
inc(l);
end;
size[ans]:=t;
end;
begin
init;
readln(m,n);
fillchar(f,sizeof(f),true);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(a);
for k:=4 downto 1 do
if a>=q[k] then
begin
f[i,j,k]:=false;
dec(a,q[k]);
end;
end;
readln;
end;
ans:=0;
for i:=1 to n do //寻找未染色的房间开始染色
for j:=1 to m do
if map[i,j]=0 then
begin
inc(ans);
map[i,j]:=ans;
bfs(i,j);
if size[ans]>max then max:=size[ans];
end;
writeln(ans);
writeln(max);
for j:=1 to m do //寻找移除哪面墙能得到的最大的房间
for i:=n downto 1 do
for k:=2 to 3 do
begin
x:=i+qx[k];
y:=j+qy[k];
if (map[x,y]<>0)and(map[i,j]<>map[x,y]) then
if max<size[map[x,y]]+size[map[i,j]] then
begin
max:=size[map[x,y]]+size[map[i,j]];
a:=i;
b:=j;
c:=k;
end;
end;
writeln(max);
write(a,' ',b,' ');
if c=2 then writeln('N')
else writeln('E');
outit;
end.
USACO 2.1.2 Ordered Fractions 顺序的分数
此题用的是usaco的题解的解题方法,模拟下便知道了。其实这题暴力枚举也可以的,完全可以过的。
{
ID: lan jm
PROG: frac1
LANG: PASCAL
}
program frac1(input,output);
var
n:longint;
procedure init;
begin
assign(input,'frac1.in');
assign(output,'frac1.out');
reset(input);
rewrite(output);
end;
procedure outit;
begin
close(input);
close(output);
end;
procedure work(a,b,c,d:longint);
begin
if b+d>n then exit;
work(a,b,a+c,b+d);
writeln(a+c,'/',b+d);
work(a+c,b+d,c,d);
end;
begin
init;
readln(n);
writeln('0/1');
work(0,1,1,1);
writeln('1/1');
outit;
end.
USACO 2.1.3 Sorting a Three-Valued Sequence 三值的排序
模拟....
{
ID: lan jm
PROG: sort3
LANG: PASCAL
}
program sort3(input,output);
var
n,l1,l2,i,ans,x,y:longint;
a:array[1..10000] of 1..3;
procedure init;
begin
assign(input,'sort3.in');
assign(output,'sort3.out');
reset(input);
rewrite(output);
end;
procedure outit;
begin
close(input);
close(output);
end;
begin
init;
readln(n);
for i:=1 to n do
begin
readln(a[i]);
if a[i]=1 then inc(l1);
if a[i]=2 then inc(l2);
end;
for i:=1 to l1 do //数值为1的段
begin
if a[i]<>1 then inc(ans);
if a[i]=3 then inc(x);
end;
for i:=l1+1 to l1+l2 do //数值为2的段
if a[i]=3 then inc(ans);
for i:=l1+l2+1 to n do //数值为3的段
if a[i]=1 then inc(y);
if x>y then writeln(ans+x-y)
else writeln(ans);
outit;
end.
USACO 2.1.4 Healthy Holsteins 健康的好斯坦奶牛
此题我刚开始看以为是01背包,可是维数过多行不通,然后看数据极小,就dfs了...
{
ID: lan jm
PROG: holstein
LANG: PASCAL
}
program holstein(input,output);
var
i,j,v,g,ans:longint;
answer,f:array[1..15] of boolean;
a:array[1..25] of longint;
b:array[1..15,1..25] of longint;
procedure init;
begin
assign(input,'holstein.in');
assign(output,'holstein.out');
reset(input);
rewrite(output);
end;
procedure outit;
begin
close(input);
close(output);
end;
procedure work;
var
i,j,k:longint;
p:array[1..25] of longint;
begin
k:=0;
for i:=1 to g do
if f[i] then inc(k);
if k>=ans then exit;
fillchar(p,sizeof(p),0);
for i:=1 to g do
if f[i] then
for j:=1 to v do
inc(p[j],b[i,j]);
for i:=1 to v do
if p[i]<a[i] then exit;
ans:=k;
for i:=1 to g do answer[i]:=f[i];
end;
procedure dfs(dep:longint);
begin
if dep=g+1 then
begin
work;
exit;
end;
f[dep]:=true;
dfs(dep+1);
f[dep]:=false;
dfs(dep+1);
end;
begin
init;
readln(v);
for i:=1 to v do read(a[i]);
readln(g);
for i:=1 to g do
begin
for j:=1 to v do read(b[i,j]);
readln;
end;
ans:=maxlongint;
dfs(1);
write(ans);
for i:=1 to g do
if answer[i] then write(' ',i);
writeln;
outit;
end.
USACO 2.1.5 Hamming Codes 海明码
看清题目很重要!(两两、至少)
{
ID: lan jm
PROG: hamming
LANG: PASCAL
}
program hamming(input,output);
const
wei:array[1..8] of integer=(1,3,7,15,31,63,127,255); //二进制位数
var
n,b,d,i,ans:longint;
a:array[1..64] of longint;
procedure init;
begin
assign(input,'hamming.in');
assign(output,'hamming.out');
reset(input);
rewrite(output);
end;
procedure outit;
begin
close(input);
close(output);
end;
function ok(x:longint):boolean;
var
t,i,s:longint;
begin
for i:=1 to ans do
begin
t:=0;
s:=x xor a[i];
while s<>0 do
begin
if s mod 2=1 then inc(t);
s:=s div 2;
end;
if t<d then exit(false);
end;
exit(true);
end;
begin
init;
readln(n,b,d);
a[1]:=0;
ans:=1;
for i:=1 to wei[b] do
if ans<n then
if ok(i) then
begin
inc(ans);
a[ans]:=i;
end;
for i:=1 to ans-1 do
if i mod 10<>0 then write(a[i],' ')
else writeln(a[i]);
writeln(a[ans]);
outit;
end.