⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j_queen.java

📁 用JAVA实现的一个解决八皇后问题的小程序
💻 JAVA
字号:
//J_Queen.java
//Creaed by Zhou Chuanle
//Readme
//This file will solve the Queen problem

import java.io.*;

class Queen
{
private int [] y;

public Queen()
{
	y=new int[8];
}//End of method Queen

private void OutputLine(BufferedWriter out)
{
	//System.out.println("+--+--+--+--+--+--+--+--+");
	try
	{
		
		out.write("+--+--+--+--+--+--+--+--+");
		out.newLine();
	}
	catch(IOException e)
	{
		System.out.println(e);	
	}//End of try-catch
}//End of method OutputLine

private void OutputLine(BufferedWriter out,int n)
{
	int i;
	/*for(i=0;i<8;i++)
		if(i!=y[n])System.out.print("|  ");
		else System.out.print("|Q ");
	System.out.println("|");*/
	try
	{
		for(i=0;i<8;i++)
			if(i!=y[n])out.write("|  ");
			else out.write("|Q ");
		out.write("|");
		out.newLine();
	}
	catch(IOException e)
	{
		System.out.println(e);	
	}
}//End of method OutputLine

public void Output(BufferedWriter out)
{
	
	int i;
	OutputLine(out);
	for(i=0;i<8;i++)
	{
		OutputLine(out,i);
		OutputLine(out);
	}
	
		
	/*try
	{	
	}
	catch(IOException e)
	{
		System.out.println(e);	
	}//End of try-catch*/
	
}//End of method Output

private boolean CheckLine()
{
int i,j=0;
int [] line=new int[8];
for(i=0;i<y.length;i++)
	line[y[i]]=1;
for(i=0;i<line.length;i++)
	j+=line[i];
if(j!=8)return(false);
return(true);
}//End of method CheckLine

private boolean CheckQueen()
{
if(!CheckLine())return(false);
int i,j=0,k=0;
int [] plus=new int[15];
int [] subtract=new int[15];
for(i=0;i<y.length;i++)
{	
	plus[y[i]+i]=1;
	subtract[y[i]-i+7]=1;
}
for(i=0;i<plus.length;i++)
{
	j+=plus[i];
	k+=subtract[i];
}
if(j!=8)return(false);
if(k!=8)return(false);
return(true);
}//End of method CheckQueen

private String ChangetoString(int n)
{
	StringBuffer s=new StringBuffer(0);
	s.append(n);
	return(s.toString());	
}

public void OutputQueen()
{
	try
	{
		BufferedWriter out=new BufferedWriter(new FileWriter("Queen.txt"));
		int i=0;
		for(y[0]=0;y[0]<8;y[0]++)
		for(y[1]=0;y[1]<8;y[1]++)
		for(y[2]=0;y[2]<8;y[2]++)
		for(y[3]=0;y[3]<8;y[3]++)
		for(y[4]=0;y[4]<8;y[4]++)
		for(y[5]=0;y[5]<8;y[5]++)
		for(y[6]=0;y[6]<8;y[6]++)
		for(y[7]=0;y[7]<8;y[7]++)
			if(CheckQueen())
			{
				i++;
				//System.out.println("result"+i+":");																
				out.write("result");
				out.write(ChangetoString(i));
				out.newLine();
				Output(out);
			
	  		}
	  	out.close();
	  }
	catch(IOException e)
	{
		System.out.println(e);	
	}//End of try-catch
}//End of method OutputQueen
}//End of class Queen

public class J_Queen
{
public static void main(String args[])
{
	Queen q=new Queen();
	q.OutputQueen();	
}//End of method main
}//End of class J_Queen

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -