/** 八皇后问题例
*/import java.io.*;public class Class1
{
public static void main (String[] args)
{
// TODO: Add initialization code here
System.out.println("Eight Queen Question");
System.out.println("Author :Xiaozhuang");
try{
System.in.read();
}catch(IOException ioe){
}
queen instance1 = new queen();
instance1.main();
try{
System.in.read();
}catch(IOException ioe){
}
System.out.println("Another instance ");
queen1 instance2 = new queen1();
instance2.main();
try{
System.in.read();
}catch(IOException ioe){
}
}
}
class queen
{
int a[][] = new int[8][8];
int ff(int m,int n,int k)
{
int i,j;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i--,j--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i--,j++)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); j--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); j++)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i++,j--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i++)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i++,j++)
a[i][j] += k;
return 0;
} public void main()
{
int i,j,m,n,sum=0;
for (i=0,j=0; i>=0; ) {
if (j==8)
for (j=0; j<8; j++)
if (a[i][j]==8) {
ff(i,j,-1);
j+=1;
break;
}
for ( ; j<8; j++)
if (a[i][j]==0) {
ff(i,j,1);
i+=2;
j=0;
break;
}
i--;
if (i==8) {
sum++;
System.out.println("");
System.out.print("-----------------");
for (m=0; m<8; m++) {
System.out.println("");
for (n=0; n<8; n++)
if (a[m][n]==8)
System.out.print(" 8");
else
System.out.print(" _");
}
j=8;
i--;
System.out.println("");
System.out.print("Sum = " + sum);
try{
System.in.read();
}catch(IOException ioe){
};
}
}
}
}/**另一解法
*/
class queen1
{
// #ifndef boolean
// enum boolean {false,true};
// #ifndef word
// typedef unsigned int word;
// boolean mybool;
// static int word;
boolean Col[] = new boolean[8];
boolean LeftCr[] = new boolean[14];
boolean RightCr[] = new boolean[14];
int Answer[] = new int[8];
int Count=0; /*Col{行标志定义}
Answer{列标志定义}
LeftCr{左斜对角线标志定义}
RightCr{右斜对角线标志定义}
{Count:解的个数纪录}
*/
//-------------------------判断一个位置是否可以置一个皇后-----------------------
boolean IfSafe(int x,int y)
{
return ((Col[y]==true)
|| (LeftCr[x-y+7]==true)
|| (RightCr[x+y]==true))?false:true;
}
//-----------------------------从当前位置删除一个皇后--------------------------
void CutQueen(int x,int y)
{
Col[y]=false;
LeftCr[x-y+7]=false;
RightCr[x+y]=false;
} //---------------------------------打印一组解----------------------------------
void PrintAnswer()
{
int CurAns;
System.out.println("");
System.out.print("COUNT");
System.out.println(++Count);
for (CurAns=0;CurAns<8;CurAns++)
System.out.print( Answer[CurAns]+1);
}
//---------------------------------置皇后(核心部分)----------------------------
void PutQueen(int x)
{
int y;
for (y=0;y<8;y++)
{
if (IfSafe(x,y))
{
Answer[x]=y;
Col[y]=true;
LeftCr[x-y+7]=true;
RightCr[x+y]=true;
if (x<7) PutQueen(x+1);
else PrintAnswer();
CutQueen(x,y);
}
}
} //--------------------主程序体--------------------------------------------
public int main()
{
for (int i=0;i<8;i++)
Answer[i]=0;
PutQueen(0);
return 0;
}}
*/import java.io.*;public class Class1
{
public static void main (String[] args)
{
// TODO: Add initialization code here
System.out.println("Eight Queen Question");
System.out.println("Author :Xiaozhuang");
try{
System.in.read();
}catch(IOException ioe){
}
queen instance1 = new queen();
instance1.main();
try{
System.in.read();
}catch(IOException ioe){
}
System.out.println("Another instance ");
queen1 instance2 = new queen1();
instance2.main();
try{
System.in.read();
}catch(IOException ioe){
}
}
}
class queen
{
int a[][] = new int[8][8];
int ff(int m,int n,int k)
{
int i,j;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i--,j--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i--,j++)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); j--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); j++)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i++,j--)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i++)
a[i][j] += k;
for (i=m,j=n; (i>=0)&&(j>=0)&&(i<8)&&(j<8); i++,j++)
a[i][j] += k;
return 0;
} public void main()
{
int i,j,m,n,sum=0;
for (i=0,j=0; i>=0; ) {
if (j==8)
for (j=0; j<8; j++)
if (a[i][j]==8) {
ff(i,j,-1);
j+=1;
break;
}
for ( ; j<8; j++)
if (a[i][j]==0) {
ff(i,j,1);
i+=2;
j=0;
break;
}
i--;
if (i==8) {
sum++;
System.out.println("");
System.out.print("-----------------");
for (m=0; m<8; m++) {
System.out.println("");
for (n=0; n<8; n++)
if (a[m][n]==8)
System.out.print(" 8");
else
System.out.print(" _");
}
j=8;
i--;
System.out.println("");
System.out.print("Sum = " + sum);
try{
System.in.read();
}catch(IOException ioe){
};
}
}
}
}/**另一解法
*/
class queen1
{
// #ifndef boolean
// enum boolean {false,true};
// #ifndef word
// typedef unsigned int word;
// boolean mybool;
// static int word;
boolean Col[] = new boolean[8];
boolean LeftCr[] = new boolean[14];
boolean RightCr[] = new boolean[14];
int Answer[] = new int[8];
int Count=0; /*Col{行标志定义}
Answer{列标志定义}
LeftCr{左斜对角线标志定义}
RightCr{右斜对角线标志定义}
{Count:解的个数纪录}
*/
//-------------------------判断一个位置是否可以置一个皇后-----------------------
boolean IfSafe(int x,int y)
{
return ((Col[y]==true)
|| (LeftCr[x-y+7]==true)
|| (RightCr[x+y]==true))?false:true;
}
//-----------------------------从当前位置删除一个皇后--------------------------
void CutQueen(int x,int y)
{
Col[y]=false;
LeftCr[x-y+7]=false;
RightCr[x+y]=false;
} //---------------------------------打印一组解----------------------------------
void PrintAnswer()
{
int CurAns;
System.out.println("");
System.out.print("COUNT");
System.out.println(++Count);
for (CurAns=0;CurAns<8;CurAns++)
System.out.print( Answer[CurAns]+1);
}
//---------------------------------置皇后(核心部分)----------------------------
void PutQueen(int x)
{
int y;
for (y=0;y<8;y++)
{
if (IfSafe(x,y))
{
Answer[x]=y;
Col[y]=true;
LeftCr[x-y+7]=true;
RightCr[x+y]=true;
if (x<7) PutQueen(x+1);
else PrintAnswer();
CutQueen(x,y);
}
}
} //--------------------主程序体--------------------------------------------
public int main()
{
for (int i=0;i<8;i++)
Answer[i]=0;
PutQueen(0);
return 0;
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货