Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program labirint;
- type
- point = record
- x, y: integer;
- end;
- PListQueue = ^TListQueue;
- PListNode = ^TListNode;
- TUsed = array [1..100,1..100] of boolean;
- TAns = array [1..500,1..500] of point;
- TListQueue = record
- head, tail: PListNode;
- size: dword;
- end;
- TListNode = record
- Next: PListNode;
- Data: point;
- 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: point): 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): point;
- begin
- exit(Queue^.head^.Data);
- end;
- function getSize(const Queue: PListQueue): dword;
- begin
- exit(Queue^.size);
- end;
- procedure Way(i,j:integer; p: point; n, m: integer; used: TUsed; ans:TAns);
- var
- buf,prov: point;
- begin
- buf.x:=0;
- buf.y:=0;
- writeln(p.x,' ',p.y);
- while (p.x<>0) and (p.y<>0) do begin
- buf.x:=p.x;
- buf.y:=p.y;
- P.x:=ans[buf.x,buf.y].x;
- P.y:=ans[buf.x,buf.y].y;
- //p.x:=ans[ans[buf.x,buf.y].x,ans[buf.x,buf.y].y].x;
- //p.y:=ans[ans[buf.x,buf.y].x,ans[buf.x,buf.y].y].y;
- if (p.x<>0) and (p.y<>0) then
- writeln( p.x,' ',p.y);
- end;
- end;
- var
- n, m, len, lengthQ: integer;
- x, y, i, j: integer;
- prov:char;
- A: array [1..500,1..500] of char;
- ans: TAns;
- q: PListQueue;
- p, buf: point;
- used: TUsed;
- begin
- Assign(input, 'input.txt');
- Assign(output, 'output.txt');
- reset(input);
- rewrite(output);
- len := 0;
- lengthQ := 0;
- readln(n, m);
- readln(i, j);
- q := NewListQueue();
- for x := 1 to n do
- begin
- for y := 1 to m do
- begin
- Read(A[x, y]);
- end;
- readln;
- end;
- p.x := i;
- p.y := j;
- ans[i,j].x := 0;
- ans[i,j].y := 0;
- prov:=a[p.x, p.y];
- PushData(q, p);
- used[p.x, p.y] := True;
- while (getSize(q) > 0) do
- begin
- if (p.x >= n) or (p.x <= 1) or (p.y >= m) or (p.y <= 1) then
- begin
- Way(i,j, p, n, m, used, ans);
- Exit;
- end
- else
- begin
- p := GetData(q);
- PopData(q);
- used[p.x, p.y] := True;
- 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 begin
- PushData(q, buf);
- ans[buf.x , buf.y].x:=p.x;
- ans[buf.x , buf.y].y:=p.y;
- // used[buf.x , buf.y] := True;
- end;
- 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 begin
- PushData(q, buf);
- ans[buf.x , buf.y].x:=p.x;
- ans[buf.x , buf.y].y:=p.y;
- // used[buf.x , buf.y] := True;
- end;
- 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 begin
- PushData(q, buf);
- ans[buf.x , buf.y].x:=p.x;
- ans[buf.x , buf.y].y:=p.y;
- // used[buf.x , buf.y] := True;
- end;
- 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 begin
- PushData(q, buf);
- ans[buf.x , buf.y].x:=p.x;
- ans[buf.x , buf.y].y:=p.y;
- // used[buf.x , buf.y] := True;
- end;
- end;
- end;
- end;
- Write('no exit');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement