[問題] 請問一下我的八皇后程式
看板C_and_CPP (C/C++)作者pocernld (時間像流水般過去了...)時間15年前 (2009/12/12 02:21)推噓67(67推 0噓 26→)留言93則, 69人參與討論串1/3 (看更多)
遇到的問題: (題意請描述清楚)
我寫了一個八皇后程式,但是編譯以後執行卻完全沒東西,
可否請板上各位高手幫我看一下,謝謝
希望得到的正確結果:
八皇后的解
程式跑出來的錯誤結果:
完全不會動...
開發平台:
Dev-c++ 4.9.9.2
有問題的code: (請善用置底文標色功能)
#include<iostream>
using std::cout;
using std::endl;
int count(int,int);
void setchessboard();
int findmin();
int findminadd();
int putqueen(int,int,int,int);
int setblock(int,int);
void printchessboard();
void removequeen(int,int);
void resetchessboard();
int chessboard[8][8];
int countsolnumber=1;
int main()
{
resetchessboard();
int x1,x2,x3,x4,x5,x6,x7,x8;
int y1,y2,y3,y4,y5,y6,y7,y8;
int z1,z2,z3,z4,z5,z6,z7,z8;
for(x1=0;x1<=7;x1++)
for(y1=0;y1<=7;y1++)
{z1=putqueen(x1,y1,0,0);
if(z1==2)
{for(x2=0;x2<=7;x2++)
for(y2=0;y2<=7;y2++)
{z2=putqueen(x2,y2,x1,y1);
if(z2==2)
{for(x3=0;x3<=7;x3++)
for(y3=0;y3<=7;y3++)
{z3=putqueen(x3,y3,x2,y2);
if(z3==2)
{for(x4=0;x4<=7;x4++)
for(y4=0;y4<=7;y4++)
{z4=putqueen(x4,y4,x3,y3);
if(z4==2)
{for(x5=0;x5<=7;x5++)
for(y5=0;y5<=7;y5++)
{z5=putqueen(x5,y5,x4,y4);
if(z5==2)
{for(x6=0;x6<=7;x6++)
for(y6=0;y6<=7;y6++)
{z6=putqueen(x6,y6,x5,y5);
if(z6==2)
{for(x7=0;x7<=7;x7++)
for(y7=0;y7<=7;y7++)
{z7=putqueen(x7,y7,x6,y6);
if(z7==2)
{for(x8=0;x8<=7;x8++)
for(y8=0;y8<=7;y8++)
{z8=putqueen(x8,y8,x7,y7);
if(z8==3)
continue;
else
continue;
}
}
else
continue;}
}
else
continue;}
}
else
continue;}
}
else
continue;}
}
else
continue;}
}
else
continue;}
}
else
continue;}
getchar();
return 0;
}
int count(int i,int j)
{
int a,b,c=0,d;
for(a=0;a<=7;a++)
for(b=0;b<=7;b++)
{
for(d=1;d<=8;d++)
{if((chessboard[a][b]!=1)&&(chessboard[a][b]!=100)&&(a==i-d)&&(b==j-d))
c+=1;
else
if((chessboard[a][b]!=1)&&(chessboard[a][b]!=100)&&(a==i+d)&&(b==j-d))
c+=1;
else
if((chessboard[a][b]!=1)&&(chessboard[a][b]!=100)&&(a==i-d)&&(b==j+d))
c+=1;
else
if((chessboard[a][b]!=1)&&(chessboard[a][b]!=100)&&(a==i+d)&&(b==j+d))
c+=1;
else
continue;
}
if((chessboard[a][b]!=1)&&(chessboard[a][b]!=100)&&((a==i)||(b==j)))
c+=1;
else
continue;
}
return c;
}
int findmin()
{
int e,f,min=1000;
for(e=0;e<=7;e++)
for(f=0;f<=7;f++)
{if((chessboard[e][f]!=1)&&(chessboard[e][f]!=0)&&(chessboard[e][f]<min))
min=chessboard[e][f];
else
continue;
}
if(min==1000)
return -1;
else
return min;
}
int findminadd()
{
int g,h,k=0;
if(findmin()!=-1)
{for(g=0;g<=7;g++)
for(h=0;h<=7;h++)
{if(chessboard[g][h]==findmin())
k+=1;
else
continue;
}
return k;
}
else
return -1;
}
int setblock(int s1,int s2)
{
int sb1,sb2,sb3;
for(sb1=0;sb1<=7;sb1++)
for(sb2=0;sb2<=7;sb2++)
{if(chessboard[sb1][sb2]!=100)
{chessboard[s1][sb2]=1;
chessboard[sb1][s2]=1;}
for(sb3=1;sb3<=8;sb3++)
{
if(((sb1-sb3)>=0)&&((sb2-sb3)>=0))
chessboard[(sb1-sb3)][(sb2-sb3)]=1;
else if(((sb1+sb3)<=7)&&((sb2-sb3)>=0))
chessboard[(sb1+sb3)][(sb2-sb3)]=1;
else if(((sb1-sb3)>=0)&&((sb2+sb3)<=7))
chessboard[(sb1-sb3)][(sb2+sb3)]=1;
else if(((sb1+sb3)<=7)&&((sb2+sb3)<=7))
chessboard[(sb1+sb3)][(sb2+sb3)]=1;
else
continue;
}
}
return 0;
}
int putqueen(int pq1,int pq2,int pq3,int pq4)
{
static int queen=0;
int k=findminadd();
if((k>=(8-queen))&&(queen<8))
{ chessboard[pq1][pq2]=100;
setblock(pq1,pq2);
queen+=1;
return 2;}
if(queen==8)
{printchessboard();
countsolnumber+=1;
queen=0;
return 3;}
if((k<queen)&&(queen!=8))
{ removequeen(pq3,pq4);
queen-=1;
return 4;}
}
void removequeen(int rq1,int rq2)
{
chessboard[rq1][rq2]=0;
int m,n,o;
for(m=0;m<=7;m++)
for(n=0;n<=7;n++)
{ chessboard[rq1][m]=0;
chessboard[n][rq2]=0;
for(o=1;o<=8;o++)
{
if(((rq1-o)>=0)&&((rq2-o)>=0))
chessboard[(rq1-o)][(rq2-o)]=0;
else if(((rq1+o)<=7)&&((rq2-o)>=0))
chessboard[(rq1+o)][(rq2-o)]=0;
else if(((rq1-o)>=0)&&((rq2+o)<=7))
chessboard[(rq1-o)][(rq2+o)]=0;
else if(((rq1+o)<=7)&&((rq2+o)<=7))
chessboard[(rq1+o)][(rq2+o)]=0;
else
continue;
}
}
setchessboard();
}
void printchessboard()
{
cout<<"--"<<endl;
cout<<"第"<<countsolnumber<<"組解\n\n";
cout<<"皇后的位置\n"<<"1.以坐標表示為:"<<endl;
int p0,p1,p2,p3;
for(p1=0;p1<=7;p1++)
for(p2=0;p2<=7;p2++)
{
if(chessboard[p1][p2]==100)
cout<<"<"<<p1<<","<<p2<<">\t";
}
cout<<endl;
cout<<"2.以圖形表示:"<<endl;
cout<<" ";
for(p0=0;p0<=7;p0++)
{cout<<p0<<" ";
}
cout<<endl;
for(p1=0;p1<=7;p1++)
for(p2=0;p2<=7;p2++)
{
if((p2==0)&&(p2!=7)&&(chessboard[p1][7]!=100))
cout<<p1<<" ";
else if(((p2==0)&&p2!=7)&&(chessboard[p1][7]==100))
cout<<p1<<" Q ";
else if ((p2!=0)&&(p2==7)&&(chessboard[p1][7]!=100))
cout<<" "<<endl;
else if ((p2!=0)&&(p2==7)&&(chessboard[p1][7]==100))
cout<<"Q"<<endl;
else if ((p2!=0)&&(p2!=7)&&(chessboard[p1][7]==100))
cout<<"Q ";
else
cout<<" ";
}
}
void resetchessboard()
{
int re1,re2;
for(re1=0;re1<=7;re1++)
for(re2=0;re2<=7;re2++)
{chessboard[re1][re2]=0;
}
setchessboard();
}
void setchessboard()
{
int set1,set2;
for(set1=0;set1<=7;set1++)
for(set2=0;set2<=7;set2++)
{chessboard[set1][set2]=count(set1,set2);
}
}
補充說明:
說一下我的想法
1.假設皇后放在一個格子之中,則他可以吃到的格子數目全部加起來
寫在那一個格子裡頭,然後去求所有格子的最小值,把皇后放在那個最小值裡面
然後把它可以吃掉的格子設為1,再去求剩下不能吃掉的格子的最小值,
然後再放第二個皇后,以此類推下去,放皇后的方法就是那些是最小值的格子
一個一個去試,如果走到死路的話,就把最後一次放皇后的格子的格子的皇后拿掉
,重新設定最小值,再把皇后放在下一個格子,一直到找到解為止
2. chseeboard[][]是棋盤,設為8*8的陣列
3.函式的功能
a.resetchessboard()
就是把整個棋盤設為初始的狀態,也就是完全沒放皇后的狀態
b.setchessboard()
設定棋盤,就是把格子的最小值放上去
c.count()
找出格子的值
d.findmin()
找出格子最小值
e.findminadd()
找出格子是最小值的個數
f.setblock()
就是把皇后放上去之後,把會被皇后吃掉的格子都設為1
g.putqueen()
把皇后放上去,或是判斷是否找到解,或是判斷是不是要移除皇后的地方
h.removequeen()
把皇后移掉,被移掉的格子汗如果皇后在那個格子的話會被吃掉的格子都設0
i.prinntchessboard()
就是把解印出來的函式
4.題目出處:
C++: How to program, 6/e
中文版叫做:c++程式設計的藝術 第五版,題目一樣
7.34題
麻煩各位大大解答了
小弟我已經熬夜兩天debug不出來...
感激不盡...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.211.19
※ 編輯: pocernld 來自: 140.112.211.19 (12/12 02:23)
推
12/12 02:45, , 1F
12/12 02:45, 1F
→
12/12 03:32, , 2F
12/12 03:32, 2F
推
12/12 04:08, , 3F
12/12 04:08, 3F
推
12/12 04:22, , 4F
12/12 04:22, 4F
推
12/12 05:33, , 5F
12/12 05:33, 5F
推
12/12 05:36, , 6F
12/12 05:36, 6F
推
12/12 05:45, , 7F
12/12 05:45, 7F
推
12/12 05:50, , 8F
12/12 05:50, 8F
推
12/12 05:55, , 9F
12/12 05:55, 9F
推
12/12 06:15, , 10F
12/12 06:15, 10F
→
12/12 07:03, , 11F
12/12 07:03, 11F
推
12/12 07:57, , 12F
12/12 07:57, 12F
推
12/12 08:02, , 13F
12/12 08:02, 13F
→
12/12 08:40, , 14F
12/12 08:40, 14F
推
12/12 10:09, , 15F
12/12 10:09, 15F
→
12/12 10:10, , 16F
12/12 10:10, 16F
→
12/12 10:48, , 17F
12/12 10:48, 17F
→
12/12 10:49, , 18F
12/12 10:49, 18F
推
12/12 10:50, , 19F
12/12 10:50, 19F
→
12/12 10:53, , 20F
12/12 10:53, 20F
推
12/12 10:53, , 21F
12/12 10:53, 21F
推
12/12 10:55, , 22F
12/12 10:55, 22F
推
12/12 11:10, , 23F
12/12 11:10, 23F
推
12/12 11:18, , 24F
12/12 11:18, 24F
→
12/12 11:21, , 25F
12/12 11:21, 25F
推
12/12 11:59, , 26F
12/12 11:59, 26F
推
12/12 12:03, , 27F
12/12 12:03, 27F
推
12/12 12:22, , 28F
12/12 12:22, 28F
→
12/12 12:31, , 29F
12/12 12:31, 29F
推
12/12 12:43, , 30F
12/12 12:43, 30F
推
12/12 13:07, , 31F
12/12 13:07, 31F
推
12/12 13:57, , 32F
12/12 13:57, 32F
推
12/12 14:04, , 33F
12/12 14:04, 33F
推
12/12 14:28, , 34F
12/12 14:28, 34F
→
12/12 15:30, , 35F
12/12 15:30, 35F
推
12/12 16:18, , 36F
12/12 16:18, 36F
→
12/12 17:37, , 37F
12/12 17:37, 37F
→
12/12 17:38, , 38F
12/12 17:38, 38F
→
12/12 17:56, , 39F
12/12 17:56, 39F
→
12/12 17:56, , 40F
12/12 17:56, 40F
→
12/12 18:12, , 41F
12/12 18:12, 41F
推
12/12 18:12, , 42F
12/12 18:12, 42F
推
12/12 18:36, , 43F
12/12 18:36, 43F
推
12/12 18:47, , 44F
12/12 18:47, 44F
→
12/12 19:47, , 45F
12/12 19:47, 45F
→
12/12 20:12, , 46F
12/12 20:12, 46F
推
12/12 20:48, , 47F
12/12 20:48, 47F
推
12/12 22:18, , 48F
12/12 22:18, 48F
推
12/12 22:27, , 49F
12/12 22:27, 49F
推
12/12 22:46, , 50F
12/12 22:46, 50F
推
12/12 23:20, , 51F
12/12 23:20, 51F
推
12/12 23:25, , 52F
12/12 23:25, 52F
推
12/12 23:31, , 53F
12/12 23:31, 53F
推
12/12 23:54, , 54F
12/12 23:54, 54F
推
12/13 00:09, , 55F
12/13 00:09, 55F
推
12/13 00:26, , 56F
12/13 00:26, 56F
推
12/13 00:58, , 57F
12/13 00:58, 57F
推
12/13 01:52, , 58F
12/13 01:52, 58F
推
12/13 08:11, , 59F
12/13 08:11, 59F
推
12/13 08:12, , 60F
12/13 08:12, 60F
→
12/13 08:12, , 61F
12/13 08:12, 61F
推
12/13 10:29, , 62F
12/13 10:29, 62F
推
12/13 10:53, , 63F
12/13 10:53, 63F
→
12/13 11:24, , 64F
12/13 11:24, 64F
推
12/13 11:25, , 65F
12/13 11:25, 65F
推
12/13 11:29, , 66F
12/13 11:29, 66F
推
12/13 12:35, , 67F
12/13 12:35, 67F
推
12/13 13:08, , 68F
12/13 13:08, 68F
推
12/13 13:15, , 69F
12/13 13:15, 69F
推
12/13 13:59, , 70F
12/13 13:59, 70F
→
12/13 13:59, , 71F
12/13 13:59, 71F
推
12/13 16:34, , 72F
12/13 16:34, 72F
推
12/13 17:44, , 73F
12/13 17:44, 73F
推
12/13 18:42, , 74F
12/13 18:42, 74F
推
12/13 23:37, , 75F
12/13 23:37, 75F
推
12/14 00:52, , 76F
12/14 00:52, 76F
推
12/14 00:59, , 77F
12/14 00:59, 77F
推
12/14 06:23, , 78F
12/14 06:23, 78F
→
12/14 06:23, , 79F
12/14 06:23, 79F
推
12/14 19:34, , 80F
12/14 19:34, 80F
※ oxoxpeter:轉錄至看板 StupidClown 12/14 20:21
推
12/14 21:45, , 81F
12/14 21:45, 81F
推
12/14 22:21, , 82F
12/14 22:21, 82F
推
12/14 23:17, , 83F
12/14 23:17, 83F
推
12/15 01:08, , 84F
12/15 01:08, 84F
→
12/15 01:08, , 85F
12/15 01:08, 85F
→
12/15 01:09, , 86F
12/15 01:09, 86F
→
12/15 01:09, , 87F
12/15 01:09, 87F
→
12/15 01:18, , 88F
12/15 01:18, 88F
推
02/15 23:16, , 89F
02/15 23:16, 89F
推
03/05 17:45, , 90F
03/05 17:45, 90F
推
03/15 22:28, , 91F
03/15 22:28, 91F
→
03/16 20:03, , 92F
03/16 20:03, 92F
推
05/18 14:31, , 93F
05/18 14:31, 93F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章