Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lines;
- {$APPTYPE CONSOLE}
- type
- PointV = record
- X,
- Y: integer;
- end;
- var
- Delta: array[1..5] of PointV;
- //
- CS: string;
- Found: boolean;
- Grid: array[0..42, 0..42] of char;
- Used: array[0..42, 0..42] of boolean;
- Ancestor: array[1..42, 1..42] of PointV;
- Queue: array[1..20000] of PointV;
- QMStart,
- QMFinish: integer;
- CurPoint: PointV;
- HoldedX, HoldedY: integer;
- N, // 2..40
- enc,
- enc2: integer;
- function IsOK(X, Y: integer): boolean;
- begin
- IsOK := true;
- If (X < 1) or (Y < 1) or (Y > N) or (X > N) then IsOK := false;
- If Used[X, Y] then IsOK := false;
- If Grid[X, Y] = 'O' then IsOK := false;
- end;
- procedure push(X, Y: integer);
- begin
- Queue[QMFinish].X := X;
- Queue[QMFinish].Y := Y;
- QMFinish := QMFinish + 1;
- end;
- procedure pop;
- begin
- Queue[QMStart].X := 0;
- Queue[QMStart].Y := 0;
- QMStart := QMStart + 1;
- end;
- procedure top;
- begin
- CurPoint.X := Queue[QMStart].X;
- CurPoint.Y := Queue[QMStart].Y;
- end;
- function size: integer;
- begin
- size := QMFinish - QMStart;
- end;
- begin
- // Preset
- QMStart := 1;
- QMFinish := 1;
- Delta[1].X := 1;
- Delta[1].Y := 0;
- Delta[2].X := 0;
- Delta[2].Y := 1;
- Delta[3].X := -1;
- Delta[3].Y := 0;
- Delta[4].X := 0;
- Delta[4].Y := -1;
- Found := false;
- // ALG
- ReadLN(N);
- For enc := 1 to N do
- begin
- ReadLN(CS);
- For enc2 := 1 to N do
- begin
- Grid[enc, enc2] := CS[enc2];
- If Grid[enc, enc2] = '@'
- Then
- begin
- Used[enc, enc2] := true;
- push(enc, enc2);
- end;
- end;
- end;
- //
- While (size > 0) and (Found = false) do
- begin
- top;
- pop;
- For enc := 1 to 4 do
- begin
- If IsOK(CurPoint.X+Delta[enc].X, CurPoint.Y+Delta[enc].Y)
- then
- begin
- Used[CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y] := true;
- Push(CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y);
- Ancestor[CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y].X := CurPoint.X;
- Ancestor[CurPoint.X + Delta[enc].X, CurPoint.Y+Delta[enc].Y].Y := CurPoint.Y;
- If Grid[CurPoint.X+Delta[enc].X, CurPoint.Y+Delta[enc].Y] = 'X'
- Then
- begin
- Found := true;
- CurPoint.X := CurPoint.X + Delta[enc].X;
- CurPoint.Y := CurPoint.Y + Delta[enc].Y;
- end;
- end;
- end;
- end;
- If Found = false
- Then Write('N')
- Else WriteLN('Y');
- If true
- Then
- begin
- While true do
- begin
- Grid[CurPoint.X, CurPoint.Y] := '+';
- HoldedX := CurPoint.X;
- HoldedY := CurPoint.Y;
- CurPoint.X := Ancestor[HoldedX, HoldedY].X;
- CurPoint.Y := Ancestor[HoldedX, HoldedY].Y;
- If Grid[CurPoint.X, CurPoint.Y] = '@' Then
- begin
- Break;
- end;
- end;
- // Print the map
- For enc := 1 to N do
- begin
- For enc2 := 1 to N do
- begin
- Write(Grid[enc, enc2]);
- end;
- If enc < N Then WriteLN('');
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement