1068 万绿丛中一点红

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

输入格式:

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

输出格式:

在一行中按照 (x, y): color 的格式输出所求像素点的位置以及颜色值,其中位置 x 和 y 分别是该像素在图像矩阵中的列、行编号(从 1 开始编号)。如果这样的点不唯一,则输出 Not Unique;如果这样的点不存在,则输出 Not Exist

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int main() {
	int m, n;
	long long color[1001][1001] = { 0 };
	int tol;
	cin >> m >> n >> tol;
	vector<int> color_2;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			long long tmp;
			cin >> tmp;
			color[i][j] = tmp;
			color_2.push_back(tmp);
		}
	}
	vector<pair<vector<int>, long long>> result;
	int next[8][2] = { {0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1} };
	for (int i = 0; i < n; i++) {
		
		for (int j = 0; j < m; j++) {
			int tx, ty;
			int cnt = 0;
			int cmp = 8;
			for (int k = 0; k < 8; k++) {
				tx = j + next[k][0];
				ty = i + next[k][1];
				if (tx < 0 || tx >= m || ty < 0 || ty >= n) {
					cmp--;
					continue;
				}
				if (color[ty][tx] - color[i][j] < -tol || color[ty][tx] - color[i][j] > tol)cnt++;
			}
			if (cnt == cmp) {
				if (count(color_2.begin(), color_2.end(), color[i][j]) == 1) {
					vector<int> tmp;
					tmp.push_back(i + 1);
					tmp.push_back(j + 1);
					result.push_back(make_pair(tmp, color[i][j]));
				}
			}
		}
	}
	if (result.size() == 0) cout << "Not Exist" << endl;
	else if (result.size() == 1) {
		for (auto i : result) {
			cout << "(" << i.first[1] << ", " << i.first[0] << "): " << i.second << endl;
		}
	}
	else {
		cout << "Not Unique" << endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值