From 0e89bde90b7417acbe30d3b8b4a4254d47df3ee8 Mon Sep 17 00:00:00 2001 From: AlexPL2201 <99511840+AlexPL2201@users.noreply.github.com> Date: Thu, 17 Feb 2022 19:25:57 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=20"?= =?UTF-8?q?=D0=A5=D0=BE=D0=B4=20=D0=BA=D0=BE=D0=BD=D1=91=D0=BC".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Алгоритм рассчитывает, из какой доступной коню точки будет произведено меньше всего шагов и если такой есть, то идёт туда. --- task_1.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 task_1.cpp diff --git a/task_1.cpp b/task_1.cpp new file mode 100644 index 0000000..f40a870 --- /dev/null +++ b/task_1.cpp @@ -0,0 +1,92 @@ +#include +#include + +using namespace std; +int steps[8][2] = { {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1} }; + +int board[8][8] = { + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, +}; + + + +int what_next(int pos_x, int pos_y) { + int vars[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + for (int i = 0; i < 8; i++) { + if (pos_x + steps[i][0] < 8 && pos_y + steps[i][1] < 8 && pos_x + steps[i][0] >= 0 && pos_y + steps[i][1] >= 0 && board[pos_y + steps[i][1]][pos_x + steps[i][0]] == 0) { + vars[i] += 1; + } + } + int next_vars[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + + for (int i = 0; i < 8; i++) { + int next_x = steps[i][0] + pos_x; + int next_y = steps[i][1] + pos_y; + + + for (int j = 0; j < 8; j++) { + if (vars[i] > 0) { + + if (next_x + steps[j][0] < 8 && next_y + steps[j][1] < 8 && next_x + steps[j][0] >= 0 && next_y + steps[j][1] >= 0 && board[next_y + steps[j][1]][next_x + steps[j][0]] == 0) { + next_vars[i] += 1; + } + } + } + } + int next_vars_min_ind = 0; + int next_vars_min = 10; + for (int i = 0; i < 8; i++) { + if (next_vars[i] < next_vars_min && next_vars[i] != 0) { + next_vars_min = next_vars[i]; + next_vars_min_ind = i; + } + } + + return next_vars_min_ind; +} + + +void show_board(int pos_x, int pos_y, int counter) { + int ind; + while (counter < 64) { + ind = what_next(pos_x, pos_y); + pos_x += steps[ind][0]; + pos_y += steps[ind][1]; + counter++; + board[pos_y][pos_x] = counter; + system("cls"); + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 8; j++) { + cout.width(3); + cout << board[i][j]; + } + cout << endl; + } + } +} + + +int main() +{ + int counter = 1; + setlocale(LC_ALL, "ru"); + int x0 = 8, y0 = 8; + while (x0 >= 8 || y0 >= 8 || x0 < 0 || y0 < 0) { + cout << "Введите координату по X: "; + cin >> x0; + cout << "Введите координату по Y: "; + cin >> y0; + } + + board[y0][x0] = counter; + show_board(x0, y0, counter); + system("pause"); + return 0; +} From 641b323301a0d378ad06b19ebe053ca194573530 Mon Sep 17 00:00:00 2001 From: AlexPL2201 <99511840+AlexPL2201@users.noreply.github.com> Date: Thu, 24 Feb 2022 13:18:12 +0300 Subject: [PATCH 2/2] Update task_1.cpp --- task_1.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/task_1.cpp b/task_1.cpp index f40a870..0c0b60d 100644 --- a/task_1.cpp +++ b/task_1.cpp @@ -17,7 +17,7 @@ int board[8][8] = { -int what_next(int pos_x, int pos_y) { +int what_next(int pos_x, int pos_y, int counter) { int vars[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; for (int i = 0; i < 8; i++) { if (pos_x + steps[i][0] < 8 && pos_y + steps[i][1] < 8 && pos_x + steps[i][0] >= 0 && pos_y + steps[i][1] >= 0 && board[pos_y + steps[i][1]][pos_x + steps[i][0]] == 0) { @@ -47,6 +47,10 @@ int what_next(int pos_x, int pos_y) { next_vars_min = next_vars[i]; next_vars_min_ind = i; } + else if (vars[i] < next_vars_min && vars[i] != 0 && counter == 63) { + next_vars_min = vars[i]; + next_vars_min_ind = i; + } } return next_vars_min_ind; @@ -56,19 +60,11 @@ int what_next(int pos_x, int pos_y) { void show_board(int pos_x, int pos_y, int counter) { int ind; while (counter < 64) { - ind = what_next(pos_x, pos_y); + ind = what_next(pos_x, pos_y, counter); pos_x += steps[ind][0]; pos_y += steps[ind][1]; counter++; board[pos_y][pos_x] = counter; - system("cls"); - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 8; j++) { - cout.width(3); - cout << board[i][j]; - } - cout << endl; - } } } @@ -84,9 +80,16 @@ int main() cout << "Введите координату по Y: "; cin >> y0; } - + clock_t start, end; board[y0][x0] = counter; show_board(x0, y0, counter); - system("pause"); + cout << endl; + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 8; j++) { + cout.width(4); + cout << board[i][j]; + } + cout << endl; + } return 0; }