Advertisement
ArinaRaguzina

labirint

Jan 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.17 KB | None | 0 0
  1. program labirint;
  2. type
  3.   point = record
  4.     x, y: integer;
  5.   end;
  6.  
  7.   PListQueue = ^TListQueue;
  8.   PListNode = ^TListNode;
  9.   TUsed =  array  [1..100,1..100] of boolean;
  10.   TAns = array [1..500,1..500]  of point;
  11.  
  12.   TListQueue = record
  13.     head, tail: PListNode;
  14.     size: dword;
  15.   end;
  16.  
  17.   TListNode = record
  18.     Next: PListNode;
  19.     Data: point;
  20.   end;
  21.  
  22.   function newListQueue(): PListQueue;
  23.   var
  24.     p: PListQueue;
  25.   begin
  26.     new(p);
  27.     if (p = nil) then
  28.       exit(nil);
  29.     p^.head := nil;
  30.     p^.tail := nil;
  31.     p^.size := 0;
  32.     exit(p);
  33.  
  34.   end;
  35.  
  36.   function pushData(Queue: PListQueue; const Data: point): boolean;
  37.   var
  38.     node: plistnode;
  39.   begin
  40.     new(node);
  41.     if (node = nil) then
  42.       exit(False);
  43.     node^.Data := Data;
  44.     node^.Next := nil;
  45.     if (Queue^.tail <> nil) then
  46.       Queue^.tail^.Next := node;
  47.     Queue^.tail := node;
  48.     Queue^.size += 1;
  49.  
  50.     if (Queue^.head = nil) then
  51.       Queue^.head := Queue^.tail;
  52.     exit(True);
  53.  
  54.   end;
  55.  
  56.   function popData(Queue: PListQueue): boolean;
  57.   var
  58.     node: plistnode;
  59.   begin
  60.     if (Queue^.head = nil) then
  61.       exit(False);
  62.     node := queue^.head;
  63.     Queue^.head := node^.Next;
  64.     dispose(node);
  65.     queue^.size -= 1;
  66.     exit(True);
  67.  
  68.   end;
  69.  
  70.   function getData(Queue: PListQueue): point;
  71.   begin
  72.     exit(Queue^.head^.Data);
  73.   end;
  74.  
  75.   function getSize(const Queue: PListQueue): dword;
  76.   begin
  77.     exit(Queue^.size);
  78.   end;
  79.  
  80.   procedure Way(i,j:integer; p: point; n, m: integer; used: TUsed; ans:TAns);
  81.   var
  82.     buf,prov: point;
  83.   begin
  84.     buf.x:=0;
  85.     buf.y:=0;
  86.     writeln(p.x,' ',p.y);
  87.     while (p.x<>0) and (p.y<>0) do begin
  88.  
  89.        buf.x:=p.x;
  90.        buf.y:=p.y;
  91.        P.x:=ans[buf.x,buf.y].x;
  92.        P.y:=ans[buf.x,buf.y].y;
  93.        //p.x:=ans[ans[buf.x,buf.y].x,ans[buf.x,buf.y].y].x;
  94.        //p.y:=ans[ans[buf.x,buf.y].x,ans[buf.x,buf.y].y].y;
  95.        if (p.x<>0) and (p.y<>0) then
  96.           writeln( p.x,' ',p.y);
  97.     end;
  98.   end;
  99.  
  100. var
  101.   n, m, len, lengthQ: integer;
  102.   x, y, i, j: integer;
  103.   prov:char;
  104.   A: array [1..500,1..500]  of char;
  105.   ans: TAns;
  106.   q: PListQueue;
  107.   p, buf: point;
  108.   used: TUsed;
  109. begin
  110.   Assign(input, 'input.txt');
  111.   Assign(output, 'output.txt');
  112.   reset(input);
  113.   rewrite(output);
  114.   len := 0;
  115.   lengthQ := 0;
  116.   readln(n, m);
  117.   readln(i, j);
  118.  
  119.   q := NewListQueue();
  120.   for x := 1 to n do
  121.   begin
  122.     for y := 1 to m  do
  123.     begin
  124.       Read(A[x, y]);
  125.     end;
  126.     readln;
  127.   end;
  128.   p.x := i;
  129.   p.y := j;
  130.   ans[i,j].x := 0;
  131.   ans[i,j].y := 0;
  132.   prov:=a[p.x, p.y];
  133.   PushData(q, p);
  134.   used[p.x, p.y] := True;
  135.   while (getSize(q) > 0) do
  136.   begin
  137.     if (p.x  >= n) or (p.x  <= 1) or (p.y >= m) or (p.y  <= 1) then
  138.     begin
  139.       Way(i,j, p, n, m, used, ans);
  140.       Exit;
  141.     end
  142.     else
  143.     begin
  144.       p := GetData(q);
  145.       PopData(q);
  146.       used[p.x, p.y] := True;
  147.       if (A[p.x + 1, p.y] = '.') then
  148.       begin
  149.         buf.x := p.x + 1;
  150.         buf.y := p.y;
  151.         if not (used[buf.x, buf.y]) then  begin
  152.           PushData(q, buf);
  153.           ans[buf.x , buf.y].x:=p.x;
  154.           ans[buf.x , buf.y].y:=p.y;
  155.      //     used[buf.x , buf.y] := True;
  156.         end;
  157.       end;
  158.       if (A[p.x - 1, p.y]  = '.') then
  159.       begin
  160.         buf.x := p.x - 1;        buf.y := p.y;
  161.         if not (used[buf.x, buf.y]) then  begin
  162.           PushData(q, buf);
  163.           ans[buf.x , buf.y].x:=p.x;
  164.           ans[buf.x , buf.y].y:=p.y;
  165.      //     used[buf.x , buf.y] := True;
  166.         end;
  167.       end;
  168.       if (A[p.x, p.y + 1] = '.') then
  169.       begin
  170.         buf.x := p.x;
  171.         buf.y := p.y + 1;
  172.         if not (used[buf.x, buf.y]) then  begin
  173.           PushData(q, buf);
  174.           ans[buf.x , buf.y].x:=p.x;
  175.           ans[buf.x , buf.y].y:=p.y;
  176.       //    used[buf.x , buf.y] := True;
  177.         end;
  178.       end;
  179.       if (A[p.x , p.y - 1] = '.') then
  180.       begin
  181.         buf.x := p.x;
  182.         buf.y := p.y - 1;
  183.         if not (used[buf.x, buf.y]) then  begin
  184.           PushData(q, buf);
  185.           ans[buf.x , buf.y].x:=p.x;
  186.           ans[buf.x , buf.y].y:=p.y;
  187.        //   used[buf.x , buf.y] := True;
  188.         end;
  189.       end;
  190.  
  191.     end;
  192.  
  193.   end;
  194.   Write('no exit');
  195. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement