20. Дан лабиринт (матрица), который проограмма должна пройти,с ввыводом результата.
21. Имеется шахматная доска. Конь должен столько ходов, чтобы побывать на всех клетках доски.
22.Расположить 8 ферзей на шахматной доске так, чтобы они не находились друг у друга «под боем».
20. Дан лабиринт (матрица), который проограмма должна пройти,с ввыводом результата.
const n=12; type matr = array [1..n] of string; const mat: matr = ('############', '#...#......#', '..#.#.####.#', '###.#....#.#', '#....###.#..', '####.#.#.#.#', '#..#.#.#.#.#', '##.#.#.#.#.#', '#........#.#', '######.###.#', '#......#...#', '############'); var m: matr; i: integer;
function step(var m: matr;x,y: integer): boolean; begin if m[x][y]<>'.' then begin step := false; exit; end; if y=1 then begin step := true; m[x][y]:='X'; exit; end; m[x][y]:='X'; if step(m,x+1,y) then begin step := true; exit; end; if step(m,x,y+1) then begin step := true; exit; end; if step(m,x-1,y) then begin step := true; exit; end; if step(m,x,y-1) then begin step := true; exit; end; m[x][y]:='.'; step := false; end;
begin m := mat; step(m,5,n); for i:= 1 to n do writeln(m[i]); readln; end.
21. Имеется шахматная доска. Конь должен столько ходов, чтобы побывать на всех клетках доски.
const n=5; var desk: array [0..n,0..n] of integer; nstep: integer;
function step(x,y,nstep: integer): boolean; const xy: array [1..8,1..2] of integer = ((1,-2),(1, 2),(-1,-2),(-1, 2),(2,-1),(2, 1),(-2, 1),(-2,-1)); var i: byte; begin if (nstep = n*n) then begin step:=true; exit; end; if (x < 0) or (x >= n) or (y < 0) or (y >= n) then begin step:= false; exit; end; if (desk[x,y] <> 0) then begin step := false; exit; end; desk[x,y] := nstep+1; for i:= 1 to 8 do if (step(x+xy[i,1], y+xy[i,2],nstep+1)) then begin step := true; exit; end; desk[x,y] := 0; step := false; end;
begin writeln(step(0,0,nstep)); readln; end.
22.Расположить 8 ферзей на шахматной доске так, чтобы они не находились друг у друга «под боем».
var r: array [0..8] of integer; int: integer;
function test(i: integer): boolean; var j: integer; begin for j:= i-1 downto 0 do begin if R[i]=R[j] then begin test:=false; exit; end; if abs(R[i]-R[j])=i-j then test:= false; end; end;
function step(i: integer): boolean; var j: integer; begin if i=8 then begin step:=true; exit; end; for j:= 0 to 7 do begin r[i]:=j; if not test(i) then continue; if step(i+1) then begin step:=true; exit; end; end; step:=false; end;
begin step(0); for int:= 0 to 7 do writeln(r[int]); readln; end.