zoukankan      html  css  js  c++  java
  • PAT——1068. 万绿丛中一点红

    对于计算机而言,颜色不过是像素点对应的一个24位的数值。现给定一幅分辨率为MxN的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围8个相邻像素的颜色差充分大。

    输入格式:

    输入第一行给出三个正整数,分别是M和N(<= 1000),即图像的分辨率;以及TOL,是所求像素点与相邻点的颜色差阈值,色差超过TOL的点才被考虑。随后N行,每行给出M个像素的颜色值,范围在[0, 224)内。所有同行数字间用空格或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

     1 package com.hone.basical;
     2 
     3 import java.util.HashMap;
     4 import java.util.Map;
     5 import java.util.Scanner;
     6 /**
     7  * 原题目:https://www.patest.cn/contests/pat-b-practise/1067
     8  * @author Xia
     9  * 题目有三个核心地方:
    10  * (1):该点必须是唯一的,一点绿,也就是说如果不是唯一,则直接跳过。
    11  * (2):怎么样存储才能保证其唯一性呢?用数值作为数组下标的方式?肯定不妥当,因为像素点涉及到2的24次方,如果强行建立一个大的数组,内存肯定得爆炸。
    12  * (3):现在有一个疑惑?
    13  *         二维数组四周的元素到底能不能算?毕竟他没有周围八个元素。
    14  *         如果周围的元素检查,则得建立一个方法避免数组越界的情况。
    15  *     尽管如此,内存还是爆炸了。。
    16  */
    17 
    18 public class basicalLevel1068color {
    19     static int a;
    20     static int b;
    21     static int[][] color = new int[1001][1001];
    22     static int tol;
    23     
    24     //用一个dir指引四周的八种情况的变化
    25     static int[][] dir ={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
    26     public static void main(String[] args) {
    27         Scanner in = new Scanner(System.in);
    28         a = in.nextInt();
    29         b = in.nextInt();
    30         tol = in.nextInt();
    31         Map<Integer, Integer> times = new HashMap<>();            //用一个map来存储每一个的出现次数
    32         for (int i = 0; i < b; i++) {
    33             for (int j = 0; j < a; j++) {
    34                 color[i][j] = in.nextInt();
    35                 if (times.get(color[i][j])!=null) {
    36                     int index = times.get(color[i][j]);
    37                     times.put(color[i][j], index+1);
    38                 }else {
    39                     times.put(color[i][j], 1);
    40                 }
    41             }
    42         }
    43         int row = -1;
    44         int col = -1;
    45         int num = 0;
    46         for (int i = 0; i < b; i++) {
    47             for (int j = 0; j < a; j++) {
    48                 if (times.get(color[i][j]) == 1&&check(i, j)) {
    49                     num++;
    50                     row = i;
    51                     col = j;
    52                 }
    53             }
    54         }
    55         
    56         if (num > 1) {
    57             System.out.println("Not Unique");
    58         }else if (num == 1) {
    59             System.out.println("("+(col+1)+", "+(row+1)+"): "+color[row][col]);
    60         }else if (num == 0) {
    61             System.out.println("Not Exist");
    62         }
    63     }
    64     
    65     /**
    66      * 检查是否符合要求
    67      * @param x
    68      * @param y
    69      * @return
    70      */
    71     public static boolean check(int x,int y){
    72         boolean sigle = true;
    73         for (int h = 0; h < 8; h++) {
    74             int xx = x + dir[h][0];  
    75             int yy = y + dir[h][1];  
    76             if(xx>=0 && xx<b && yy<a && yy>=0 && Math.abs(color[xx][yy]-color[x][y])<=tol) sigle = false;  
    77         }
    78         return sigle;
    79     }
    80     
    81 }




  • 相关阅读:
    [Algo] 646. Store Number Of Nodes In Left Subtree
    [Algo] 611. Compress String II
    [Algo] 175. Decompress String II
    [Algo] 625. Longest subarray contains only 1s
    [Algo] 253. Longest Substring Without Repeating Characters
    [Algo] 292. String Abbreviation Matching
    [Algo] 649. String Replace (basic)
    [Algo] 397. Right Shift By N Characters
    flume 详细介绍
    Hive学习路线图【转】
  • 原文地址:https://www.cnblogs.com/xiaxj/p/8005622.html
Copyright © 2011-2022 走看看