写的是丑陋了些 之后再改进一下
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中间有个空格