|
PL/Sql ход конем
|
|||
---|---|---|---|
#18+
Пытаюсь решить задачу про коня. есть пустая шахматная доска (обыкновенная, фиде-европейская, размера 8x8 ) за 64 хода обойти конем все её клетки ( не наступив ни на какое поле дважды) с условием что с поля 64-го хода конь может допрыгнуть до начальной своей позиции (то есть зациклить всю траекторию) На данный момент на начальном этапе обучения, есть ли возможность решить данную задачу без процедур и функций. Нашла решение, но сложно понять как она работает ( чтобы переделать) declare type t is table of binary_integer index by binary_integer; type tt is table of t index by binary_integer; desk tt; ii binary_integer := 1;--по высоте jj binary_integer := 1;--по ширине ind binary_integer := 10; rpt binary_integer := 1; v varchar2(25); function cnt_unvisited(x in binary_integer, y in binary_integer) return binary_integer as result binary_integer := 0; begin for i in (select x + dx x, y + dy y from — NoFormat Start (select 2 dx, -1 dy from dual union all select 2 dx, 1 dy from dual union all select 1 dx, 2 dy from dual union all select -1 dx, 2 dy from dual union all select -2 dx, +1 dy from dual union all select -2 dx, -1 dy from dual union all select -1 dx, -2 dy from dual union all select 1 dx, -2 dy from dual) — NoFormat End where x + dx >= 1 and x + dx <= 8 and y + dy >= 1 and y + dy <= 8) loop result := result + (1 - sign(desk(i.x) (i.y))); end loop; return result; end; procedure find_next(x in out binary_integer, y in out binary_integer) as tmp sys.odcivarchar2list := sys.odcivarchar2list(); begin if ind = 64 then for i in 1 .. 8 loop for j in 1 .. 8 loop if desk(i) (j) = 0 then desk(i)(j) := 64; end if; end loop; end loop; else for i in (select x + dx x, y + dy y from — NoFormat Start (select 2 dx, -1 dy from dual union all select 2 dx, 1 dy from dual union all select 1 dx, 2 dy from dual union all select -1 dx, 2 dy from dual union all select -2 dx, +1 dy from dual union all select -2 dx, -1 dy from dual union all select -1 dx, -2 dy from dual union all select 1 dx, -2 dy from dual) — NoFormat End where x + dx >= 1 and x + dx <= 8 and y + dy >= 1 and y + dy <= 8) loop if desk(i.x) (i.y) = 0 then tmp.extend; tmp(tmp.count) := to_char(cnt_unvisited(i.x, i.y), 'fm00') || to_char(i.x, 'fm00') || to_char(i.y, 'fm00'); end if; end loop; if tmp.count = 0 then raise_application_error(-20000, 'It''s a dead end!'); end if; select t.x, t.y into find_next.x, find_next.y from (select substr(column_value, 3, 2) x, substr(column_value, 5, 2) y from table(tmp) order by substr(column_value, 1, 2), dbms_random.value) t where rownum = 1; end if; end; begin loop ii := 1; jj := 1; ind := 1; — initializing for i in 1 .. 8 loop for j in 1 .. 8 loop desk(i)(j) := 0; end loop; end loop; — finding solution begin for i in 1 .. 8 loop for j in 1 .. 8 loop desk(ii)(jj) := ind; find_next(ii, jj); ind := ind + 1; end loop; end loop; exception when others then if sqlcode = -20000 then null; else raise; end if; end; exit when 64 in (desk(2)(3),desk(3)(2)) or rpt>10000; rpt := rpt+1; end loop; — printing result dbms_output.put_line('rpt='||rpt); dbms_output.put_line(lpad('-', 25, '-')); for i in 1 .. 8 loop v := '|'; for j in 1 .. 8 loop v := v || to_char(desk(i) (j), 'fm00') || '|'; end loop; dbms_output.put_line(v); end loop; dbms_output.put_line(lpad('-', 25, '-')); end; ... |
|||
:
Нравится:
Не нравится:
|
|||
24.03.2020, 18:56 |
|
PL/Sql ход конем
|
|||
---|---|---|---|
#18+
Jack963, 1) Для начала, воспользоваться поиском: - Базовая тема: 12346571 - Вот аналогичный вопрос, как у Вас 22099501 2) Правильно задать вопрос Вы показали код, но читать его, без оформления невозможно. 3) Что Вам непонятно в приведенном коде? Вы разобрались хоть в чем-то? Напишите, как Вы поняли алгоритм. Попробуйте съесть слона по частям Для ознакомления: Студентам, желающим помощи HOWTO :: Как правильно задавать вопросы Как мне оформить свое сообщение? Если хотите, чтобы Вам помогли, задайте вопрос правильно: 1) Полная постановка задачи (без сокращений) 2) Подготовьте тестовые данные, лучше в виде with например (не ваш случай): Код: plsql 1. 2. 3. 4. 5. 6.
2) Покажите что сделали, и что получили (не в пересказе) 3) Старайтесь не использовать screenshot, научитесь копировать и оформлять текст В этом случае Вам обязательно помогут ... |
|||
:
Нравится:
Не нравится:
|
|||
24.03.2020, 21:31 |
|
PL/Sql ход конем
|
|||
---|---|---|---|
#18+
Jack963 Нашла решение, но сложно понять как она работает ... |
|||
:
Нравится:
Не нравится:
|
|||
24.03.2020, 23:14 |
|
PL/Sql ход конем
|
|||
---|---|---|---|
#18+
Stax, спасибо! Но в итоге все равно не в каждой строке понимаю что происходит ... |
|||
:
Нравится:
Не нравится:
|
|||
27.03.2020, 14:40 |
|
|
start [/forum/topic.php?fid=52&fpage=49&tid=1881405]: |
0ms |
get settings: |
10ms |
get forum list: |
14ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
39ms |
get topic data: |
13ms |
get forum data: |
3ms |
get page messages: |
47ms |
get tp. blocked users: |
2ms |
others: | 17ms |
total: | 153ms |
0 / 0 |