某大厂云事业部考试题

2022-07-25

题目大意:任意给定包含两种数值(10,3)的二维数组,求取由其中包含的边界数。注意:两条相邻边界算作一条。

package com.song.exam;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Exam_SideCount {
    public static int sidenum=0; 
    public static int bx=100; 
    public static int by=100; 
    public static int[][] basicArray=null;
    public static List<Points> hlist=new ArrayList<>();
    public static List<Points> siders=new ArrayList<>();
    public static List<Points> checker=new ArrayList<>();
    public static Set<String> siderset=new HashSet<String>();
    
    public void calcuSider(int m,int n) {
        checker.add(new Points(-1,-1,0));
        checker.add(new Points(-1,0,0));
        checker.add(new Points(-1,1,0));
        checker.add(new Points(0,1,0));
        checker.add(new Points(1,1,0));
        checker.add(new Points(1,0,0));
        checker.add(new Points(1,-1,0));
        checker.add(new Points(0,-1,0));
        for (int i = 0; i < m ; i++) {
            for (int j = 0; j < n ; j++) {
                if(basicArray[i][j]==3) {
                    hlist.add(new Points(i,j,basicArray[i][j]));
                }
            }
        }
        for(Points hpoint:hlist) {
            check_is_side(hpoint);
        }
        while(!check_link_side());
        sidenum=siderset.size();    
    }
    public void check_is_side(Points hpoint) {
            String ptside="";
            for(Points wk:checker) {
                int tx=hpoint.getX()+wk.x,ty=hpoint.getY()+wk.y;
                if(check_loc(tx,ty)) {
                if(basicArray[tx][ty]!=10) {
                    hlist.remove(new Points(tx,ty,3));
                }
                else 
                {
                    ptside+=tx+":"+ty+",";    
                }
                }
            }
            siderset.add(ptside);    
    }
    public boolean check_loc(int m,int n) {
        if (m< 0||m>=bx||n<0||n>=by)
           return false;
           return true;
    }
    public boolean check_link_side() {
        for(String tsider:siderset) {
            String[] pnames=tsider.split(",");
            String othername="";
            for(String csider:siderset) {
                if(!tsider.equals(csider))
                    othername+="@,"+csider;
            }
            for(String check_name:pnames) {
                Points chpt=new Points(check_name);
                for(Points wk:checker) {
                    int tx=chpt.getX()+wk.x,ty=chpt.getY()+wk.y;
                    if(check_loc(tx,ty)) {    
                        String tstr=","+tx+":"+ty+",";    
                        //两条边有重叠或相交
                        if(othername.indexOf(tstr)>=0) {
                            int loc=othername.indexOf(tstr);
                            String oside=othername.substring(0,loc);
                            if(oside.lastIndexOf("@")>=0)
                                oside=othername.substring(oside.lastIndexOf("@")+1);
                            else
                                oside=othername;    
                            if(oside.lastIndexOf("@")>=0)
                            oside=oside.substring(0,oside.lastIndexOf("@"));
                            else
                                oside=oside.substring(1);
                            String sider=tsider+oside.substring(1);
                            
                            siderset.add(sider);
                            siderset.remove(tsider);
                            siderset.remove(oside);
                            return false;
                        }
                }
            }
            
        }    
    }
        return true;
    }
    public class Points{
        String name="";
        int x=0;
        int y=0;
        int value=1;
        
        public Points(int x, int y,int value) {
            this.x = x;
            this.y = y;
            this.name+=x+":"+y;
            this.value = value;
        }
        public Points(String name) {
            this.name=name;
            this.x = Integer.parseInt(name.substring(0, name.indexOf(":")));
            this.y =Integer.parseInt(name.substring(name.indexOf(":")+1));
            this.value = 1;
        }
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
        public int getValue() {
            return value;
        }
        public void setValue(int value) {
            this.value = value;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }    
    }
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();// 行数
        int n = sc.nextInt();// 列数
        if (m<= 0||m>=bx||n<=0||n>=by) {
            System.out.println("行列参数输入错误");
        }
        bx=m;
        by=n;
        int[][] tarray = new int[m][n];
    
        for (int i = 0; i < m; i++) {
                sc.nextLine();
                for (int j = 0; j < n; j++) {
                    tarray[i][j] = sc.nextInt();
                    if (tarray[i][j] != 10 && tarray[i][j] != 3) {
                        System.out.println("参数输入错误");
                    }
                }
         }  
        basicArray=tarray;    
        Exam_SideCount sider=new Exam_SideCount();
        sider.calcuSider(m,n);
        System.out.println("边界数: "+sidenum);
    }
}

还有一点问题,比如有连续高值的情况~

本文地址:https://blog.csdn.net/u014694028/article/details/112226524

《某大厂云事业部考试题.doc》

下载本文的Word格式文档,以方便收藏与打印。