春風博客

          春天里,百花香...

          導航

          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          求最大價值轟炸目標

          package com.sitinspring;

          /**
           * 從8*8的方陣中找出最大價值轟炸目標,轟炸范圍為9格
           * 
           * 
          @author sitinspring(junglesong@gmail.com)
           * 
          @since 2008-6-17 上午11:05:15
           * @vsersion 1.00 創建 sitinspring 2008-6-17 上午11:05:15
           
          */

          public class MaxValuableTarget{
              
          /**
               * 程序入口
               * 
          @param args
               
          */

              
          public static void main(String[] args){
                  
          // 地圖數組
                  int[][] map={
                      
          {1,2,3,4,5,3,4,6},
                      
          {3,2,0,4,5,3,4,2},
                      
          {0,2,3,6,5,3,4,6},
                      
          {5,2,3,4,9,3,4,4},
                      
          {1,7,2,4,5,3,4,6},
                      
          {0,2,3,6,5,3,9,6},
                      
          {5,2,3,4,9,3,4,6},
                      
          {1,7,2,4,5,3,2,6},
                  }
          ;
                  
                  displayMap(map,
          8);
                  
          // 轟炸范圍為九格(3*3),找出最大價值目標
                  printMaxValuableTargetInfo(map,8);
              }

              
              
          /**
               * 顯示地圖
               * 
          @param map
               * 
          @param maxRow
               * 
          @param colCount
               
          */

              
          public static void displayMap(int[][] map,int colCount){
                  
          int i,j;
                  
          int maxRow=map.length;
                  
          for(i=0;i<maxRow;i++){
                      
          for(j=0;j<colCount;j++){
                          System.out.print(map[i][j]
          +"|");                
                      }

                      
                      System.out.println(
          "");
                  }

              }

              
              
          /**
               * 打印最大轟炸價值點信息
               * 
          @param map
               * 
          @param maxRow
               * 
          @param colCount
               
          */

              
          public static void printMaxValuableTargetInfo(int[][] map,int colCount){
                  
          int maxRow=map.length;
                  
          int i,j;
                  
          int max=0;
                  
          int xCoordinate=0,yCoordinate=0;
                  
          for(i=0;i<maxRow;i++){
                      
          for(j=0;j<colCount;j++){
                          
          int value=getBombingTargetValue(i,j,map);
                          
          // System.out.println("i="+i+" j="+j+" value="+value);
                          if(value>max){
                              max
          =value;
                              xCoordinate
          =i;
                              yCoordinate
          =j;
                          }

                      }

                  }

                  
                  System.out.print(
          "最大價值轟炸點位于x="+xCoordinate+" y="+yCoordinate+"其價值為"+max);
              }

              
              
          /**
               * 取得轟炸點價值
               * 
          @param x
               * 
          @param y
               * 
          @param map
               * 
          @return
               
          */

              
          public static int getBombingTargetValue(int x,int y,int[][] map){
                  
          /*final int[][] arr={
                      {-1,-1},{0,-1},{1,-1},
                      {-1,0},{0,0},{1,0},
                      {-1,1},{0,1},{1,1},
                  };
          */

                  
                  
          final int[][] arr=getBoomArea(3);
                  
                  
          int sum=0;
                  
                  
          for(int i=0;i<arr.length;i++){
                      
          int newX=x+arr[i][0];
                      
          int newY=y+arr[i][1];
                      
          if(newX>-1 && newY>-1 && newX<8 && newY<8){
                          sum
          +=map[newX][newY];
                      }

                  }

                  
                  
          return sum;
              }

              
              
          /**
               * 取得爆炸范圍
               * 
          @param sideLength 邊長(滾據題意邊長應該是奇數)
               * 
          @return
               
          */

              
          public static int[][] getBoomArea(int sideLength){
                  
          int arrLength=sideLength*sideLength;
                  
          int[][] arr=new int[arrLength][2];
                  
                  
          for(int i=0;i<arr.length;i++){
                      arr[i][
          0]=i/sideLength-sideLength/2;
                      arr[i][
          1]=i%sideLength-sideLength/2;
                  }

                  
                  
          return arr;
              }

          }

          輸出結果:
          1|2|3|4|5|3|4|6|
          3|2|0|4|5|3|4|2|
          0|2|3|6|5|3|4|6|
          5|2|3|4|9|3|4|4|
          1|7|2|4|5|3|4|6|
          0|2|3|6|5|3|9|6|
          5|2|3|4|9|3|4|6|
          1|7|2|4|5|3|2|6|
          最大價值轟炸點位于x
          =4 y=5其價值為45

          posted on 2008-06-17 11:56 sitinspring 閱讀(289) 評論(0)  編輯  收藏 所屬分類: Java基礎算法數據結構

          sitinspring(http://www.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 麻江县| 霍州市| 宕昌县| 上虞市| 绍兴市| 司法| 封开县| 深泽县| 甘南县| 桃源县| 留坝县| 垣曲县| 寿宁县| 竹山县| 樟树市| 太湖县| 金门县| 宁波市| 寿宁县| 富阳市| 江城| 南郑县| 双桥区| 梁河县| 晋宁县| 郓城县| 崇礼县| 彝良县| 醴陵市| 噶尔县| 岱山县| 泾川县| 古交市| 荣成市| 天柱县| 会宁县| 长治县| 溧水县| 长寿区| 基隆市| 卓尼县|