鱼C论坛

 找回密码
 立即注册
查看: 4643|回复: 3

蓝桥杯-2n皇后问题

[复制链接]
发表于 2016-9-23 22:32:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 DAY 于 2016-11-30 22:15 编辑

在自己的eclipse编译器里面可以编译通过,并且答案与题目的相同,但提交后出现“运行错误”这是怎么回事?
  1. import java.util.Scanner;

  2. /*输入的第一行为一个整数n,表示棋盘的大小。
  3. 接下来n行,每行n个0或1的整数,如果一个整数为1,
  4. 表示对应的位置可以放皇后,如果一个整数为0,表示对应的位置不可以放皇后。*/

  5. public class Main {
  6.        
  7.         @SuppressWarnings("unused")
  8.         private static int step = 0 ;
  9.        
  10.         //初始化数组
  11.         @SuppressWarnings("resource")
  12.         private static void setArrays(int[][]a){
  13.                 Scanner sc = new Scanner(System.in) ;
  14.                
  15.                 for(int i =0 ;i < a.length ;++i){
  16.                         for(int j =0 ; j < a.length;++j){
  17.                                 a[i][j] = sc.nextInt() ;
  18.                         }
  19.                 }
  20.         }
  21.        
  22.         //打印数组
  23.         @SuppressWarnings("unused")
  24.         private static void printArrays(int[][]a){
  25.                
  26.                 for (int i = 0; i < a.length; i++) {
  27.                         for (int j = 0; j < a.length; j++) {
  28.                                 System.out.print(a[i][j]+" ");
  29.                         }
  30.                         System.out.println();
  31.                 }
  32.         }
  33.        
  34.         //判断i,j 位置是否合法
  35.         @SuppressWarnings("unused")
  36.         private static boolean legalWhiteQueen(int[][]a,final int i ,final int j) {
  37.                 for(int n = 0 ;n < i ; ++n){
  38.                         for(int m = 0 ; m < a.length ; ++m){
  39.                                 if(a[n][m] == 2){
  40.                                         if(Math.abs(i-n) == Math.abs(j- m) || j == m) return false;
  41.                                 }else continue ;
  42.                         }
  43.                 }
  44.                 return true;
  45.         }
  46.        
  47.         //判断i,j位置是否合法
  48.         @SuppressWarnings("unused")
  49.         private static boolean legalBlackQueen(int[][]a,final int i ,final int j) {
  50.                
  51.                 for(int n = 0 ; n < i ; n++){
  52.                        
  53.                         for(int m = 0 ; m < a.length ;++m){
  54.                                 if(a[n][m] == 3){
  55.                                         if(Math.abs(i-n) == Math.abs(j-m) || j == m)return false;
  56.                                 }
  57.                         }
  58.                 }
  59.                
  60.                 return true;
  61.         }
  62.        
  63.         //TODO 其次判断黑皇后
  64.         private static void blackQueen(int[][]a,int tag){
  65.                 if(tag == a.length){step++;/*System.out.println("step:" +step);printArrays(a);System.out.println();*/}
  66.                 else{
  67.                         for(int j = 0 ; j < a.length ; ++j){
  68.                                 if(a[tag][j] != 2 && a[tag][j] != 0 ){
  69.                                         a[tag][j] = 3;
  70.                                         if(legalBlackQueen(a, tag, j)){
  71.                                                 blackQueen(a, tag+1);
  72.                                         }
  73.                                         a[tag][j] = 1;
  74.                                 }
  75.                         }
  76.                 }
  77.         }
  78.        
  79.         // TODO 首先判断白皇后
  80.         @SuppressWarnings("unused")
  81.         private static void whiteQueen(int[][]a,int tag){
  82.                 if(tag == a.length)blackQueen(a, 0);
  83.                 else{
  84.                         for(int j = 0 ; j < a.length ; ++j){
  85.                                 if(a[tag][j] != 0){
  86.                                         a[tag][j] = 2;
  87.                                         if(legalWhiteQueen(a, tag, j)){
  88.                                                 whiteQueen(a, tag+1);
  89.                                         }
  90.                                         a[tag][j] = 1;
  91.                                 }
  92.                         }
  93.                        
  94.                 }
  95.                
  96.                
  97.         }
  98.        
  99.         @SuppressWarnings({ "unused", "resource" })
  100.         public static void main(String[] args) {
  101.                 Scanner sc = new Scanner(System.in);
  102.                 while(sc.hasNextInt()){
  103.                         int n = sc.nextInt() ;
  104.                         int[][]a = new int[n][n] ;
  105.                        
  106.                         setArrays(a);
  107.        
  108.                         whiteQueen(a, 0);
  109.                         System.out.println(step);
  110.                         step = 0;
  111.                 }
  112.         }
  113. }
复制代码
2n皇后问题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-23 22:35:33 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-24 11:33:07 | 显示全部楼层
编译环境和运行环境jre一样吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-24 13:32:43 | 显示全部楼层
本帖最后由 DAY 于 2016-9-24 15:55 编辑

如果把数据输入放在主函数main里面就可以提交了,这是为什么啊!


  1. /*
  2. * 蓝桥杯-2n皇后问题
  3. * */

  4. import java.util.Scanner;

  5. /*输入的第一行为一个整数n,表示棋盘的大小。
  6. 接下来n行,每行n个0或1的整数,如果一个整数为1,
  7. 表示对应的位置可以放皇后,如果一个整数为0,表示对应的位置不可以放皇后。*/

  8. public class Main {
  9.        
  10.         @SuppressWarnings("unused")
  11.         private static int STEP = 0 ;
  12.        
  13.         //判断i,j 位置是否合法
  14.         @SuppressWarnings("unused")
  15.         private static boolean legalWhiteQueen(int[][]a,final int i ,final int j) {
  16.                 for(int n = 0 ;n < i ; ++n){
  17.                         for(int m = 0 ; m < a.length ; ++m){
  18.                                 if(a[n][m] == 2){
  19.                                         if(Math.abs(i-n) == Math.abs(j- m) || j == m) return false;
  20.                                 }else continue ;
  21.                         }
  22.                 }
  23.                 return true;
  24.         }
  25.        
  26.         //判断i,j位置是否合法
  27.         @SuppressWarnings("unused")
  28.         private static boolean legalBlackQueen(int[][]a,final int i ,final int j) {
  29.                
  30.                 for(int n = 0 ; n < i ; n++){
  31.                        
  32.                         for(int m = 0 ; m < a.length ;++m){
  33.                                 if(a[n][m] == 3){
  34.                                         if(Math.abs(i-n) == Math.abs(j-m) || j == m)return false;
  35.                                 }
  36.                         }
  37.                 }
  38.                
  39.                 return true;
  40.         }
  41.        
  42.         //TODO 其次判断黑皇后
  43.         private static void blackQueen(int[][]a,int tag){
  44.                 if(tag == a.length){STEP++;/*System.out.println("step:" +step);printArrays(a);System.out.println();*/}
  45.                 else{
  46.                         for(int j = 0 ; j < a.length ; ++j){
  47.                                 if(a[tag][j] != 2 && a[tag][j] != 0 ){
  48.                                         a[tag][j] = 3;
  49.                                         if(legalBlackQueen(a, tag, j)){
  50.                                                 blackQueen(a, tag+1);
  51.                                         }
  52.                                         a[tag][j] = 1;
  53.                                 }
  54.                         }
  55.                 }
  56.         }
  57.        
  58.         // TODO 首先判断白皇后
  59.         @SuppressWarnings("unused")
  60.         private static void whiteQueen(int[][]a,int tag){
  61.                 if(tag == a.length)blackQueen(a, 0);
  62.                 else{
  63.                         for(int j = 0 ; j < a.length; ++j){
  64.                                 if(a[tag][j] != 0){
  65.                                         a[tag][j] = 2;
  66.                                         if(legalWhiteQueen(a, tag, j)){
  67.                                                 whiteQueen(a, tag+1);
  68.                                         }
  69.                                         a[tag][j] = 1;
  70.                                 }
  71.                         }
  72.                        
  73.                 }
  74.                
  75.                
  76.         }
  77.        
  78.         @SuppressWarnings({ "unused", "resource" })
  79.         public static void main(String[] args) {
  80.                 Scanner sc = new Scanner(System.in);

  81.                         int n = sc.nextInt() ;
  82.                         int[][]a = new int[n][n] ;
  83.                        
  84.                         for(int i =0 ;i < n ;++i){
  85.                                 for(int j =0 ; j < n;++j){
  86.                                         a[i][j] = sc.nextInt() ;
  87.                                 }
  88.                         }
  89.                         whiteQueen(a, 0);
  90.                         System.out.println(STEP);
  91.                        
  92.                
  93.         }
  94. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 19:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表