Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Eight Queen
#include <iostream> #include <Windows.h> #define n 8 using namespace std; bool m[n][n] = { false }; bool flag_row[n] = { false }; bool flag_column[n] = { false }; void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } bool ck_diagonal(short row, short column) { for (short i = row + 1, j = column + 1; i < n && j < n; i++, j++) if (m[i][j]) return true; for (short i = row + 1, j = column - 1; i < n && j >= 0; i++, j--) if (m[i][j]) return true; for (short i = row, j = column; i >= 0 && j >= 0; i--, j--) if (m[i][j]) return true; for (short i = row, j = column; i >= 0 && j < n; i--, j++) if (m[i][j]) return true; return false; } bool check(short row, short column) { if (flag_row[row] || flag_column[column] || ck_diagonal(row, column)) return false; else return true; } void reset(short row, short column) { flag_row[row] = flag_column[column] = m[row][column] = false; } void set(short row, short column) { flag_row[row] = flag_column[column] = m[row][column] = true; } void out() { static int counter = 0; counter++; for (short i = 0; i < n; i++) { for (short j = 0; j < n; j++) cout << m[i][j]; cout << endl; } cout << endl; cout << "Solution N" << (char)248 << ' ' << counter << endl; } void search(short row = 0, short column = 0) { static short counter = 0; for (short i = row; i < n; i++) for (short j = column; j < n; j++) if (check(i, j)) { set(i, j); counter++; if (counter == n) out(); search(i + 1, 0); reset(i, j); counter--; } } void main() { search(); }
run
|
edit
|
history
|
help
0
C++ exception
Wide string conversion with multibyte chars and locale + concatenation
additional layer of indirection
list multiply
narrow_cast
Spirit Lex combining token_id's with semantic actions
WinAPI not accessible
StreamParam
algorithm_1
Why is vsnprintf Not Writing the Same Number of Characters as strncpy Would?