Ex 6_5棋子放置问题_第八次作业

题目貌似有问题

(b)

子问题定义: 设maxValue[i][j]为棋盘的前i行中最后一行为i时第i行按照第j种放置方式放置时得到的最大覆盖值,comp[i][j]为第i种放置方式与第j种放置方式是否相容,value[i][j]为第i行按照第j种放置方式放置时覆盖整数的最大值,如此可以得到递归式。

递归关系:

初值设定:

       maxValue的行数为棋盘的行数加一,因此令maxValue[0][j]=0表示没有棋盘时值为0

求解顺序:

       按从上到下,从左到右的次序求解maxValue的每一行的值,最终返回maxValue的最后一行的最大值即为最终解。

  1 package org.xiu68.ch6.ex8;
  2 
  3 public class Ex6_5 {
  4 
  5 
  6     public static void main(String[] args) {
  7         // TODO Auto-generated method stub
  8         int[][] chessBoard1=new int[][]{
  9             {1,2,3,4},
 10             {2,1,2,1}
 11         };
 12         maxChessBoard(chessBoard1);   
 13         
 14         int[][] chessBoard2=new int[][]{
 15             {0,1,0,1},
 16             {1,0,1,0},
 17             {1,2,1,2}
 18         };
 19         maxChessBoard(chessBoard2);
 20         
 21         //运行结果
 22         /*
 23          被棋子覆盖的整数总和最大为: 10
 24         被棋子覆盖的整数总和最大为: 8
 25         */
 26 
 27     }
 28 
 29     //chessBoard:棋盘
 30     public static void maxChessBoard(int[][] chessBoard){
 31         int TYPE=8;                        //每一行可以放置的棋的种类数
 32         int rows=chessBoard.length;        //棋盘的行
 33         int cols=4;                        //棋盘的列
 34         
 35         //comp[i][j]表示i、j两种放置模式是否相容,每行共8种放置方式
 36         boolean[][] comp=new boolean[][]{
 37             {true,true,true,true,true,true,true,true},
 38             {true,false,true,true,true,false,true,false},
 39             {true,true,false,true,true,true,false,true},
 40             {true,true,true,false,true,false,true,true},
 41             {true,true,true,true,false,true,false,false},
 42             {true,false,true,false,true,false,true,false},
 43             {true,true,false,true,false,true,false,false},
 44             {true,false,true,true,false,false,false,false}
 45         };
 46         
 47          //每行8种放置方式,method[i][j]表示某一行在第i种放置方式下的第j列是否放棋
 48          boolean[][] method=new boolean[][]{
 49              {false,false,false,false},
 50              {true,false,false,false},
 51              {false,true,false,false},
 52              {false,false,true,false},
 53              {false,false,false,true},
 54              {true,false,true,false},
 55              {false,true,false,true},
 56              {true,false,false,true}
 57          };    
 58          
 59         //max[i][j]表示前i行中最后一行为i时第i行按照第j种放置方式的最大值
 60         int[][] max=new int[rows+1][TYPE];            
 61         for(int i=0;i<TYPE;i++)
 62             max[0][i]=0;                //最小子问题,初始化为0        
 63         
 64         //value[i][t]表示第i行按照第t种方式放棋得到的值
 65         int[][] value=new int[rows][TYPE];
 66         //初始化value数组
 67         for(int i=0;i<rows;i++){
 68             for(int t=0;t<TYPE;t++){
 69                 
 70                 for(int j=0;j<cols;j++){
 71                     if(method[t][j]){        //第t种放置方式下第j列是否放棋
 72                         value[i][t]+=chessBoard[i][j];
 73                     }
 74                 }
 75                 
 76             }
 77         }
 78         //求max数组
 79         for(int i=1;i<max.length;i++){
 80             for(int t=0;t<TYPE;t++){
 81 
 82                 max[i][t]=0;
 83                 for(int k=0;k<TYPE;k++){
 84                     if(!comp[t][k])        //t、k两种放置方式不相容
 85                         continue;
 86                     if(max[i-1][k]+value[i-1][t]>max[i][t])
 87                         max[i][t]=max[i-1][k]+value[i-1][t];
 88                 }
 89                 
 90             }
 91         }
 92         
 93         //求max数组的最后一行的最大值即为最终解
 94         int maxValue=0;
 95         for(int i=0;i<TYPE;i++){
 96             if(max[max.length-1][i]>maxValue)
 97                 maxValue=max[max.length-1][i];
 98         }
 99         System.out.println("被棋子覆盖的整数总和最大为: "+maxValue);
100     }
101     
102 }
View Code
原文地址:https://www.cnblogs.com/xiu68/p/7988896.html