Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.00 KB | None | 0 0
  1. program n1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes,
  10.   SysUtils
  11.   { you can add units after this };
  12.  
  13.  const Q = 10;
  14.  
  15.  type chessArray = array[1..Q, 1..Q] of integer;  //Тип хранящий номер хода на клетках доски
  16.  
  17.  type moves = array[1..8] of integer;
  18.  
  19. {Прикрепить файлы}
  20. procedure AssignFiles(var input, output : Text);
  21. begin
  22.     Assign(input, 'input.txt');
  23.     Reset(input);
  24.     Assign(output, 'output.txt');
  25. end;
  26.  
  27. {Считывание размерности доски и текущего положения Коня}
  28. procedure GetData(var input : Text; var M, N, curC, curR : integer);
  29. begin
  30.     readln(input, M);
  31.     readln(input, N);
  32.     readln(input, curC);
  33.     readln(input, curR);
  34. end;
  35.  
  36. {Разметить доску}
  37. procedure AssignBoard(var board : chessArray; M, N, curC, curR, Q : integer);
  38. var i, j : integer;
  39. begin
  40.     for i:=1 to Q do
  41.       for j:=1 to Q do
  42.        if (i <= N) and (j <= M) then
  43.        begin
  44.         if (curR = i) and (curC = j) then
  45.            board[i,j]:=1
  46.                                      else
  47.            board[i,j]:=0;
  48.         end
  49.         else
  50.         board[i,j]:=-1;
  51. end;
  52.  
  53.  
  54. {Можно ли совершить данный ход}
  55. function possibleMove(c, r, M, N : integer; board : chessArray) : boolean;
  56. begin
  57.     if (c >= 1) and (c <= M) and (r >= 1) and (r <= N) and (board[c, r] = 0) then result:=true
  58.     else result:=false;
  59. end;
  60.  
  61. {Существует ли маршрут}
  62. function Movement(move, curC, curR, M, N : integer; var board : chessArray) : boolean;
  63. var newC, newR: integer; K : boolean; usedMoves : moves;
  64. begin
  65.     board[curC, curR]:=move;
  66.     K:=false;
  67.     if (move >= N*M) then K:=true
  68.     else
  69.     begin
  70.         if (possibleMove(curC + 2, curR - 1, M, N, board)) and Movement(move+1, curC + 2, curR - 1, M, N, board) then K:=true
  71.          else if (possibleMove(curC + 2, curR + 1, M, N, board)) and Movement(move+1, curC + 2, curR + 1, M, N, board) then K:=true
  72.           else if (possibleMove(curC - 2, curR + 1, M, N, board)) and Movement(move+1, curC - 2, curR + 1, M, N, board) then K:=true
  73.            else if (possibleMove(curC - 2, curR - 1, M, N, board)) and Movement(move+1, curC - 2, curR - 1, M, N, board) then K:=true
  74.             else if (possibleMove(curC + 1, curR + 2, M, N, board)) and Movement(move+1, curC + 1, curR + 2, M, N, board) then K:=true
  75.              else if (possibleMove(curC + 1, curR - 2, M, N, board)) and Movement(move+1, curC + 1, curR - 2, M, N, board) then K:=true
  76.               else if (possibleMove(curC - 1, curR + 2, M, N, board)) and Movement(move+1, curC - 1, curR + 2, M, N, board) then K:=true
  77.                else if (possibleMove(curC - 1, curR - 2, M, N, board)) and Movement(move+1, curC - 1, curR - 2, M, N, board) then K:=true
  78.                 else begin board[curC, curR]:=0; end;
  79.     end;
  80.     result:=K;
  81. end;
  82.  
  83.  
  84. {Вывод}
  85. procedure outputData(var outF : Text; f : boolean; M, N : integer; board : chessArray);
  86. var   i, j : integer;
  87. begin
  88. writeln(M,N);
  89.     Rewrite(outF);
  90.     if not f then Writeln(outF, 'Marshruta ne sushestvuet')
  91.              else
  92.              begin
  93.                  for i:=1 to N do
  94.                    for j:=1 to M do
  95.                        if (j = M) then
  96.                        writeln(outF, IntToStr(board[i, j]))
  97.                        else
  98.                        begin
  99.                        if (board[i,j]>=10) then
  100.                        write(outF, IntToStr(board[i, j]) + ' ')
  101.                        else
  102.                        write(outF, IntToStr(board[i, j]) + '  ');
  103.                        end;
  104.              end;
  105.     Close(outF);
  106. end;
  107.  
  108. var M, N, curC, curR, move : integer; input, output : Text; board : chessArray;
  109.  
  110. begin
  111. move:=1;
  112. AssignFiles(input, output);
  113. GetData(input, M, N, curC, curR);
  114. AssignBoard(board, M, N, curC, curR, Q);
  115. outputData(output, Movement(move, curC, curR, M, N, board), M, N, board);
  116. readln();
  117. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement