Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- type
- point = record
- x, y: integer;
- end;
- TData = point;
- PListQueue = ^TListQueue;
- PListNode = ^TListNode;
- TUsed = array of array of boolean;
- TListQueue = record
- head, tail: PListNode;
- size: dword;
- end;
- TListNode = record
- next: PListNode;
- data: TData;
- end;
- function newListQueue(): PListQueue;
- var
- p: PListQueue;
- begin
- new(p);
- if (p = nil) then
- exit(nil);
- p^.head := nil;
- p^.tail := nil;
- p^.size := 0;
- exit(p);
- end;
- function pushData(Queue: PListQueue; const Data: TData): boolean;
- var
- node: plistnode;
- begin
- new(node);
- if (node = nil) then
- exit(False);
- node^.Data := Data;
- node^.Next := nil;
- if (Queue^.tail <> nil) then
- Queue^.tail^.Next := node;
- Queue^.tail := node;
- Queue^.size += 1;
- if (Queue^.head = nil) then
- Queue^.head := Queue^.tail;
- exit(True);
- end;
- function popData(Queue: PListQueue): boolean;
- var
- node: plistnode;
- begin
- if (Queue^.head = nil) then
- exit(False);
- node := queue^.head;
- Queue^.head := node^.Next;
- dispose(node);
- queue^.size -= 1;
- exit(True);
- end;
- function getData(Queue: PListQueue): TData;
- begin
- exit(Queue^.head^.Data);
- end;
- function getSize(const Queue: PListQueue): dword;
- begin
- exit(Queue^.size);
- end;
- /////////////////
- procedure Make_path(q: PListQueue; p: point; n, m: integer; used: TUsed);
- var
- buf: point;
- begin
- if (p.x > 0) then
- begin
- if (used[p.x, p.y]) then
- begin
- buf.x := p.x + 1;
- buf.y := p.y;
- writeln('(', buf.x, ',', buf.y, ')');
- end;
- end
- else if (p.x < n) then
- begin
- if (used[p.x, p.y]) then
- begin
- buf.x := p.x - 1;
- buf.y := p.y;
- writeln('(', buf.x, ',', buf.y, ')');
- end;
- end
- else if (p.y < n) then
- begin
- if (used[p.x, p.y]) then
- begin
- buf.x := p.x;
- buf.y := p.y + 1;
- writeln('(', buf.x, ',', buf.y, ')');
- end;
- end
- else if (p.y > 0) then
- begin
- if (used[p.x, p.y]) then
- begin
- buf.x := p.x;
- buf.y := p.y - 1;
- writeln('(', buf.x, ',', buf.y, ')');
- end;
- end;
- end;
- var
- n, m, len, lengthQ: integer;
- x, y, i, j: integer;
- A: array of array of char;
- q: PListQueue;
- p, buf: point;
- used: array of array of boolean;
- begin
- Assign(input, 'test4.txt');
- Assign(output, 'output.txt');
- reset(input);
- rewrite(output);
- len := 0;
- lengthQ := 0;
- readln(n, m);
- readln(i, j);
- SetLength(used, n, m);
- for y := 0 to n do
- begin
- for x := 1 to m + 1 do
- begin
- used[x, y] := False;
- end;
- end;
- SetLength(A, n, m);
- q := NewListQueue();
- for y := 0 to n do
- begin
- for x := 1 to m + 1 do
- begin
- Read(A[x, y]);
- end;
- readln();
- end;
- p.x := i;
- p.y := j;
- PushData(q, p);
- used[p.x, p.y] := True;
- while (getSize(q) > 0) do
- begin
- if (p.x + 1 > x) or (p.x - 1 < 0) or (p.y + 1 > y) or (p.y - 1 < 0) then
- begin
- Make_path(q, p, n, m, used);
- Exit;
- end
- else
- begin
- p := GetData(q);
- PopData(q);
- if (A[p.x + 1, p.y] = ' ') then
- begin
- buf.x := p.x + 1;
- buf.y := p.y;
- if not (used[buf.x, buf.y]) then
- PushData(q, buf);
- end;
- if (A[p.x - 1, p.y] = ' ') then
- begin
- buf.x := p.x - 1;
- buf.y := p.y;
- if not (used[buf.x, buf.y]) then
- PushData(q, buf);
- end;
- if (A[p.x, p.y + 1] = ' ') then
- begin
- buf.x := p.x;
- buf.y := p.y + 1;
- if not (used[buf.x, buf.y]) then
- PushData(q, buf);
- end;
- if (A[p.x + 1, p.y - 1] = ' ') then
- begin
- buf.x := p.x;
- buf.y := p.y - 1;
- if not (used[buf.x, buf.y]) then
- PushData(q, buf);
- end;
- end;
- end;
- write('no exit');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement