pat 乙级 1068 万绿丛中一点红

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

输入格式:

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

输出格式:

在一行中按照 (x, y): color 的格式输出所求像素点的位置以及颜色值,其中位置 xy 分别是该像素在图像矩阵中的列、行编号(从 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

写代码一定要认真认真啊  ,写if的时候一定要想想else,还有写完一行代码时一定要回看一下是否有问题。一直卡到测试点3和5上,测试点3和4考查的主要原因有没有对上下左右四个角及边界特殊处理另一点没考虑到独一无二,也就是说有两个相同值同时满足tol也是不行的。

处理完之后测试点一直不过,全都是在细节上,1)判断边界位置时坐标位置写错了, 2)if判断完后没有else处理。

看了一下其他同学的代码,感觉我的代码有点冗余(稍后按照其他同学思路写一下)

#include <iostream>
#include <unordered_map>

using namespace std;

int pixel[1000][1000] = { 0 };
unordered_map<int, int>flag;

bool tolfunc(int i, int j, int (*array)[1000], int tolValue,int &unique,int m,int n)// m列
{

	if (flag[pixel[i][j]] ==1)
	{
		if (i == 0 && j == 0)//lt
		{
			if (   abs( (array[i][j] - array[i][j + 1]) )  > tolValue &&  abs( (array[i][j] - array[i + 1][j + 1]) )> tolValue && abs( (array[i][j] - array[i + 1][j]) ) > tolValue  )
			{
			    
				unique++;
			}
			else
			{
				return false;
			}
		}

		else if (i == 0 && j == m - 1)//rt
		{
			if (  abs((array[i][j] - array[i][j - 1])) > tolValue && abs((array[i][j] - array[i + 1][j - 1]) )> tolValue &&  abs((array[i][j] - array[i + 1][j]) ) > tolValue)
			{
				unique++;
			}
			else
			{
				return false;
			}
		}
		else if (i == n - 1 && j == 0)//lb
		{
			if (  abs( (array[i][j] - array[i][j + 1])) > tolValue && abs( (array[i][j] - array[i - 1][j])) > tolValue &&  abs((array[i][j] - array[i - 1][j + 1])) > tolValue)
			{
				unique++;
			}
			else
			{
				return false;
			}
		}
		else if (i == n - 1 && j == m - 1)//rb
		{
			if (  abs ((array[i][j] - array[i][j - 1])) > tolValue && abs((array[i][j] - array[i - 1][j - 1]) )> tolValue && abs( (array[i][j] - array[i - 1][j])) > tolValue)
			{
				unique++;
			}
			else
			{
				return false;
			}
		}
		else if (i == 0 && j != 0 && j != m - 1)//up
		{
			if (abs((array[i][j] - array[i][j - 1])) > tolValue && abs((array[i][j] - array[i + 1][j])) > tolValue && abs((array[i][j] - array[i + 1][j + 1])) > tolValue &&
				abs((array[i][j] - array[i + 1][j - 1])) > tolValue && abs((array[i][j] - array[i][j+1])) > tolValue)
			{
				unique++;
			}
			else
			{
				return false;
			}
		}
		else if (i != 0 && i != n - 1 && j == 0)//left
		{
			if (abs((array[i][j] - array[i][j + 1])) > tolValue && abs((array[i][j] - array[i + 1][j])) > tolValue && abs((array[i][j] - array[i + 1][j + 1])) > tolValue &&
				abs((array[i][j] - array[i - 1][j])) > tolValue && abs((array[i][j] - array[i - 1][j + 1])) > tolValue)
			{
				unique++;
			}
			else
			{
				return false;
			}
		}
		else if (i == n - 1 && j != 0 && j != m - 1)//bottom
		{
			if (abs((array[i][j] - array[i][j - 1])) > tolValue && abs((array[i][j] - array[i - 1][j - 1])) > tolValue && abs((array[i][j] - array[i - 1][j])) > tolValue &&
				abs((array[i][j] - array[i - 1][j + 1])) > tolValue && abs((array[i][j] - array[i][j + 1])) > tolValue)
			{

				unique++;
			}
			else
			{
				return false;
			}
		}
		else if (j == m- 1 && i != 0 && i != n- 1)//right
		{
			if (  abs((array[i][j] - array[i - 1][j])) > tolValue && abs((array[i][j] - array[i - 1][j - 1])) > tolValue && (abs(array[i][j] - array[i][j - 1])) > tolValue &&
				abs((array[i][j] - array[i + 1][j - 1])) > tolValue && abs((array[i][j] - array[i + 1][j])) > tolValue)
			{
				
					unique++;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (   (abs((array[i][j] - array[i - 1][j])) > tolValue && (abs(array[i][j] - array[i][j - 1])) > tolValue &&
				(abs(array[i][j] - array[i + 1][j])) > tolValue && (abs(array[i][j] - array[i][j + 1])) > tolValue &&
				(abs(array[i][j] - array[i - 1][j - 1])) > tolValue && (abs(array[i][j] - array[i - 1][j + 1])) > tolValue &&
				(abs(array[i][j] - array[i + 1][j + 1])) > tolValue && (abs(array[i][j] - array[i + 1][j - 1])) > tolValue)  )
			{
				unique++;
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
	return true;
	
}
int main()
{
	
	int m, n,tol,unique;
	cin >> m >> n >> tol;
	unique = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			int temp;
			cin >> temp;
			pixel[i][j] = temp;
			flag[pixel[i][j]]++;
		}
	}
	int x, y;
	int value = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			bool ret=tolfunc(i, j, pixel, tol, unique,m,n);
			if (ret)
			{
				x = i;
				y = j;
				value = pixel[i][j];
			}
		}
	}
	if (unique == 1)
	{
		cout << "(" << y + 1 << ", " << x + 1 << "): " << value << endl;;
	}
	if (unique == 0)
		cout << "Not Exist" << endl;
	if (unique >= 2)
	{
		cout << "Not Unique" << endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值