在国际象棋的棋盘上,放置8个皇后,如何放才能使得这8个皇后无论横看竖看斜看都不在同一条直线上.
解决方案 »
- Windows Server 2003 Enterprise R2 - CD2 - 英文版本的序列号
- Image绘制图像的问题
- java在线等:我想问一下这段游戏代码是怎么样实现蛇头不断移动(用哪些代码实现),还有就是蛇吃到食物后蛇头和蛇身怎么变换,怎么实现蛇身加1??(完整代码在附件中)
- 小问题
- 求教一些关于JTable的知识
- 请教
- JET不是可以生成EXE后脱离JVM的吗?怎么我用了它搞的EXE在其它机上就运行不出来呢?
- 新手求救!请问怎样在函数中中断该函数??
- ant的问题
- JDK1.3 JDK1.4的问题
- Cannot initialize context because there is already a root application
- 刚刚开始学java 请高手指点一下 我该怎么做
o o x o o o o o
o o o o x o o o
o o o o o o x o
o x o o o o o o
o o o x o o o o
o o o o o x o o
o o o o o o o x
代码:
//Queens.java
//国际象棋8个皇后,使得8个皇后无论横看竖看斜看都不在同一条直线上
import javax.swing.*;
public class Queens
{
int[] a =new int[8];
int[] b =new int[15];
int[] c =new int[15];
int[][] Queen=new int[8][8];
void next(int i)
{
for(int j=0;j<8;j++)
{
if(a[j]==0&&b[i+j]==0&&c[i-j+7]==0)
{
a[j]=b[i+j]=c[i-j+7]=1;
Queen[i][j]=1;
if(i<7)
next(i+1);
else
{
String output=new String();
for(int m=0;m<8;m++)
{
for(int n=0;n<8;n++)
output+=" "+Queen[m][n]+" ";
output+="\n";
}
JTextArea outputArea=new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null,outputArea,"One possible distribution",JOptionPane.INFORMATION_MESSAGE);
}
a[j]=b[i+j]=c[i-j+7]=Queen[i][j]=0;
}
}
}
public static void main(String args[])
{
Queens one=new Queens();
one.next(0);
System.exit(0);
}
}其他算法>>
百度百科:http://baike.baidu.com/view/622604.htm--4楼 只需百度一下就可得40分,你都不去.还散分?