PAT乙级 1068.万绿丛中一点红 Java

 写的是丑陋了些   之后再改进一下

import java.util.*;

public class Main {
    static class Color{
        private int val;
        private int x;
        private int y;

        public Color(int val,int x, int y) {
            this.val = val;
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return val+" "+x+" "+y;
        }
    }

    static int[][] cube = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
    static int tol;
    static int m;
    static int n;
    static int [][]map=new int[1001][1001];
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        m=scanner.nextInt();
        n=scanner.nextInt();
        tol=scanner.nextInt();
        HashMap<Integer,Integer>hashMap=new HashMap<>();
        ArrayList<Color>arrayList=new ArrayList<>();
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                int val=scanner.nextInt();
                map[i][j]=val;
                if (!hashMap.containsKey(val)){
                    hashMap.put(val,1);
                    arrayList.add(new Color(val,i+1,j+1));
                }else {
                    hashMap.remove(val);//只存出现一次的
                }
            }
        }
        Iterator<Integer> iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()) {
            int key = iterator.next();
            int i=0;
            // 检查颜色值在map中是否只出现了一次
            for (;i< arrayList.size();i++){
                if (key==arrayList.get(i).val){
                    break;
                }
            }
            if (!isUnique(arrayList.get(i))) {
                iterator.remove(); // 从hashMap中删除不是唯一的颜色值
            }
        }
        if (hashMap.size()==0){
            System.out.println("Not Exist");
        } else if (hashMap.size()>1) {
            System.out.println("Not Unique");
        } else {
            for (int i: hashMap.keySet()){
                int j=0;
                for (;j< arrayList.size();j++){
                    if (i==arrayList.get(j).val)break;
                }
                System.out.println("("+arrayList.get(j).y+", "+arrayList.get(j).x+"): "+arrayList.get(j).val);
            }
        }

    }

    static boolean isUnique(Color color){

        for(int k=0;k<8;k++) {
            int x = color.x-1 + cube[k][0];		//定位
            int y = color.y-1 + cube[k][1];		//定位
            if (x >= 0 && x < n && y >= 0 && y < m && map[color.x-1][color.y-1] - map[x][y] >= -tol && map[color.x-1][color.y-1] - map[x][y] <= tol)
                return false;
        }
        return true;
    }
}

         注意:(5, 3) ,和3中间有个空格

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值