def dijkstra(graph, node): """ Simulate the dijkstra algorithm in a graph """ distance_to = {} distance_to[node] = 0 distance_path = {} while (distance_to): # in case we have a disjoint graph op_node = min_distance(distance_to) distance_path[op_node] = distance_to[op_node] del distance_to[op_node] for x, x_len in graph[op_node].items(): if x not in distance_path: if x not in distance_to: distance_to[x] = distance_path[op_node] + x_len elif distance_to[x] > distance_path[op_node] + x_len: distance_to[x] = distance_path[op_node] + x_len return distance_path
type maze = array [1..k, 1..k] of integer; var l : maze; n, m: integer; i, j: integer; c: char; t: text; w: integer; x0, y0: integer; x1, y1: integer;
procedure ways(a,b,r:integer); begin if (w = 0) or (r < w) then {нет смысла идти дальше, если текущий путь уже превосходит найденный} if (l[a,b] <> -2) then if (r < l[a,b]) or (l[a,b] = -1) then {нет смысла идти, если текущая клетка уже была достигнута за меньшее число шагов} begin l[a,b] := r; if (a = x1) and (b = y1) then w := r else begin if a <> 1 then ways(a - 1, b, r + 1); if b <> 1 then ways(a, b - 1, r + 1); if a <> n then ways(a + 1, b, r + 1); if b <> m then ways(a, b + 1, r + 1); end end; end; begin assign(t, 'input.txt'); reset(t); w := 0; readln(t, n, m); readln(t, x0, y0); readln(t, x1, y1); for i := 1 to n do begin for j := 1 to m do begin read(t, c); case c of '.' : l[i,j] := -1; {будем считать, что если клетка отмечена как -1, то путь к ней еще не найден} 'X' : l[i,j] := -2; {-2, если клетка непроходима} end; end; readln(t) end; close(t); if (l[x0,y0] <> -2) and (l[x1,y1] <> -2) then begin l[x0,y0] := 1; {просто трюк, чтобы пройти проверку на (r < l[x0,y0])} ways(x0, y0, 0); end else l[x1,y1] := -1; writeln(l[x1,y1]) end.
DIM A(X)
s = 0
FOR i = 0 TO X - 1
INPUT "A(X)=", A(X)
IF A(X) > 0 THEN s = s + A(X)
NEXT i
PRINT "s="; s
END
Тестовое решение:
X=6
A(X)=-3
A(X)=4.5
A(X)=2
A(X)=-7
A(X)=0
A(X)=2.1
s= 8.6