春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

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

          統(tǒng)計(jì)

          公告

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

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個(gè)人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          求最大價(jià)值轟炸目標(biāo)

          package com.sitinspring;

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

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

              
          public static void main(String[] args){
                  
          // 地圖數(shù)組
                  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),找出最大價(jià)值目標(biāo)
                  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(
          "");
                  }

              }

              
              
          /**
               * 打印最大轟炸價(jià)值點(diǎn)信息
               * 
          @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(
          "最大價(jià)值轟炸點(diǎn)位于x="+xCoordinate+" y="+yCoordinate+"其價(jià)值為"+max);
              }

              
              
          /**
               * 取得轟炸點(diǎn)價(jià)值
               * 
          @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 邊長(zhǎng)(滾據(jù)題意邊長(zhǎng)應(yīng)該是奇數(shù))
               * 
          @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;
              }

          }

          輸出結(jié)果:
          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|
          最大價(jià)值轟炸點(diǎn)位于x
          =4 y=5其價(jià)值為45

          posted on 2008-06-17 11:56 sitinspring 閱讀(289) 評(píng)論(0)  編輯  收藏 所屬分類: Java基礎(chǔ)算法數(shù)據(jù)結(jié)構(gòu)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處.
          主站蜘蛛池模板: 永寿县| 兴安县| 萨迦县| 三门峡市| 如皋市| 墨江| 普安县| 永吉县| 淳安县| 长沙市| 安宁市| 南部县| 和田县| 黔南| 沭阳县| 比如县| 佛坪县| 日土县| 英德市| 虞城县| 邓州市| 土默特右旗| 香格里拉县| 资源县| 宜春市| 鱼台县| 葫芦岛市| 林周县| 延津县| 门源| 昆山市| 江口县| 拜城县| 峡江县| 东方市| 谢通门县| 交城县| 双辽市| 德化县| 怀集县| 简阳市|