Program li;
type ceil = record
weight: string;
index: integer;
direction: string
end;
var map: List<List<ceil>>;
all: List<(integer, integer, integer)>; // i, j, weight
start, end_: (integer, integer);
n, m, index, weight: integer;
resultMap: List<List<string>>;
function mapFromFile(source: string): List<List<ceil>>;
var mapFile: TextFile;
n, m: integer; // размерность исходной карты/матрицы
line: string; // переменная для хранения строки из файла
begin
result := new List<List<ceil>>; // создаем пустой двумерный лист с типом данных string (строка)
assignfile(mapFile, source); // связываем дескриптор файла с переменной mapFile
reset(mapFile); // открываем файл для чтения
while not eof(mapFile) do begin // цикл до тех пор, пока не конец файла (not eof(mapFile))
readln(mapFile, line); // читаем строку файла в переменную line
result.Add(new List<ceil>);
foreach var v in line.Split(' ').ToList() do begin
var temp: ceil;
temp.weight := v;
temp.index := 0;
temp.direction := '0';
result[result.Count - 1].Add(temp);
end;
end;
closefile(mapFile); // закрываем файл
end;
function mapFromKeyboard(): List<List<ceil>>;
var mapFile: TextFile;
n, m: integer; // размерность исходной карты/матрицы
line: string; // переменная для хранения строки из файла
begin
result := new List<List<ceil>>; // создаем пустой двумерный лист с типом данных string (строка)
writeln('Введите n');
readln(n);
writeln('Введите m');
readln(m);
writeln('Введите поэлементно матрицу. X - заблокированная ячейка, A - стартовая, B - конечная, 0 - пустая');
for var i := 0 to n - 1 do begin
result.Add(new List<ceil>);
for var j := 0 to m - 1 do begin
var temp: ceil;
readln(temp.weight);
temp.direction := '0';
temp.index := 0;
result[i].Add(temp);
end;
end;
end;
function check(i, j: integer): boolean;
begin
result := false;
if(i >= 0) and (j >= 0) and (i < n) and (j < m) and (map[i][j].index = 0) then begin
if(map[i][j].weight <> 'X') and (map[i][j].weight <> 'A') then begin
result := true;
end;
end;
end;
function move(direction: char; i, j: integer): boolean;
begin
var temp: ceil;
case direction of
'u': begin
dec(i);
direction := 'd';
end;
'r': begin
inc(j);
direction := 'l';
end;
'd': begin
inc(i);
direction := 'u';
end;
'l': begin
dec(j);
direction := 'r';
end;
end;
result := true;
if(check(i, j)) and (all.Where(t -> (t[0] = i) and (t[1] = j)).Count = 0) then begin
if(map[i][j].weight <> 'B') then begin
inc(index);
temp.weight := weight.ToString;
temp.index := index;
temp.direction := direction;
map[i][j] := temp;
all.Add((i, j, weight));
end else begin
temp.weight := map[i][j].weight;
temp.index := index;
temp.direction := direction;
map[i][j] := temp;
case direction of
'u': begin
dec(i);
end;
'r': begin
inc(j);
end;
'd': begin
inc(i);
end;
'l': begin
dec(j);
end;
end;
result := false;
end;
end;
end;
function path(current: (integer, integer); length: integer): integer;
begin
if(map[current[0]][current[1]].weight <> 'A') then begin
resultMap[current[0]][current[1]] := map[current[0]][current[1]].weight;
if(map[current[0]][current[1]].direction = 'd') then begin
result += 1 + path((current[0] + 1, current[1]), length + 1);
end;
if(map[current[0]][current[1]].direction = 'u') then begin
result += 1 + path((current[0] - 1, current[1]), length + 1);
end;
if(map[current[0]][current[1]].direction = 'l') then begin
result += 1 + path((current[0], current[1] - 1), length + 1);
end;
if(map[current[0]][current[1]].direction = 'r') then begin
result += 1 + path((current[0], current[1] + 1), length + 1);
end;
end else begin
result := 1;
end;
end;
Begin
var switch: string;
writeln('Осуществить ввод данных из файла? y/n');
readln(switch);
if(switch = 'y') then begin
map := mapFromFile('1');
end else begin
map := mapFromKeyboard();
end;
all := new List<(integer, integer, integer)>; // i, j, weight
n := map.Count;
m := map[0].Count;
for var i := 0 to n - 1 do begin
for var j := 0 to m - 1 do begin
if(map[i][j].weight = 'A') then begin
start := (i, j);
end;
if(map[i][j].weight = 'B') then begin
end_ := (i, j);
end;
end;
end;
index := 0;
var temp: ceil;
for var i := 0 to n - 1 do begin
for var j := 0 to m - 1 do begin
if(map[i][j].weight = 'A') then begin
inc(weight);
move('u', i, j);
move('r', i, j);
move('d', i, j);
move('l', i, j);
end;
end;
end;
writeln(all);
repeat begin
var current := all.First;
var i := current[0];
var j := current[1];
weight := current[2] + 1;
if(weight = 4) then begin
weight := 1;
end;
if(move('u', i, j) = false) then begin
break;
end;
if(move('r', i, j) = false) then begin
break;
end;
if(move('d', i, j) = false) then begin
break;
end;
if(move('l', i, j) = false) then begin
break;
end;
all.Remove(current);
end until(all.Count = 0);
for var i := 0 to map.Count - 1 do begin
for var j := 0 to map[i].Count - 1 do begin
write(map[i][j].weight, Chr(9));
end;
writeln;
end;
writeln;
for var i := 0 to map.Count - 1 do begin
for var j := 0 to map[i].Count - 1 do begin
write(map[i][j].direction, Chr(9));
end;
writeln;
end;
resultMap := new List<List<string>>;
for var i := 0 to n - 1 do begin
resultMap.Add(new List<string>);
for var j := 0 to m - 1 do begin
if(map[i][j].weight = 'X') or (map[i][j].weight = 'A') then begin
resultMap[i].Add(map[i][j].weight);
end else begin
resultMap[i].Add('0');
end;
end;
end;
if(map[end_[0]][end_[1]].direction = '0') then begin
writeln('Путь не найден');
end else begin
writeln('Путь найден, его длина: ', path(end_, 0) - 2);
for var i := 0 to map.Count - 1 do begin
for var j := 0 to map[i].Count - 1 do begin
write(resultMap[i][j], Chr(9));
end;
writeln;
end;
end;
writeln('Сохранить результат работы программы в файл? y/n');
readln(switch);
if(switch = 'y') then begin
var f: Text;
var res: string;
assign(f, 'out.txt');
Rewrite(f);
for var i := 0 to resultMap.count - 1 do begin
for var j := 0 to resultMap[i].count - 1 do begin
res += resultMap[i][j] + ' ';
end;
res += #10;
end;
WriteLn (f, res);
Close (f);
end;
end.