Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program test;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- const
- MAXN = 80;
- INF = 1000000000;
- var
- n, m, i, j, x, y, f, x1, y1, ans: integer;
- c: char;
- used: array[0..MAXN, 0..MAXN] of boolean;
- a: array[0..MAXN, 0..MAXN] of record
- north, south, east, west: boolean;
- end;
- procedure dfs (x, y, p, q: integer);
- begin
- if (x = x1) and (y = y1) and (p < ans) then begin
- ans := p;
- exit;
- end;
- if (q = f) then exit;
- used[x, y] := true;
- if (x < n) and not (used[x + 1, y]) and (a[x, y].north) then
- dfs(x + 1, y, p, q + 1)
- else if (x < n) and not (used[x + 1, y]) then
- dfs(x + 1, y, p + 1, q + 1);
- if (x > 0) and not (used[x - 1, y]) and (a[x, y].south) then
- dfs(x - 1, y, p, q + 1)
- else if (x > 0) and not (used[x - 1, y]) then
- dfs(x - 1, y, p + 1, q + 1);
- if (y < m) and not (used[x, y + 1]) and (a[x, y].east) then
- dfs(x, y + 1, p, q + 1)
- else if (y < m) and not (used[x, y + 1]) then
- dfs(x, y + 1, p + 1, q + 1);
- if (y > 0) and not (used[x, y - 1]) and (a[x, y].west) then
- dfs(x, y - 1, p, q + 1)
- else if (y > 0) and not (used[x, y - 1]) then
- dfs(x, y - 1, p + 1, q + 1);
- used[x, y] := false;
- end;
- begin
- readln(n, m);
- readln(x, y, f);
- readln(x1, y1);
- for i := n downto 0 do begin
- for j := 0 to m do with a[i, j] do begin
- used[i, j] := false;
- north := false;
- south := false;
- east := false;
- west := false;
- c := '=';
- while not ((c = ' ') or eoln) do begin
- read(c);
- case (c) of
- 'N': north := true;
- 'S': south := true;
- 'E': east := true;
- 'W': west := true;
- end;
- end;
- end;
- readln;
- end;
- ans := INF;
- dfs(x, y, 0, 0);
- writeln(ans);
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement