您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 八皇后问题
  所属分类: Java
  开发工具:
  文件大小: 9kb
  下载次数: 0
  上传时间: 2006-02-23
  提 供 者: che***
 详细说明: package bia.arithmetic; import java.util.Date; /** * @author Administrator * * To change this generated comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class EightQueen { Queen[] stack = new Queen[8]; int sp = 0; int num = 0; public EightQueen() { num = 8; stack = new Queen[num]; sp = 0; } public EightQueen(int num) { this.num = num; stack = new Queen[num]; sp = 0; } /** * 打印皇后的坐标列表。 * @renzc * */ public void list() { System.out.println("Begin list the stack Point:&quo t;); for (int i = 0; i < sp; i++) { stack[i].pos.println(); } System.out.println("End list the stack Point:"); } /** * 主算法流程。 * @Administrator * */ public void calc() { sp = 0; stack[sp++] = new Queen(); while (sp >= 0 && sp <= num - 1) { Queen queen = getQueen(sp); if (null == queen) { boolean flag = true; while (flag) { --sp; if (sp < 0) break; if (stack[sp].pos.y == num - 1) { } else { stack[sp++].pos.y++; flag = false; for (int k = 0; k < sp - 1; k++) { if (stack[k].isUnderControl(stack[sp - 1].pos)) { flag = true; break; } } } } } else { stack[sp++] = queen; } } } public Queen getQueen(int x) { boolean flag = true; int y = 0; while (flag) { flag = false; for (int i = 0; i < x; i++) { if (stack[i].isUnderControl(new Point(x, y))) { flag = true; break; } } if (flag && y <= num - 1) { y++; } else if (y >= num) { return null; } } return new Queen(new Point(x, y)); } public static void main(String[] args) { EightQueen a = new EightQueen(20); long start = new Date().getTime(); System.out.println("起始时间:[" + start + "]"); a.calc(); long end = new Date().getTime(); System.out.println("截止时间:[" + end + "]"); System.out.println("共耗时:[" + (end - start) + "]毫秒"); if (a.sp > 0) { a.list(); } else { System.out.println("这个题目无解!"); } } } class Point { int x, y; public void println() { System.out.println("The Point is [x,y]=[" + x + "," + y + "]"); } public Point() { x = 0; y = 0; } public Point(int x, int y) { this.x = x; this.y = y; } /** * @return int */ public int getX() { return x; } /** * @return int */ public int getY() { return y; } /** * Sets the x. * @param x The x to set */ public void setX(int x) { this.x = x; } /** * Sets the y. * @param y The y to set */ public void setY(int y) { this.y = y; } } class Queen { Point pos; public Queen() { pos = new Point(); } public Queen(Point pos) { this.pos = pos; } public boolean isUnderControl(Point point) { boolean ret = true; if (point.x != pos.x && point.y != pos.y && Math.abs(point.x - pos.x) != Math.abs(point.y - pos.y) && Math.abs(point.x + point.y) != Math.abs(pos.x + pos.y)) { ret = false; } return ret; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: j2me 控件 源码 资源
 输入关键字,在本站1000多万海量源码库中尽情搜索: