PTA 1068 万绿丛中一点红 (20 分) C++实现

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

对于计算机而言,颜色不过是像素点对应的一个 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
结尾无空行

解题思路:

①判断周围有8个点的点是否满足条件,若满足则将其存在数组ans中,然后判断该点是否重复,若不重复则满足条件的点加1,最后按条件输出
②利用map容器来确定每个点出现的次数,然后只判断只出现一次的点的周围8个点是否满足条件,如果满足则满足条件的点加1,最后按条件输出

代码示例(第①种思路,有两个测试点没过):

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>
#include <map>

using namespace std;


int main()
{
	int col = 0, row = 0, tol = 0;
	cin >> col >> row >> tol;

	int nums[row][col];
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cin >> nums[i][j];
		}
	}

	int x_col = 0, y_row = 0; //记录位置
	vector<int> ans;
	for (int i = 1; i < row - 1; i++)
	{
		for (int j = 1; j < col - 1; j++)
		{
			if (abs(nums[i][j] - nums[i][j - 1]) > tol && abs(nums[i][j] - nums[i][j + 1]) > tol && abs(nums[i][j] - nums[i - 1][j]) > tol && abs(nums[i][j] - nums[i + 1][j]) > tol&& abs(nums[i][j] - nums[i - 1][j - 1]) > tol && abs(nums[i][j] - nums[i - 1][j + 1]) > tol && abs(nums[i][j] - nums[i + 1][j - 1]) > tol && abs(nums[i][j] - nums[i + 1][j + 1]) > tol)
			{
				ans.push_back(nums[i][j]);
			}
		}
	}

	int count = 0;//对满足条件的点计数
	int res; //存最终结果
	for (int i = 0; i < ans.size(); i++)
	{
		int cnt = 0;
		int j, k;
		for (j = 0; j < row; j++)
		{
			for (k = 0; k < col; k++)
			{
				if (ans[i] == nums[j][k])
				{
					x_col = k;
					y_row = j;
					cnt++;
				}
			}
		}

		if (cnt == 1)
		{
			res = ans[i];
			count++;
		}
	
	}

	if (count == 1)
	{
		printf("(%d, %d): %d\n", x_col + 1, y_row + 1, res);
	}
	else if (count >= 2)
	{
		cout << "Not Unique" << endl;
	}
	else if (count == 0)
	{
		cout << "Not Exist" << endl;
	}
}

运行结果:

在这里插入图片描述

代码示例(第②种思路):

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>
#include <map>

using namespace std;

int nums[1001][1001];

int main()
{
	int col = 0, row = 0, tol = 0;
	cin >> col >> row >> tol;

	map<int, int> mapp;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cin >> nums[i][j];
			mapp[nums[i][j]]++;
		}
	}
	int count = 0; //对满足条件的点计数
	int res = 0; //存最终结果
	int x_col = 0, y_row = 0; //记录位置

	vector<int> ans;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (mapp[nums[i][j]] == 1)
			{
				if (i - 1 >= 0) 
				{
					if (abs(nums[i][j] - nums[i - 1][j]) <= tol)
						continue;
				}
				if (i - 1 >= 0 && j - 1 >= 0) 
				{
					if (abs(nums[i][j] - nums[i - 1][j - 1]) <= tol)
						continue;
				}
				if (i - 1 >= 0 && j + 1 < col) 
				{
					if (abs(nums[i][j] - nums[i - 1][j + 1]) <= tol)
						continue;
				}
				if (j - 1 >= 0) 
				{
					if (abs(nums[i][j] - nums[i][j - 1]) <= tol)
						continue;
				}
				if (j + 1 < col) 
				{
					if (abs(nums[i][j] - nums[i][j + 1]) <= tol)
						continue;
				}
				if (i + 1 < row) 
				{
					if (abs(nums[i][j] - nums[i + 1][j]) <= tol)
						continue;
				}
				if (i + 1 < row && j - 1 >= 0) 
				{
					if (abs(nums[i][j] - nums[i + 1][j - 1]) <= tol)
						continue;
				}
				if (i + 1 < row && j + 1 < col) 
				{
					if (abs(nums[i][j] - nums[i + 1][j + 1]) <= tol)
						continue;
				}
				
				//如果能避过所有continue,则说明该点满足条件
				count++;
				if (count >= 2)
				{
					break;
				}
				res = nums[i][j];
				x_col = j;
				y_row = i;
				
			}
			
		}
	}
	
	if (count == 1)
	{
		printf("(%d, %d): %d\n", x_col + 1, y_row + 1, res);
	}
	else if (count >= 2)
	{
		cout << "Not Unique" << endl;
	}
	else if (count == 0)
	{
		cout << "Not Exist" << endl;
	}
	
}

运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Crazy.宥思

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值