1068
对于计算机而言,颜色不过是像素点对应的一个 24 位的数值。现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
输入格式:
输入第一行给出三个正整数,分别是 M 和 N(≤ 1000),即图像的分辨率;以及 TOL,是所求像素点与相邻点的颜色差阈值,色差超过 TOL 的点才被考虑。随后 N 行,每行给出 M 个像素的颜色值,范围在 [0,2
24
) 内。所有同行数字间用空格或 TAB 分开。
输出格式:
在一行中按照 (x, y): color 的格式输出所求像素点的位置以及颜色值,其中位置 x 和 y 分别是该像素在图像矩阵中的列、行编号(从 1 开始编号)。如果这样的点不唯一,则输出 Not Unique;如果这样的点不存在,则输出 Not Exist。
输入样例 1:
8 6 200
0 0 0 0 0 0 0 0
65280 65280 65280 16711479 65280 65280 65280 65280
16711479 65280 65280 65280 16711680 65280 65280 65280
65280 65280 65280 65280 65280 65280 165280 165280
65280 65280 16777015 65280 65280 165280 65480 165280
16777215 16777215 16777215 16777215 16777215 16777215 16777215 16777215
输出样例 1:
(5, 3): 16711680
输入样例 2:
4 5 2
0 0 0 0
0 0 3 0
0 0 0 0
0 5 0 0
0 0 0 0
输出样例 2:
Not Unique
输入样例 3:
3 3 5
1 2 3
3 4 5
5 6 7
输出样例 3:
Not Exist
思路:这题完全不会题目也不是很懂这里附上大佬写的代码,还没搞懂先记下来之后做的差不多了在回头看这些不会的
代码来源:
个人博客:小景哥哥
package PTA2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class PTA1068 {
private static int[][] dir = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
private static int[][] screen = new int[1001][1001];
private static int tol;
private static int n;
private static int m;
private static boolean judge(int i, int j) {
for (int k = 0; k < 8; k++) {
int tx = i + dir[k][0];
int ty = j + dir[k][1];
if (tx >= 0 && tx < n && ty >= 0 && ty < m && screen[i][j] - screen[tx][ty] >= 0 - tol && screen[i][j] - screen[tx][ty] <= tol) return false;
}
return true;
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cnt = 0, x = 0, y = 0;
String[] s = br.readLine().split(" ");
m = Integer.parseInt(s[0]);
n = Integer.parseInt(s[1]);
tol = Integer.parseInt(s[2]);
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++) {
String[] sp = br.readLine().split("\\s+|\t");
for(int j = 0; j < m; j++) {
screen[i][j] = Integer.parseInt(sp[j]);
if(map.containsKey(screen[i][j]))
map.put(screen[i][j], map.get(screen[i][j]) + 1);
else
map.put(screen[i][j], 1);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map.get(screen[i][j]) == 1 && judge(i, j)) {
cnt++;
x = i + 1;
y = j + 1;
}
}
}
if (cnt == 1)
System.out.printf("(%d, %d): %d", y, x, screen[x-1][y-1]);
else if (cnt == 0)
System.out.print("Not Exist");
else
System.out.print("Not Unique");
}
}