为什么 运行不出来
#include
int chess[8][8];
int count = 0;
int judge(int r,int c)
{
int i;
int j;
for(i=0;i<8;i++)
{
if(chess[i][c])
return 0;
}
for(i=r,j=c;i>=0&&j>=0;i--,j--)
{
if(chess [i][j])
return 0;
}
for(i=r,j=c;i>=0&&j<8;i--,j++)
{
if(chess[i][j])
return 0;
}
return 1;
}
void eightQueen()
{
int stack[10];
int top = 0;
int i,k;
while(top>=0)
for(;stack[top]<8; stack[top]++)
{
k = stack[top];
if(judge(top,k))
{
chess[top][k] = 1;
break;
}
}
if (top == 7 && stack[top]<8)
{
count++;
stack[top]++;
}
else if(stack[top] < 8 && top <7)
{
top++;
}
else if (stack[top] == 8 && top <=7)
{
for (i=0;i<8;i++)
{
chess[top][i] = 0;
chess[top-1][i] = 0;
}
stack[top--] = 0;
stack[top]++;
}
}
void main()
{
int i ;
int j ;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chess[i][j] = 0;
}
}
eightQueen();
printf("八皇后的个数%d",count);
}
#include <stdio.h>
#include <stdlib.h>
#define N 8
int column[N+1];//同栏是否有皇后
int rup[2*N +1];//右上至左下是否有皇后
int lup[2*N +1];//左上至右下是否有皇后
int queen[N+1] = {0};
int num ;//解答编号
void backtrack(int);//递回求解
main(void)
{
int i;
num = 0;
for(i=1;i<=N;i++)
column[i] = 1;
for(i=1;i<=2*N;i++)
rup[i] = lup[i] = 1;
backtrack(1);
system("pause");
return 0;
}
void showAnswer()
{
int x,y;
printf("\n 解答 %d\n",++num);
for (y=1;y<=N;y++)
{
for (x=1;x<=N;x++)
{
if(queen[y] == x)
printf("Q");
else
printf("K");
}
printf("\n");
}
}
void backtrack(int i)
{
int j;
if(i > N)
showAnswer();
else
for (j=1;j<=N;j++)
{
if(column[j] == 1 && rup[i+j] == 1 &&lup[i-j+N] == 1){
queen[i] = j;
//设定为占用
column[j] = rup[i+j] = lup[i-j+N] = 0;
backtrack(i+1);
column[j] = rup[i+j] = lup[i-j+N] = 1; //在左下角递归完后进行下一个位置的递归运算
}
}
}
亲测没毛病,望采纳!!!
#include <stdio.h>
#include <stdlib.h>
#define N 8
int column[N+1];//同栏是否有皇后
int rup[2*N +1];//右上至左下是否有皇后
int lup[2*N +1];//左上至右下是否有皇后
int queen[N+1] = {0};
int num ;//解答编号
void backtrack(int);//递回求解
main(void)
{
int i;
num = 0;
for(i=1;i<=N;i++)
column[i] = 1;
for(i=1;i<=2*N;i++)
rup[i] = lup[i] = 1;
backtrack(1);
system("pause");
return 0;
}
void showAnswer()
{
int x,y;
printf("\n 解答 %d\n",++num);
for (y=1;y<=N;y++)
{
for (x=1;x<=N;x++)
{
if(queen[y] == x)
printf("Q");
else
printf("K");
}
printf("\n");
}
}
void backtrack(int i)
{
int j;
if(i > N)
showAnswer();
else
for (j=1;j<=N;j++)
{
if(column[j] == 1 && rup[i+j] == 1 &&lup[i-j+N] == 1){
queen[i] = j;
//设定为占用
column[j] = rup[i+j] = lup[i-j+N] = 0;
backtrack(i+1);
column[j] = rup[i+j] = lup[i-j+N] = 1; //在左下角递归完后进行下一个位置的递归运算
}
}
}
//非递归、只用一个一维数组
#include "stdio.h"
#define TRUE 1
#define FALSE 0
char sign;
int b[8];
outputresult (int b[8])
{
int i;
for (i = 1; i <= 8; i++)
printf(" ( %d,%d) ",b[i - 1],i);
printf("\n");
}
int main()
{
int i,n,k;
char c;
n = 0;
for (i = 1; i <= 8; i++)
b[i - 1] = 0;
k = 1;
do {
b[k - 1] = (b[k - 1] + 1) % 9;
if (b[k - 1] == 0)
k = k - 1;
else {
sign = TRUE;
for (i = 1; i <= k - 1; i++)
sign = sign && (b[i - 1] != b[k - 1]) &&
(k-i != abs(b[k - 1] - b[i - 1]));
if (sign)
if (k == 8)
{ /*output*/
outputresult(b);
n = n + 1;
if (n % 22 == 0)
{
printf("Press Enter to continue!\n");
gets(c);
}
} /*End output*/
else
k = k + 1;
}
} while (!(k == 0));
printf("There are %d kind of conditions in total.\n",n);
return 0;
}
/*b[i]: The number of the line which the queen lies in. */
/*i : The number of the column which the queen lies in. */
不知道原来的代码是不是这样的
几个问题
while循环没有加大括号
stack是在函数内定义的,要全部初始化为0
这样改一改至少应该可以运行出结果吧
如果结果不对还需要继续调试
改了我说的那几个地方,仅供参考
#include <cstdio>
#include <cstring>
int chess[8][8];
int count = 0;
int judge(int r,int c)
{
int i;
int j;
for(i=0;i<8;i++)
{
if(chess[i][c])
return 0;
}
for(i=r,j=c;i>=0&&j>=0;i--,j--)
{
if(chess [i][j])
return 0;
}
for(i=r,j=c;i>=0&&j<8;i--,j++)
{
if(chess[i][j])
return 0;
}
return 1;
}
void eightQueen()
{
int stack[10];
memset(stack, 0, sizeof(stack));
int top = 0;
int i,k;
while(top>=0) {
for(;stack[top]<8; stack[top]++)
{
k = stack[top];
if(judge(top,k))
{
chess[top][k] = 1;
break;
}
}
if (top == 7 && stack[top]<8)
{
count++;
stack[top]++;
}
else if(stack[top] < 8 && top <7)
{
top++;
}
else if (stack[top] == 8 && top <=7)
{
for (i=0;i<8;i++)
{
chess[top][i] = 0;
chess[top-1][i] = 0;
}
stack[top--] = 0;
if (top >= 0)
stack[top]++;
}
}
}
int main()
{
int i ;
int j ;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chess[i][j] = 0;
}
}
eightQueen();
printf("%d", count);
}
只有20行!,n皇后,,,把n值赋成8即可。
有看不懂的地方可以继续问我!!!
//c[i]:第i行的皇后放在第几列
#include<iostream>
using namespace std;
int n, c[20], ans;
void dfs(int cur){
if(cur > n)ans++;
else for(int i = 1; i <= n; i++){
int ok = 1;
for(int j = 1; j < cur; j++)
if(c[j]==i || c[j]-j==i-cur || c[j]+j==i+cur)
{ ok = 0; break; }
if(ok){
c[cur] = i;
dfs(cur+1);
}
}
}
int main(){
cin>>n;
dfs(1);
cout<<ans<<"\n";
return 0;
}