More actions
imported>Unknown No edit summary |
(Repair MoniWiki formatting after migration) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
DeleteMe) 페이지 제목에 대한 통일성 관계상 페이지 이름 수정을 했습니다. --1002 | [[DeleteMe]]) 페이지 제목에 대한 통일성 관계상 페이지 이름 수정을 했습니다. --1002 | ||
== 8퀸즈 소스1 == | == 8퀸즈 소스1 == | ||
| Line 8: | Line 8: | ||
int y; | int y; | ||
}; | }; | ||
int map | int map[8][8]; | ||
bool MARK(QUEEN Pt); | bool MARK(QUEEN Pt); | ||
int main(){ | int main(){ | ||
QUEEN history | QUEEN history[8]; | ||
int nQueens=0; | int nQueens=0; | ||
history | history[0].x=0; | ||
history | history[0].y=0; | ||
bool bOk; | bool bOk; | ||
while(!bOk || nQueens<8){ | while(!bOk || nQueens<8){ | ||
for(int i=0;i<8;i++) for(int j=0;j<8;j++) map | for(int i=0;i<8;i++) for(int j=0;j<8;j++) map[i][j]=0; | ||
for(i=0;i<nQueens;i++) MARK(history | for(i=0;i<nQueens;i++) MARK(history[i]); | ||
do{ | do{ | ||
bOk=MARK(history | bOk=MARK(history[nQueens]); | ||
if(bOk){ | if(bOk){ | ||
if(nQueens!=7) history | if(nQueens!=7) history[nQueens+1]=history[nQueens]; | ||
nQueens++; | nQueens++; | ||
} | } | ||
else{ | else{ | ||
if(++history | if(++history[nQueens].x==8){ | ||
history | history[nQueens].x=0; | ||
if(++history | if(++history[nQueens].y==8){ | ||
do{ | do{ | ||
nQueens--; | nQueens--; | ||
}while(history | }while(history[nQueens].x==7 && history[nQueens].y==7); | ||
if((++history | if((++history[nQueens].x)==8){ | ||
history | history[nQueens].x=0; | ||
history | history[nQueens].y++; | ||
} | } | ||
break; | break; | ||
| Line 44: | Line 44: | ||
for(int y=0;y<8;y++){ | for(int y=0;y<8;y++){ | ||
for(int x=0;x<8;x++){ | for(int x=0;x<8;x++){ | ||
if(map | if(map[x][y]==0) cout<<' '; | ||
else if(map | else if(map[x][y]==1) cout<<'X'; | ||
else if(map | else if(map[x][y]==2) cout<<'O'; | ||
} | } | ||
cout<<endl; | cout<<endl; | ||
| Line 53: | Line 53: | ||
} | } | ||
bool MARK(QUEEN Pt){ | bool MARK(QUEEN Pt){ | ||
int tmp | int tmp[8][8]; | ||
for(int x=0;x<8;x++) for(int y=0;y<8;y++) tmp | for(int x=0;x<8;x++) for(int y=0;y<8;y++) tmp[x][y]=map[x][y]; | ||
if(tmp | if(tmp[Pt.x][Pt.y]>0) return false; | ||
for(int i=0;i<8;i++){ | for(int i=0;i<8;i++){ | ||
if(tmp | if(tmp[Pt.x][i]==2) return false; | ||
else tmp | else tmp[Pt.x][i]=1; | ||
if(tmp | if(tmp[i][Pt.y]==2) return false; | ||
else tmp | else tmp[i][Pt.y]=1; | ||
if(i-Pt.x+Pt.y<8 && i-Pt.x+Pt.y>=0){ | if(i-Pt.x+Pt.y<8 && i-Pt.x+Pt.y>=0){ | ||
if(tmp | if(tmp[i][i-Pt.x+Pt.y]==2) return false; | ||
else tmp | else tmp[i][i-Pt.x+Pt.y]=1; | ||
} | } | ||
if(Pt.x+Pt.y-i<8 && Pt.x+Pt.y-i>=0){ | if(Pt.x+Pt.y-i<8 && Pt.x+Pt.y-i>=0){ | ||
if(tmp | if(tmp[i][Pt.x-i+Pt.y]==2) return false; | ||
else tmp | else tmp[i][Pt.x-i+Pt.y]=1; | ||
} | } | ||
} | } | ||
tmp | tmp[Pt.x][Pt.y]=2; | ||
for(x=0;x<8;x++) for(int y=0;y<8;y++) map | for(x=0;x<8;x++) for(int y=0;y<8;y++) map[x][y]=tmp[x][y]; | ||
return true; | return true; | ||
} | } | ||
Latest revision as of 00:34, 29 March 2026
DeleteMe) 페이지 제목에 대한 통일성 관계상 페이지 이름 수정을 했습니다. --1002
8퀸즈 소스1
2h:30m, 71line
#include <iostream.h>
struct QUEEN{
int x;
int y;
};
int map[8][8];
bool MARK(QUEEN Pt);
int main(){
QUEEN history[8];
int nQueens=0;
history[0].x=0;
history[0].y=0;
bool bOk;
while(!bOk || nQueens<8){
for(int i=0;i<8;i++) for(int j=0;j<8;j++) map[i][j]=0;
for(i=0;i<nQueens;i++) MARK(history[i]);
do{
bOk=MARK(history[nQueens]);
if(bOk){
if(nQueens!=7) history[nQueens+1]=history[nQueens];
nQueens++;
}
else{
if(++history[nQueens].x==8){
history[nQueens].x=0;
if(++history[nQueens].y==8){
do{
nQueens--;
}while(history[nQueens].x==7 && history[nQueens].y==7);
if((++history[nQueens].x)==8){
history[nQueens].x=0;
history[nQueens].y++;
}
break;
}
}
}
}while(!bOk);
}
for(int y=0;y<8;y++){
for(int x=0;x<8;x++){
if(map[x][y]==0) cout<<' ';
else if(map[x][y]==1) cout<<'X';
else if(map[x][y]==2) cout<<'O';
}
cout<<endl;
}
return 0;
}
bool MARK(QUEEN Pt){
int tmp[8][8];
for(int x=0;x<8;x++) for(int y=0;y<8;y++) tmp[x][y]=map[x][y];
if(tmp[Pt.x][Pt.y]>0) return false;
for(int i=0;i<8;i++){
if(tmp[Pt.x][i]==2) return false;
else tmp[Pt.x][i]=1;
if(tmp[i][Pt.y]==2) return false;
else tmp[i][Pt.y]=1;
if(i-Pt.x+Pt.y<8 && i-Pt.x+Pt.y>=0){
if(tmp[i][i-Pt.x+Pt.y]==2) return false;
else tmp[i][i-Pt.x+Pt.y]=1;
}
if(Pt.x+Pt.y-i<8 && Pt.x+Pt.y-i>=0){
if(tmp[i][Pt.x-i+Pt.y]==2) return false;
else tmp[i][Pt.x-i+Pt.y]=1;
}
}
tmp[Pt.x][Pt.y]=2;
for(x=0;x<8;x++) for(int y=0;y<8;y++) map[x][y]=tmp[x][y];
return true;
}