bạn nào test thấy code của mình đã chuẩn thì có thể post lên topics luôn, (ko post lên code chưa hoàn chỉnh hoặc thuật toán). Hiện tại mình đã nhận được bài giải của:
Code:
program robot;
const
fi = 'robot.inp';
fo = 'robot.out';
maxn = 20;
gd : array[1..4] of integer = (-1,0,1,0);
gc : array[1..4] of integer = (0,1,0,-1);
var
f,g : text;
m,n,h : integer;
a : array[1..maxn,1..maxn] of byte;
c : array[1..maxn,1..maxn,0..20] of integer;
queue : array[1..9000,1..3] of byte;
first,last,result : integer;
procedure khoitao;
begin
fillchar(a,sizeof(a),0);
fillchar(c,sizeof(c),0);
fillchar(queue,sizeof(queue),0);
result:=0;
end;
procedure doc;
var i,j:integer;
begin
readln(f,m,n);
readln(f,h);
for i := 1 to m do
begin
for j := 1 to n do read(f,a[i,j]);
readln(f);
end;
end;
procedure lam;
var i,j,p,q,ii,jj,k:integer;
begin
queue[1,1]:=1;
queue[1,2]:=1;
queue[1,3]:=0;
first:=1;
last:=1;
c[1,1,0]:=1;
while first<=last do
begin
i:=queue[first,1];
j:=queue[first,2];
p:=queue[first,3];
inc(first);
for k := 1 to 4 do
begin
ii:=i+gd[k];
jj:=j+gc[k];
if (ii>0) and (jj>0) and (ii<=m) and (jj<=n) then
begin
if a[ii,jj]=1 then q:=p+1 else q:=0;
if (q<=h) and (c[ii,jj,q]=0) then
begin
c[ii,jj,q]:=c[i,j,p]+1;
inc(last);
queue[last,1]:=ii;
queue[last,2]:=jj;
queue[last,3]:=q;
end;
end;
end;
end;
result:=maxint;
for i := 0 to h do
if (c[m,n,i]>0) and (result>c[m,n,i]) then result:=c[m,n,i];
if result=maxint then result:=-1 else dec(result);
end;
procedure viet;
begin
writeln(g,result);
end;
procedure mofile;
begin
assign(f,fi); reset(f);
assign(g,fo); rewrite(g);
end;
procedure dongfile;
begin
close(f);
close(g);
end;
var test,testcase:integer;
begin
mofile;
readln(f,test);
for testcase:=1 to test do
begin
khoitao;
doc;
lam;
viet;
end;
dongfile;
end.
- Bạn lưu trữ dữ liệu vào 1 mảng 3 chiều ? Ko nên vậy! Chỉ cần đọc vào từng ma trận rồi làm luôn rồi tiếp tục ma trận sau ...
P/S: Bài giải này sẽ là tối ưu nhất nếu như trong 1 tuần tới ko có bài giải nào tốt hơn
Bookmarks