#include <windows.h>
#include <iostream.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void cek(int, int, char [100][100]);
void print(char [100][100]);
void gotoxy(int x,int y) // koordinat posisi
{
COORD point;
point.X=x-1;
point.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point);
}
void textcolor(unsigned short color) // fungsi warna
{
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
int no_of_queens; //jumlah ratu
int queen = 2; // default ratu
int count = 1;
char ch;
int main(void)
{
char board[100][100];
system("CLS");
printf("---- N-Queen ----\n\n");
printf("Masukkan Jumlah Queen (Minimal 4 & Maksimal 22) : \n");
cin>>no_of_queens;
if(no_of_queens > 23)
{
cout<<"Jumlah queen tidak boleh lebih dari 22\n";
return (0); //exit
}
else if(no_of_queens < 4)
{
printf("Jumlah Queen Minimal 4\n");
return (0); //exit
}
system("CLS");
for(int col = 0; col < no_of_queens; col++) //buat papan
{
memset(board, '-', sizeof(board));
cek( 0, col, board ); // panggil fungsi rekursi
}
system("CLS");
textcolor(15);
getch();
return (0);
}
void cek(int r, int c, char board[100][100]) //r=baris, c=kolom
{
int i, j;
// kondisi selesai rekursi
if ( ( r == no_of_queens ) && ( c == 0 )) // jika baris=jumlah queen dan kolom=0
{
system("CLS");
textcolor(15);
printf("(%d-Queen) Set : %d\n", no_of_queens, count++);
printf("\n");
print( board ); //cetak.
fflush(stdin);
ch = (char)getch(); //kalau mau exit bisa pencet e
system("CLS");
if(ch == 'e')
exit (0);
}
// Vertical check...
for(i = 0; i < r; i++)
{
if ( board[i][c] == queen) // cek koordinat satu kolom dari baris 0-r
return
; // apakah di tiap koordinat itu ada queen atau tidak.
}
// Horizontal check...
for(j = 0; j < c; j++)
{
if ( board[r][j] == queen) // cek koordinat satu baris dari kolom 0-c
return; // apakah di tiap koordinat itu ada queen atau tidak.
}
// Left-Diagonal check...
i = r; j = c;
do
{
if ( board[i][j] == queen ) // cek koordinat dari baris r dan kolom c sampai 0
return;
i--; j--;
}
while( i >= 0 && j >= 0 );
// Right-Diagonal check...
i = r; j = c;
do
{
if ( board[i][j] == queen ) // cek koordinat dari baris r dan kolom c sampai r=0
return;
i--; j++;
}
while( i >= 0 && j < no_of_queens );
// Pasang queen -> posisinya udah pas.
board[r][c] = queen;
r++;
// kembali panggil dirinya-> pergi ke baris dan kolom yg lain.
for(int p = 0; p < no_of_queens; p++)
cek(r, p, board); //panggil dirinya.
for(int h = 0; h < no_of_queens; h++)
board[r - 1][h] = '-'; //isi yang kosong
}
void print(char board[100][100]) //untuk mencetak queen.
{
for(int i = 0; i < no_of_queens; i++)
{
printf("%3d", i + 1); // print nama baris
for(int j = 0; j < no_of_queens; j++)
{
if(board[i][j] == queen)
{
cprintf("%3c", queen); // print queen
}
else
{
cprintf("%3c", 22); // print field yang kosong
}
}
printf("\n");
}
printf(" ");
for(i = 0; i < no_of_queens; i++)
printf("%3d", i + 1); //print nama kolom
printf("\n\nPress E to exit.");
textcolor(7);
}
nqueen.rar_Possibility
版权申诉
38 浏览量
2022-09-20
19:25:53
上传
评论
收藏 1KB RAR 举报
小贝德罗
- 粉丝: 70
- 资源: 1万+
最新资源
- TG-2024-05-23-204718255.mp4
- 候志强@181 5428 8938_20240420112107.amr
- spispispispispi
- 实验二:IP协议分析.zip
- 驱动代码驱动代码驱动代码驱动代码
- SVID_20240523_141155_1.mp4
- Code for the complete guide to tkinter tutorial
- 关于百货中心供应链管理系统.zip
- SimpleFolderIcon-master 修改Unity的Project下的文件夹图标
- A python Tkinter widget to display tile based maps
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈