zoukankan      html  css  js  c++  java
  • PTA乙级 (1068 万绿丛中一点红 (20分)(map))

    1068 万绿丛中一点红 (20分)

    https://pintia.cn/problem-sets/994805260223102976/problems/994805265579229184

    题目思路:

    题目给定N*M个数字,要求找出只出现一次的数字,并且这个数字的相邻的数字都和他的差值必须大于给定的阈值tol。那么我们就找出出现次数为1的数字,然后判断该数字是否满足阈值这个条件。我们用map的key表示数字,value来表示key出现的次数。

    题目代码:

     1 #include <iostream>
     2 #include <cmath>
     3 #include <algorithm>
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <string>
     7 #include <map>
     8 using namespace std;
     9 int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};
    10 int s[1002][1002];
    11 map<int, int> vis;
    12 int m,n,tol;
    13 bool check(int x,int y)
    14 {
    15     for(int i=0;i<8;i++)
    16     {
    17         int xx=x+dir[i][0];
    18         int yy=y+dir[i][1];
    19         if(xx>=0&&xx<n&&yy>=0&&yy<m&&(abs(s[xx][yy]-s[x][y])<=tol)) return false;
    20     }
    21     return true;
    22 }
    23 int main()
    24 {
    25     cin>>m>>n>>tol;
    26     for(int i=0;i<n;i++)
    27     {
    28         for(int j=0;j<m;j++)
    29         {
    30             cin>>s[i][j];
    31             vis[s[i][j]]++;
    32         }
    33     }
    34     int cnt=0,x,y;
    35     for(int i=0;i<n;i++)
    36     {
    37         for(int j=0;j<m;j++)
    38         {
    39             if((vis[s[i][j]]==1)&&(check(i,j)))
    40             {
    41                 cnt++;
    42                 x=i;
    43                 y=j;
    44             }
    45         }
    46     }
    47     if(cnt==1) printf("(%d, %d): %d
    ",y+1,x+1,s[x][y]);
    48     else if(cnt>1) puts("Not Unique");
    49     else if(cnt==0) puts("Not Exist");
    50     return 0;
    51 }

    参考:https://blog.csdn.net/qq_34594236/article/details/63692920

    天晴了,起飞吧
  • 相关阅读:
    Libvirt错误总结
    linux学习
    HMC 命令行登陆设置
    AIX扩VG和扩文件系统
    Bootstrap CSS2
    Bootstrap CSS
    JQuery的实例集合
    JQuery的noConflict()方法
    android的intent实现页面的跳转
    android的activity
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/12215018.html
Copyright © 2011-2022 走看看