Помощь - Поиск - Пользователи - Календарь
Полная версия: поиск минимального пути в массиве
Форум «Всё о Паскале» > Pascal, Object Pascal > Задачи
MaziF
УСЛОВИЕ
Mr. F. wants to get a document be signed by a minister. A minister signs a document only if it is approved by his ministry. The ministry is an M-floor building with floors numbered from 1 to M, 1 ≤ M ≤ 100. Each floor has N rooms (1 ≤ N ≤ 500) also numbered from 1 to N. In each room there is one (and only one) official.
A document is approved by the ministry only if it is signed by at least one official from the M-th floor. An official signs a document only if at least one of the following conditions is satisfied:
the official works on the 1st floor;
the document is signed by the official working in the room with the same number but situated one floor below;
the document is signed by an official working in a neighbouring room (rooms are neighbouring if they are situated on the same floor and their numbers differ by one).
Each official collects a fee for signing a document. The fee is a non-negative integer not exceeding 109.
You should find the cheapest way to approve the document.
Исходные данные
The first line of an input contains two integers, separated by space. The first integer M represents the number of floors in the building, and the second integer N represents the number of rooms per floor. Each of the next M lines contains N integers separated with spaces that describe fees (the k-th integer at l-th line is the fee required by the official working in the k-th room at the l-th floor).
Результат
You should print the numbers of rooms in the order they should be visited to approve the document in the cheapest way. If there are more than one way leading to the cheapest cost you may print an any of them.
ПРИМЕР
3 4
10 10 1 10
2 2 2 10
1 10 10 10
ОТВЕТ
3 3 2 1 1


я написал программу, которая правильно находит минимальную цену, но выводит не правильный путь
не могу понять, где теряется правильный sad.gif
я вообще правильно решаю?
program Project;
type
sp=record
st:string;
cen:longint;
end;
var A,B: Array [1..100,1..500] of integer;
M,N,i,j: integer;
C:longint;
S:string;
D:sp;
procedure Input;
var F:Text;
i,j:integer;
begin
Assign(F,'input.txt');
Reset(F);
Readln(F,M,N);
for i:=1 to m do
begin
for j:=1 to n do read(F,A[i,j]);
readln(F)
end;
end;

function Osn(X,Y:integer):sp;
var
Left,Right,Front:sp;
V:string;
i,j:integer;
rez:sp;
begin
Left.cen:=maxlongint; Right.cen:=maxlongint; Front.cen:=maxlongint;
Left.st:=''; Right.st:=''; Front.st:='';
If (y<>1)and(B[x,y-1]<>1) then begin
B[x,y-1]:=1; Left:=Osn(x,y-1); left.cen:=left.cen + A[x,y]; str(y-1,V); left.st :=left.st +V+' ';
end;
If (y<>N)and(B[x,y+1]<>1) then begin
B[x,y+1]:=1; Right:=Osn(x,y+1); Right.cen:=Right.cen+ A[x,y]; str(y+1,V); right.st :=right.st +V+' ';
end;
If x<>M then
begin
for i:=x+1 to M do
for j:=1 to N do B[i,j]:=0;
Front:=Osn(x+1,y); Front.cen:=Front.cen + A[x,y]; str(y,V); front.st:=front.st +V+' ';
end else
begin
Front.cen:=A[x,y];
Front.st:='';
end;

If (Left.cen<=Right.cen)and(Left.cen<=Front.cen) then begin rez:=Left; end else
If (Right.cen<=Left.cen)and(Right.cen<=Front.cen) then begin rez:=Right; end else
begin rez:=Front; end;
Osn:=rez;
end;

procedure Output;
var
F:text;
i:integer;
begin
assign(F,'output.txt');
rewrite(F);
writeln(F,C);
writeln(F,S);
Close(F);
end;

begin
Input;
C:=maxlongint;
for i:=1 to N do
begin
D:=Osn(1,i);
If C>D.cen then
begin
C:=D.cen;
S:=D.st
end;
end;
Output;
end.
Lapp
Если программа использует файл, то желательно прикладывать проверочный файл. Чтоб люди не мучились, создавая и пытаясь повторить твой результат. Если файл текстовый и не очень большой, то можно прямо в тексте.
MaziF
ПРИМЕР
5 6
525 0 171 0 872 673
0 843 0 0 0 0
0 277 0 202 0 0
0 0 733 957 65 96
637 566 0 0 0 441

ОТВЕТ
4 4 5 5 5 5

А у меня выводит
3 3 4 5 5 5 6 6 5 4 3 4
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.