Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program n1;
- {$mode objfpc}{$H+}
- uses
- {$IFDEF UNIX}{$IFDEF UseCThreads}
- cthreads,
- {$ENDIF}{$ENDIF}
- Classes,
- SysUtils
- { you can add units after this };
- const Q = 10;
- type chessArray = array[1..Q, 1..Q] of integer; //Тип хранящий номер хода на клетках доски
- type moves = array[1..8] of integer;
- {Прикрепить файлы}
- procedure AssignFiles(var input, output : Text);
- begin
- Assign(input, 'input.txt');
- Reset(input);
- Assign(output, 'output.txt');
- end;
- {Считывание размерности доски и текущего положения Коня}
- procedure GetData(var input : Text; var M, N, curC, curR : integer);
- begin
- readln(input, M);
- readln(input, N);
- readln(input, curC);
- readln(input, curR);
- end;
- {Разметить доску}
- procedure AssignBoard(var board : chessArray; M, N, curC, curR, Q : integer);
- var i, j : integer;
- begin
- for i:=1 to Q do
- for j:=1 to Q do
- if (i <= N) and (j <= M) then
- begin
- if (curR = i) and (curC = j) then
- board[i,j]:=1
- else
- board[i,j]:=0;
- end
- else
- board[i,j]:=-1;
- end;
- {Можно ли совершить данный ход}
- function possibleMove(c, r, M, N : integer; board : chessArray) : boolean;
- begin
- if (c >= 1) and (c <= M) and (r >= 1) and (r <= N) and (board[c, r] = 0) then result:=true
- else result:=false;
- end;
- {Существует ли маршрут}
- function Movement(move, curC, curR, M, N : integer; var board : chessArray) : boolean;
- var newC, newR: integer; K : boolean; usedMoves : moves;
- begin
- board[curC, curR]:=move;
- K:=false;
- if (move >= N*M) then K:=true
- else
- begin
- if (possibleMove(curC + 2, curR - 1, M, N, board)) and Movement(move+1, curC + 2, curR - 1, M, N, board) then K:=true
- else if (possibleMove(curC + 2, curR + 1, M, N, board)) and Movement(move+1, curC + 2, curR + 1, M, N, board) then K:=true
- else if (possibleMove(curC - 2, curR + 1, M, N, board)) and Movement(move+1, curC - 2, curR + 1, M, N, board) then K:=true
- else if (possibleMove(curC - 2, curR - 1, M, N, board)) and Movement(move+1, curC - 2, curR - 1, M, N, board) then K:=true
- else if (possibleMove(curC + 1, curR + 2, M, N, board)) and Movement(move+1, curC + 1, curR + 2, M, N, board) then K:=true
- else if (possibleMove(curC + 1, curR - 2, M, N, board)) and Movement(move+1, curC + 1, curR - 2, M, N, board) then K:=true
- else if (possibleMove(curC - 1, curR + 2, M, N, board)) and Movement(move+1, curC - 1, curR + 2, M, N, board) then K:=true
- else if (possibleMove(curC - 1, curR - 2, M, N, board)) and Movement(move+1, curC - 1, curR - 2, M, N, board) then K:=true
- else begin board[curC, curR]:=0; end;
- end;
- result:=K;
- end;
- {Вывод}
- procedure outputData(var outF : Text; f : boolean; M, N : integer; board : chessArray);
- var i, j : integer;
- begin
- writeln(M,N);
- Rewrite(outF);
- if not f then Writeln(outF, 'Marshruta ne sushestvuet')
- else
- begin
- for i:=1 to N do
- for j:=1 to M do
- if (j = M) then
- writeln(outF, IntToStr(board[i, j]))
- else
- begin
- if (board[i,j]>=10) then
- write(outF, IntToStr(board[i, j]) + ' ')
- else
- write(outF, IntToStr(board[i, j]) + ' ');
- end;
- end;
- Close(outF);
- end;
- var M, N, curC, curR, move : integer; input, output : Text; board : chessArray;
- begin
- move:=1;
- AssignFiles(input, output);
- GetData(input, M, N, curC, curR);
- AssignBoard(board, M, N, curC, curR, Q);
- outputData(output, Movement(move, curC, curR, M, N, board), M, N, board);
- readln();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement