Created
October 25, 2013 16:56
-
-
Save marsharp/7157984 to your computer and use it in GitHub Desktop.
Задача про ферзей
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
//#include <fstream> | |
//#include <algorithm> | |
using namespace std; | |
int a[8][8]; | |
int q; | |
void ferz(int i,int j)//закраска битых полей | |
{ | |
int z; | |
for(z=0;z<8;z++) | |
if(a[z][j]!=1)a[z][j]=8; | |
for(z=0;z<8;z++) | |
if(a[i][z]!=1)a[i][z]=8; | |
z=i+j; | |
for(i=0;i<=z;i++) | |
if(a[i][z-i]!=1)a[i][z-i]=8; | |
z-=2*j; | |
for(i=0;i<8;i++) | |
if(a[i+z][i]!=1)a[i+z][i]=8;//работает не на всех компиляторах)) | |
} | |
int vvar(int k)//функция выбора места | |
{ | |
int i=k/8,j=k%8,z; | |
for(;i<8;i++) | |
{ | |
for(j=k%8;j<8;j++) | |
{ | |
if(a[i][j]==0){ | |
ferz(i,j); | |
a[i][j]=1; | |
return k; | |
} | |
k++; | |
} | |
} | |
return 63; | |
} | |
void print() | |
{ | |
//ofstream cout("out.txt"); | |
//cout<<q++<<endl; | |
for(int i=0;i<8;i++) | |
{ | |
for(int j=0;j<8;j++) | |
cout<<a[i][j]; | |
cout<<endl; | |
} | |
cout<<endl<<endl; | |
} | |
void ster(int k)//стирание позиции ферзя | |
{ | |
int i=k/8,j=k%8; | |
a[i][j]=0; | |
for(i=0;i<8;i++) | |
for(j=0;j<8;j++) | |
if(a[i][j]==8)a[i][j]=0; | |
for(i=0;i<8;i++) | |
for(j=0;j<8;j++) | |
if(a[i][j]==1)ferz(i,j); | |
} | |
int chess(int n) | |
{ | |
int k=0; | |
if(n==8)print(); | |
do{ | |
k=vvar(k); | |
k++; | |
if (k==64) return 1; | |
if (chess(n+1)==1)ster(k-1); | |
}while(n<7);//хз почему именно так | |
} | |
int main() | |
{ | |
chess(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment