Практика программирования (Бейсик, Си, Паскаль)


Дерево решений - часть 4


DIM SHARED tree(360) AS STRING * 6, ind(360) AS INTEGER, pos0 AS STRING * 6

nmax = 1: x0 = 1: y0 -= 2

tree(0) = "BASIC*"

ind(0) = -1

CLS

INPUT "Введите строку с исходной позицией - ", pos0

FOR from = 0 TO nmax - 1:

level NEXT from poisk END

SUB change (il AS INTEGER, jl AS INTEGER)

DIM n AS INTEGER, kl AS INTEGER, tmp AS STRING, str2 AS STRING

IF (il < 0) OR (il > 1) OR (jl < 0) OR (jl > 2) THEN EXIT SUB

kl = il * 3 + jl + 1

str2 = tree(from)

tmp = MID$(str2, k, 1)

MID$(str2, k, 1) =MID$(str2, kl, 1)

MID$(str2, kl, 1) = tmp

FOR n = 0 TO nmax - 1

IF str2 = tree(n) THEN EXIT SUB

NEXT n

ind(nmax) = from

tree(nmax) = str2

nmax = nmax + 1 END SUB

SUB level

FOR k = 1 TO 6

IF MID$(tree(from), k, 1) = "*" THEN EXIT FOR

NEXT k

i = (k - 1} \ 3 ' номер строки

j = (k - 1) MOD 3' номер столбца

CALL change(i - 1, j)

CALL change(i + 1, j )

CALL change(i, j - 1)

CALL change(i, j + 1)

END SUB

SUB poisk

FOR q = 0 TO nmax - 1

IF pos0 = tree(q) THEN GOTO m

NEXT q

PRINT "Эта позиция не сводится к требуемой"

EXIT SUB

m:

CALL printTAB(tree(q))

q = ind(q)

IF q >= 0 THEN GOTO m

END SUB

SUB printTAB (s AS STRING)

LOCATE y0, x0: PRINT "+-+-+-+"

LOCATE y0 + 1, x0: PRINT " | ";

MID$(s, 1, 1);

PRINT "|"; MID$(s, 2, 1);

PRINT "|"; MID$(s, 3, 1); "I" LOCATE y0 + 2, x0:

PRINT "+-+-+-+" LOCATE y0 + 3, x0: PRINT "|"; MID$(s, 4, 1);

PRINT "|"; MID$(s, 5, 1);

PRINT "|"; MID$(s, 6, 1); " |" LOCATE y0 + 4, x0:

PRINT "+-+-+-+" x0 = x0 + 10

IF x0 = 81 THEN y0 = y0 + 5: x0 = 1

END SUB

Программа 6_01.с

#include <stdio.h>

#include <conio.h>

#include <string.h>

void level();

void change(int il, int J12);

void poisk(};

void print_tab(char* s);

char tree[360][7];

char pos0[7];

int ind[360];

int i,j,k,nmax=l;




- Начало -  - Назад -  - Вперед -



Книжный магазин