سلام دوستان.
من دنبال الگریتم مسئله n وزیر میگردم. چون به نظر من پیچیده میاد دنبال یه توضیح ویه کدساده از این برنامه میگردم تا بتونم خودم بنویسمش.
در ضمن باید به روش عقبگرد هم باشه.
Printable View
سلام دوستان.
من دنبال الگریتم مسئله n وزیر میگردم. چون به نظر من پیچیده میاد دنبال یه توضیح ویه کدساده از این برنامه میگردم تا بتونم خودم بنویسمش.
در ضمن باید به روش عقبگرد هم باشه.
سلام
این کد برای 8وزیره
[ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ]
و این:
کد:// eight queens puzzle in C++
// written by Tim Budd, Oregon State University, 1996
//
# include <iostream.h>
# define bool int // not all compilers yet support booleans
class queen {
public:
// constructor
queen (int, queen *);
// find and print solutions
bool findSolution();
bool advance();
void print();
private:
// data fields
int row;
const int column;
queen * neighbor;
// internal method
bool canAttack (int, int);
};
queen::queen(int col, queen * ngh)
: column(col), neighbor(ngh)
{
row = 1;
}
bool queen::canAttack (int testRow, int testColumn)
{
// test rows
if (row == testRow)
return true;
// test diagonals
int columnDifference = testColumn - column;
if ((row + columnDifference == testRow) ||
(row - columnDifference == testRow))
return true;
// try neighbor
return neighbor && neighbor->canAttack(testRow, testColumn);
}
bool queen::findSolution()
{
// test position against neighbors
// The while loop is not necessary since advance() always
// moves the queen to the next "safe" position.
// [Jerry Roth, Gonzaga University, Feb. 2000]
//while (neighbor && neighbor->canAttack (row, column))
if (neighbor && neighbor->canAttack (row, column))
if (! advance())
return false;
// found a solution
return true;
}
bool queen::advance()
{
if (row < 8) {
row++;
return findSolution();
}
if (neighbor && ! neighbor->advance())
return false;
row = 1;
return findSolution();
}
void queen::print()
{
if (neighbor)
neighbor->print();
cout << "column " << column << " row " << row << '\n';
}
int main() {
queen * lastQueen = 0;
for (int i = 1; i <= 8; i++) {
lastQueen = new queen(i, lastQueen);
if (! lastQueen->findSolution())
cout << "no solution\n";
}
lastQueen->print();
}
این سایتم جالبه:
[ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ]
از کمکتون ممنون.
ولی میشه این کد رو با vcpp هم قرار بدین.
سلام
این کد وزیر ها رو سطر به سطر جا به جا میکنه
واسه اینکه بخوایم ستون به ستون تست کنه چی کار باد کنیم؟؟؟
سلامنقل قول:
منظورتون چیه؟
ما تو مسئله nوزیر توی هر سطر تنها می تونیم یه وزیر داشته باشیم
پس فقط اونا رو تو ستونا جابه جا می کنیم:20:
کدی که شما نوشتین توش با کلاس ها کار شده ولی من دنبال یه کد با روش عقب گرد میگردم که هم ساده تر باشه و هم منم بتونم یه چیزلیی لزش بفهمم.
ممنون.
وزیر ها سطر به سطر جا به جا میشننقل قول:
من میخوام ستون به ستون تست کنه و جابه جا کنه
یعنی به جای اینکه توی سطر یه وزیر داشته باشیم
توی ستون یه وزیر باشه فقط
سلام اینم یکم ساده تر
کد:#include <stdio.h> intis_safe(int rows[8], int x, int y) { int i; for (i=1; i <= y; ++i) { if (rows[y-i] == x || rows[y-i] == x-i || rows[y-i] == x+i) return 0; } return 1;} voidputboard(int rows[8]) { static int s = 0; int x, y; printf("\nSolution #%d:\n---------------------------------\n", ++s); for (y=0; y < 8; ++y) { for (x=0; x < 8; ++x) printf(x == rows[y] ? "| Q " : "| "); printf("|\n---------------------------------\n"); }} voideight_queens_helper(int rows[8], int y){ int x; for (x=0; x < 8; ++x) { if (is_safe(rows, x, y)) { rows[y] = x; if (y == 7) putboard(rows); else eight_queens_helper(rows, y+1); } }} intmain(){ int rows[8]; eight_queens_helper(rows, 0); return 0;}
[PHP]#include <stdio.h> intis_safe(int rows[8], int x, int y) { int i; for (i=1; i <= y; ++i) { if (rows[y-i] == x || rows[y-i] == x-i || rows[y-i] == x+i) return 0; } return 1;} voidputboard(int rows[8]) { static int s = 0; int x, y; printf("\nSolution #%d:\n---------------------------------\n", ++s); for (y=0; y < 8; ++y) { for (x=0; x < 8; ++x) printf(x == rows[y] ? "| Q " : "| "); printf("|\n---------------------------------\n"); }} voideight_queens_helper(int rows[8], int y){ int x; for (x=0; x < 8; ++x) { if (is_safe(rows, x, y)) { rows[y] = x; if (y == 7) putboard(rows); else eight_queens_helper(rows, y+1); } }} intmain(){ int rows[8]; eight_queens_helper(rows, 0); return 0;}[/PHP]
این چیه فاطمه؟؟×!!