春风博客

春天里,百花香...

导航

<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 阅读(274) 评论(0)  编辑  收藏 所属分类: Java基础算法数据结构


只有注册用户登录后才能发表评论。


网站导航:
 
sitinspring(http://www.blogjava.net)原创,转载请注明出处.