八皇后 国际象棋

本文介绍了一种通过随机放置皇后并检查对角线冲突来解决八皇后问题的方法。该方法不断尝试新的布局直到找到所有互不冲突的解决方案,并将这些方案保存下来。涉及随机数生成、二维数组操作等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个算起来很快只有92种放法


 


package 难题.八皇后;

import java.util.Random;

public class TryRun {

 Random r = new Random();

 static int df[][][] = new int[200][10][10];

 static int dfIndex = 0;


 /**
  * @param args
  */
 public static void main(String[] args) {
  while (true) {
   Map.mapSet();
   // Map.showMaps();
   Queens Q = new Queens();
   // for (int i = 0; i <
   // Q.queensXY.length; i++) {
   // for (int j = 0; j <
   // Q.queensXY[i].length;
   // j++) {
   // System.out.print(Q.queensXY[i][j]
   // + " ");
   // }
   // System.out.println();
   // }
   writeMap(Q.queensXY);
   if (Q.scanDiagonal()) {
    // Map.showMaps();
    saveArrs(Map.map);
    // break;
    // System.out.println("不同");
   }
   // else {
   // Map.showMaps();
   // System.out.println("同了");
   // break;
   // }
  }
 }


 static void writeMap(int[][] queensXY) {
  for (int i = 0; i < 8; i++) {
   Map.map[queensXY[0][i]][queensXY[1][i]] = 1;
  }
 }


 static void saveArrs(int[][] map) {
  if (dfIndex == 0) {
   copyArr(map);
   return;
  }
  if (checkSame(map)) {
   return;
  }
  copyArr(map);
 }


 static void copyArr(int[][] map) {
  for (int m = 0; m < map.length; m++) {
   for (int n = 0; n < map[m].length; n++) {
    df[dfIndex][m][n] = map[m][n];
   }
  }
  Map.showMaps();
  System.out.println("第" + (dfIndex + 1) + "种");
  dfIndex++;
 }


 static boolean checkSame(int[][] map) {
  boolean same = false;
  checkSame: for (int k = 0; k < dfIndex; k++) {
   for (int i = 0; i < map.length; i++) {
    for (int j = 0; j < map[i].length; j++) {
     if (map[i][j] != df[k][i][j]) {
      same = false;
      continue checkSame;
     }
     same = true;
    }
   }
   if (same) {
    return true;
   }
  }
  return false;
 }
 /*
  * static boolean scanRow() { // 同行
  *
  * for (int i = 1; i < 9; i++) { int
  * count = 0; for (int j = 1; j < 9;
  * j++)
  * { if(Map.map[i][j]==1){ count++;
  * } if(count==2){ return false; } }
  * }
  *
  * // 同列
  *
  * for (int i = 1; i < 9; i++) { int
  * count = 0; for (int j = 1; j < 9;
  * j++)
  * { if(Map.map[j][i]==1){ count++;
  * } if(count==2){ return false; } }
  * }
  *
  * // 斜线 /方向
  *//**
  *
  for (int i = 0; i < 15; i++) {
  * int count = 0; for (int j = 1; j
  * < i /
  * 2 + 1; j++) { if (Map.map[8 - i /
  * 2 + j][j] == 1) { count++; } if
  * (count
  * == 2) { //
  * System.out.println("A 同"); return
  * false; } } }
  */
 /*
  * // 斜线 /方向
  *
  * for (int i = 0; i < 15; i++) {
  * int count = 0; for (int j = 1; j
  * < i / 2 +
  * 1; j++) { if (Map.map[j][8 - i /
  * 2 + j] == 1) { count++; } if
  * (count ==
  * 2) { //
  * System.out.println("B 同"); return
  * false; } } }
  *
  * // for (int i = 1; i < 9; i++) {
  * // if (Map.map[i][i] == 1) { //
  * count++;
  * // } // if (count == 2) { //
  * System.out.println("斜线有双"); //
  * return false;
  * // } // }
  *
  * return true; }
  */
}


package 难题.八皇后;

final class ScanRow {
 boolean scanDiagonal() {
  // 斜线 /方向 左
  int x = 0;
  int y = 0;
  int[][] arr = new int[8][8];

  int k = 8;

  for (int i = 0; i < k; i++) {
   int count = 0;
   if (arr[y][i] == 1) {
    count++;
   }
   if (count == 2) {
    return false;
   }
   y++;
   k--;
  }
  x = 1;
  y = 0;
  for (int i = 0; i < k - 1; i++) {
   int count = 0;
   if (arr[i][x] == 1) {
    count++;
   }
   if (count == 2) {
    return false;
   }
   k--;
   x++;
  }

  // //////
  return true;
 }
}


package 难题.八皇后;

import java.util.Random;

public final class Queens {
 int[][] xy = new int[2][8];
 int[][] queensXY = new int[2][8];
 int numQueenX;
 int numQueenY;
 Random r = new Random();

 Queens() {
  for (int i = 0; i < this.xy.length; i++) {
   for (int j = 0; j < this.xy[i].length; j++) {
    this.xy[i][j] = j + 1;
   }
  }
  setQueenX();
  setQueenY();
 }

 void setQueenX() {
  int Rdx = this.r.nextInt(this.xy[0].length);
  if (this.xy[0][Rdx] == 0) {
   setQueenX();
  } else {
   this.queensXY[0][this.numQueenX] = this.xy[0][Rdx];
   this.numQueenX++;
   this.xy[0][Rdx] = 0;
  }
  if (this.numQueenX != 8) {
   setQueenX();
  }
 }

 void setQueenY() {
  int Rdy = this.r.nextInt(this.xy[1].length);
  if (this.xy[1][Rdy] == 0) {
   setQueenY();
  } else {
   this.queensXY[1][this.numQueenY] = this.xy[1][Rdy];
   this.numQueenY++;
   this.xy[1][Rdy] = 0;
  }
  if (this.numQueenY != 8) {
   setQueenY();
  }
 }

 boolean scanDiagonal() {
  // 斜线 /方向 左

  for (int k = 0; k < 8; k++) {
   int count = 0;

   for (int x = 0, y = k; x < 8 - k; x++, y++) {
    if (Map.map[y + 1][x + 1] == 1) {
     count++;
    }
    if (count == 2) {
     // System.out.println("测  斜1左");
     return false;
    }
   }

  }
  // 斜线 /方向 右
  for (int k = 0; k < 7; k++) {
   int count = 0;

   for (int x = k + 1, y = 0; y < 8 - k - 1; x++, y++) {
    if (Map.map[y + 1][x + 1] == 1) {
     count++;
    }
    if (count == 2) {
     // System.out.println("测  斜1右");
     return false;
    }
   }

  }
  // 斜线 /方向 左

  for (int k = 7; k >= 0; k--) {
   int count = 0;

   for (int x = k, y = 0; x >= 0; x--, y++) {
    if (Map.map[y + 1][x + 1] == 1) {
     count++;
    }
    if (count == 2) {
     // System.out.println("测  斜2左");
     return false;
    }
   }

  }

  // 斜线 /方向 右

  for (int k = 0; k < 7; k++) {
   int count = 0;

   for (int x = 7, y = k + 1; y < 8; x--, y++) {
    if (Map.map[y + 1][x + 1] == 1) {
     count++;
    }
    if (count == 2) {
     // System.out.println("测  斜2右");
     return false;
    }
   }

  }

  // //////
  return true;
 }
}


package 难题.八皇后;


public class Map {
 static int map[][] = new int[10][10];
 static int hight=map.length;
 static int weight=map[0].length;

 static void mapSet() {

  for (int i = 0; i < map.length; i++) {
   for (int j = 0; j < map[i].length; j++) {
    // 边界
    if (i == 0 || i == map.length - 1 || j == 0
      || j == map[i].length - 1) {
     map[i][j] = 100;
     // 低级怪物
    } else{
     map[i][j]=0;
    }
   }
  }
 }

 static void showMaps() {
  for (int i = 0; i < map.length; i++) {
   for (int j = 0; j < map[i].length; j++) {
    if (map[i][j] == 100) {
     System.out.print("* ");
    }
    else if (map[i][j] == 1) {
     System.out.print("& ");
    }
    else {
     String step="` ";
//     step=step+(" "+Integer.toString(map[i][j]));
//     if(step.length()<3){
//      step+=" ";
//     }
     System.out.print(step);
    }
   }
   System.out.println();
  }
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值