Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
курс._ по _инф..doc
Скачиваний:
0
Добавлен:
17.07.2019
Размер:
270.34 Кб
Скачать
  1. Библиографический список.

1. Е. А. Зуев. Программирование на языке Turbo Pascal 6.0, 7.0, М.:Веста,Радио и связь, 1993, — С.376.

2. Марченко А.И. Программирование в среде Turbo Pascal 7.0 / А.И. Марченко, Л.А. Марченко — К.: Век, 2000. — 464 с.

3. Дал У. Структурное программирование / У. Дал, Э. Дейкстра, К. Хоор — М.: Мир, 1975. — 248 с.

4. Моргун Александр Николаевич. Справочник по Turbo Pascal для студентов. — М.: Диалектика, 2006. — С. 608.

5. Острейковский В.А. Информатика: учебн. для вузов / В.А. Острейковский. — М.: Высш. шк., 2005. — 511 с.: ил.

  1. Приложение.

Комментированный текст программы.

Program Kurs_3;

Uses CRT;

Label 1;

Const

N=20 ; {max number matrix points}

D=50000; {delay value}

Var

x ,y , {cursor co-ordinates variables}

sx,sy, {starting element's co-ordinates}

ly,lx, {last changed element co-ordinates}

i ,j {matrix size}

: Integer;

L : Array [1..N,1..N] of Integer; {main matrix}

st,fin : Boolean; {start and final elements switches}

{special keys scan function}

Function code : Integer;

Var

ch : char;

Begin

ch := ReadKey;

code:=ord(ch);

End;

{neighbor check function}

Function nei : Boolean;

Begin

if ((x=lx+1) and (y=ly))

or ((x=lx-1) and (y=ly))

or ((y=ly+1) and (x=lx))

or ((y=ly-1) and (x=lx))

then nei:=True else nei:=False;

End;

{the border of matrix checking function}

Function border : integer;

Begin

border:=0;

if (y=1) then border:=2; {top border}

if (y=i) then border:=4; {bottom border}

if (x=1) then border:=1; {left border}

if (x=j) then border:=3; {right border}

End;

Begin

ClrScr;

Repeat

Begin

Writeln ('enter the number of matrix''s strings 0<i<=',N);

Read (i);

if i<=2 then Write (' wrong number of strings, ');

if i>N then Write (' too much strings, ');

End

Until (2<i) and (i<=N);

Repeat Begin

Writeln ('enter the number of matrix''s columns 0<j<=',N);

Read (j);

if j<=2 then Write (' wrong number of columns, ');

if j>N then Write (' too much columns, ');

End Until (2<j) and (j<=N);

Writeln;

1: ClrScr;

{matrix output}

For y:=1 to i do

Begin

For x:=1 to j do

Begin

Write (1);

L[y,x]:=1; {input elements into matrix}

End;

Writeln;

End;

GotoXY (1,i+2);

Write ('move the cursor to choose the path element of maze');

st:=False; fin:=False; {turn the switches off}

{move cursor to the 1st element}

y:=1; x:=1;

{prevent corner elements from changing}

L[1,1]:=2;

L[1,j]:=2;

L[i,1]:=2;

L[i,j]:=2;

Repeat Begin {1}

GotoXY(x,y);

Case code of {2}

75: if x>1 then Dec(x); {left arrow}

72: if y>1 then Dec(y); {up arrow}

77: if x<j then Inc(x); {right arrow}

80: if y<i then Inc(y); {down arrow}

13: {"enter" key}

Begin {3}

{condition of element being a "1" digit}

if L[y,x]=1 then

{if we don't have the start of the path,

then this part choosing the start}

if st=False then

Begin {4};

{border check}

if (border<>0) then

Begin {5}

L[y,x]:=0;

Write(0);

sx:=x;

sy:=y;

Case border of

{left}

1:Begin

L[y-1,x]:=2;

L[y+1,x]:=2;

Inc(x);

End;

{top}

2:Begin

L[y,x-1]:=2;

L[y,x+1]:=2;

Inc(y);

End;

{right}

3:Begin

L[y-1,x]:=2;

L[y+1,x]:=2;

Dec(x);

End;

{bottom}

4:Begin

L[y,x-1]:=2;

L[y,x+1]:=2;

Dec(y);

End;

End;

ly:=y;

lx:=x;

GotoXY(x,y);

L[y,x]:=0;

Write(0);

GotoXY(x,y);

st:=True;

End; {5}

End {4 - of st=False}

else {if we already have the starting element,

this part is about making the path}

if nei=True then

Begin {4.1}

Write(0);

L[y,x]:=0;

GotoXY(x,y);

if border<>0 then fin:=True; {the end is found}

{blocking last unused elements}

if (x<>lx-1) and (L[ly,lx-1]=1) then L[ly,lx-1]:=2;

if (x<>lx+1) and (L[ly,lx+1]=1) then L[ly,lx+1]:=2;

if (y<>ly-1) and (L[ly-1,lx]=1) then L[ly-1,lx]:=2;

if (y<>ly+1) and (L[ly+1,lx]=1) then L[ly+1,lx]:=2;

lx:=x;

ly:=y;

{dead end check}

if fin=False then

Begin

if ((L[y+1,x]=2) or (L[y+1,x]=0))

and ((L[y-1,x]=2) or (L[y-1,x]=0))

and ((L[y,x-1]=2) or (L[y,x-1]=0))

and ((L[y,x+1]=2) or (L[y,x+1]=0)) then

Begin

GotoXY(1,i+2);

ClrEol;

Write('Dead end. Press any key to choose the path again');

ReadKey;

Goto 1;

End;

End;

End; {4.1 - end of else branch}

End; {3 - of enter key}

End; {2 - end of key case}

End Until fin=True; {1}

GotoXY(1,i+2);

ClrEol;

Write ('the maze is prepared to be tested by the program. Press any key to start');

{program is going through the maze}

y:=sy;

x:=sx;

GotoXY(x,y);

fin:=False; st:=False;{turn the switches off}

ReadKey;

Write(8);

L[y,x]:=8;

GotoXY(x,y);

Delay(D);

Repeat Begin

lx:=x;

ly:=y;

if st=False then {start going the maze}

Begin

Case border of

1: Inc(x); {left border}

2: Inc(y); {top border}

3: Dec(x); {right border}

4: Dec(y); {bottom border}

End;

st:=True;

End else {continue going through until maze ends}

Begin

if (L[y+1,x]=0) and (ly<>y+1) then Inc(y) {go down} else

if (L[y-1,x]=0) and (ly<>y-1) then Dec(y) {go up} else

if (L[y,x+1]=0) and (lx<>x+1) then Inc(x) {go right} else

Dec(x);{go left}

End;

GotoXY(x,y);

Write(8);

L[y,x]:=8;

GotoXY(x,y);

Delay(D);

if border<>0 then fin:=True;

End Until fin=True;

GotoXY(1,i+2);

ClrEol;

Write('press any key to quit the program');

ReadKey;

End.