网易在线笔试题 采蘑菇

在N*M的草地上,提莫种了K个蘑菇,蘑菇爆炸的威力极大,兰博不想贸然去闯,而且蘑菇是隐形的.只 有一种叫做扫描透镜的物品可以扫描出隐形的蘑菇,于是他回了一趟战争学院,买了2个扫描透镜,一个 扫描透镜可以扫描出(3*3)方格中所有的蘑菇,然后兰博就可以清理掉一些隐形的蘑菇. 问:兰博最多可以清理多少个蘑菇?

这里采用暴力法每次都算出能检测到的最大的蘑菇数,但是一个有点儿难度的问题就是如何记录蘑菇数最多的那个透镜的位置,一遍在第二次扫描之前将那个位置的蘑菇数减一。

代码已经accept,如果要想通过在线验证,必须把类名和生成对象那两行改为Main就好了,欢迎大家交流。

 1 import java.util.Scanner;
 2 
 3 public class Mushroom {
 4 
 5     public static void main(String[] args){
 6         Scanner scanner=new Scanner(System.in);
 7         while(scanner.hasNext()){
 8             int N=scanner.nextInt();
 9             int M=scanner.nextInt();
10             int K=scanner.nextInt();
11             int[][] pos=new int[K][2];
12             for(int i=0;i<K;i++){
13                 pos[i][0]=scanner.nextInt();
14                 pos[i][1]=scanner.nextInt();
15             }
16             Mushroom mushroom=new Mushroom();
17             int[][] NM=mushroom.getNM(N, M, K, pos);
18             int count = mushroom.scanMushroom(NM);
19             count += mushroom.scanMushroom(NM);
20             
21             System.out.println(count);
22         }
23         
24     }
25     
26     public int[][] getNM(int N, int M, int K, int[][] pos){
27         if(N<3) N=3;
28         if(M<3) M=3;
29         int[][] NM=new int[N+1][M+1];
30         for(int i=1;i<=N;i++){
31             for(int j=1;j<=M;j++){
32                 NM[i][j]=0;
33             }
34         }
35         
36         for(int i=0;i<K;i++){
37             ++NM[pos[i][0]][pos[i][1]];
38         }
39         
40         return NM;
41     }
42     
43     public int scanMushroom(int[][] NM){
44         int maxCount=0; 
45         // 数组是按行向量存储的
46         int N=NM.length-1;
47         int M=NM[0].length-1;
48         
49         CountAndPos rc1=getMushroomInRect(NM, 2, 2);   //记录3*3矩形内最大蘑菇数和此时矩形的中心坐标
50         for(int i=2;i<=N-1;i++){
51             for(int j=2;j<=M-1;j++){
52                 CountAndPos rc2= getMushroomInRect(NM, i, j);
53                 if(maxCount<rc2.countInRect){
54                     rc1=rc2;
55                     maxCount = rc2.countInRect;
56                 }
57             }
58         }
59         
60         // 扫描蘑菇后清除蘑菇
61         int centerX=rc1.x;
62         int centerY=rc1.y;
63         for(int i=-1;i<=1;i++){
64             for(int j=-1;j<=1;j++){
65                 if(NM[centerX+i][centerY+j]>0){
66                     --NM[centerX+i][centerY+j];
67                 }
68             }
69         }    
70         
71         return maxCount;
72     }
73     
74     public CountAndPos getMushroomInRect(int[][]NM,int centerX, int centerY){
75         int countInRect=0;
76         for(int i=-1;i<=1;i++){
77             for(int j=-1;j<=1;j++){
78                 if(NM[centerX+i][centerY+j]>0){
79                     countInRect++;
80                 }
81             }
82         }
83         CountAndPos cp = new CountAndPos(countInRect,centerX,centerY);
84         return cp;
85     }
86     
87     private class CountAndPos{
88         public int countInRect;
89         public int x;
90         public int y;
91         public CountAndPos(int count, int x, int y){
92             this.countInRect=count;
93             this.x=x;
94             this.y=y;
95         }
96     }
97 
98 }
Mushroom
原文地址:https://www.cnblogs.com/kylinxue/p/5300005.html