Problem
Suppose that you have a chess board and eight queens. The squares of the chess board being (1,1) to (8,8). Now you have to place the queens such that they could not attack eachother(no queen should be able to attack any other).
Solution
Suppose that you have a chess board and eight queens. The squares of the chess board being (1,1) to (8,8). Now you have to place the queens such that they could not attack eachother(no queen should be able to attack any other).
Solution
/*
* File: main.cpp
* Author: power
*
* Created on January 19, 2012, 2:38 PM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class point{
public:
point(){
this->x=0;
this->y=0;
}
point(int x,int y){
this->x=x;
this->y=y;
}
int x;
int y;
void setx(int a){
this->x=a;
}
void sety(int a){
this->y=a;
}
void display(){
cout<<"("<<x<<","<<y<<")";
}
};
* Author: power
*
* Created on January 19, 2012, 2:38 PM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class point{
public:
point(){
this->x=0;
this->y=0;
}
point(int x,int y){
this->x=x;
this->y=y;
}
int x;
int y;
void setx(int a){
this->x=a;
}
void sety(int a){
this->y=a;
}
void display(){
cout<<"("<<x<<","<<y<<")";
}
};
point* p(char x,char y);
void solution(point *p,int size,point *store[],point *a[]);
bool nonattack(point *p, point *q,int size);
void display(point *p);
void solve(point*[]);
bool chkPredefined(point *c,point *pre[]);
int main(int argc, char** argv) {
point *po[8]={p('1','8'),p('2','4'),p('3','C'),p('4','D'),p('5','E'),p('6','F'),p('7','G'),p('8','H')};
solve(po);
return 0;
}
bool chkPredefined(point *c,point *pre[]){
point *q=c;
for(int i=0;i<8;i++){
if(pre[i]->x!=0){
if(pre[i]->x!=q->x){
//cout<<"Returning false1"<<endl;
return false;
}
}
if(pre[i]->y!=0){
if(pre[i]->y!=q->y){
//cout<<"returning false2"<<endl;
return false;
}
}
q+=1;
}
return true;
}
void solve(point *a[]){
point p[8];
point *store[8];
//initialization
for(int i=1;i<=8;i++){
p[i-1].x=i;
}
solution(p,8,store,a);
//return 0;
}
void solution(point *c,int size,point *store[],point *a[]){
static int st=0;
if(size==0){
//display(c-8);exit(1);
return;
}else{
int i=0;
while(i++<8){
c->y=i;
//recdisplay(c,8);
//display(c);return;
//display(c);return;
//cout<<c->y;
//continue;
solution((c+1),size-1,store,a);
if(size==1)
if(nonattack(c-7,(c-6),7)){
if(chkPredefined(c-7,a)){
cout<<st++<<" :";
display(c-7);
}else{
continue;
}
}else{
continue;
}
}
}
}
bool nonattack(point *c, point *others,int size){
/*returns true if queens are in non attacking condition*/
if(size==0){
//cout<<size<<endl;
//recdisplay(others-1,1);exit(1);//display last one
return true;
}else{
int y=c->y;
int y1=others->y;
int x=c->x;
int x1=others->x;
if(y==y1){return false;}//attack
if((y1-y)==(x1-x)){return false;}
if((y1-y)==(x-x1)){return false;}
if(nonattack(c,(others+1),size-1)&& nonattack(others,others+1,size-1)){
return true;
}else{
return false;
}
}
}
void display(point *p){
point *q;//=p;
q=p;
for(int i=0;i<8;i++){
cout<<"("<<q->x<<","<<q->y<<") ";
q=q+1;
}
cout<<endl;
}
point* p(char x,char y){
//cout<<x<<y<<endl;
int xt=0,yt=0;
if(x>'0'&&x<='9'){
xt=x-'0';
}
if(y>'0'&&y<='9'){
yt=y-'0';
}
return new point(xt,yt);
}
void solution(point *p,int size,point *store[],point *a[]);
bool nonattack(point *p, point *q,int size);
void display(point *p);
void solve(point*[]);
bool chkPredefined(point *c,point *pre[]);
int main(int argc, char** argv) {
point *po[8]={p('1','8'),p('2','4'),p('3','C'),p('4','D'),p('5','E'),p('6','F'),p('7','G'),p('8','H')};
solve(po);
return 0;
}
bool chkPredefined(point *c,point *pre[]){
point *q=c;
for(int i=0;i<8;i++){
if(pre[i]->x!=0){
if(pre[i]->x!=q->x){
//cout<<"Returning false1"<<endl;
return false;
}
}
if(pre[i]->y!=0){
if(pre[i]->y!=q->y){
//cout<<"returning false2"<<endl;
return false;
}
}
q+=1;
}
return true;
}
void solve(point *a[]){
point p[8];
point *store[8];
//initialization
for(int i=1;i<=8;i++){
p[i-1].x=i;
}
solution(p,8,store,a);
//return 0;
}
void solution(point *c,int size,point *store[],point *a[]){
static int st=0;
if(size==0){
//display(c-8);exit(1);
return;
}else{
int i=0;
while(i++<8){
c->y=i;
//recdisplay(c,8);
//display(c);return;
//display(c);return;
//cout<<c->y;
//continue;
solution((c+1),size-1,store,a);
if(size==1)
if(nonattack(c-7,(c-6),7)){
if(chkPredefined(c-7,a)){
cout<<st++<<" :";
display(c-7);
}else{
continue;
}
}else{
continue;
}
}
}
}
bool nonattack(point *c, point *others,int size){
/*returns true if queens are in non attacking condition*/
if(size==0){
//cout<<size<<endl;
//recdisplay(others-1,1);exit(1);//display last one
return true;
}else{
int y=c->y;
int y1=others->y;
int x=c->x;
int x1=others->x;
if(y==y1){return false;}//attack
if((y1-y)==(x1-x)){return false;}
if((y1-y)==(x-x1)){return false;}
if(nonattack(c,(others+1),size-1)&& nonattack(others,others+1,size-1)){
return true;
}else{
return false;
}
}
}
void display(point *p){
point *q;//=p;
q=p;
for(int i=0;i<8;i++){
cout<<"("<<q->x<<","<<q->y<<") ";
q=q+1;
}
cout<<endl;
}
point* p(char x,char y){
//cout<<x<<y<<endl;
int xt=0,yt=0;
if(x>'0'&&x<='9'){
xt=x-'0';
}
if(y>'0'&&y<='9'){
yt=y-'0';
}
return new point(xt,yt);
}
No comments:
Post a Comment
If you like to say anything (good/bad), Please do not hesitate...