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

这道题注意题目中的“独一无二”,就是必须这个点只能出现一次。与其8个周围的点并不是指只能在内圈进行扫描,在

最外圈也必须扫描,8个周围的点意思表达不明确。最后没法通过一个超时的例子,需要把cin换成scanf,因为cin效率比

scanf效率低很多。这就是这道题的几个坑点。下面代码:

#include<iostream>
#include<vector>
#include<map>
using namespace std;

int tol, n, m;
vector<int> v[1000];
int dir[8][2] = { { -1, -1 },{ -1, 0 },{ -1, 1 },{ 0, 1 },{ 1, 1 },{ 1, 0 },{ 1, -1 },{ 0, -1 } };

bool judge(int i, int j) {
	for (int k = 0; k < 8;k++) {
		int tx = j + dir[k][0];
		int ty = i + dir[k][1];
		if (tx >= 0 && tx < n && ty >= 0 && ty < m && v[i][j] - v[ty][tx] >= 0 - tol && v[i][j] - v[ty][tx] <= tol) {
			return false;
		}
	}
	return true;
}

int main() {
	int count = 0, x = 0, y = 0, c, data = 0;
	cin >> n >> m >> tol;
	std::ios::sync_with_stdio(false);//此处是让cin效率提高,变得和scanf一样快
	map<int, int> mapp;

	for (int i = 0; i < m;i++) {
		for (int j = 0; j < n; j++) {
			cin >> c;
			std::ios::sync_with_stdio(false);
			v[i].push_back(c);
			mapp[v[i][j]]++;
		}
	}
	
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			if (mapp[v[i][j]] == 1 && judge(i,j)==true) {
				y = i + 1;
				x = j + 1;
				data = v[i][j];
				count++;
			}
			if (count == 2) break;
		}
		if (count == 2) break;
	}

	if (count == 2) {
		cout << "Not Unique";
	}
	else if (count == 0) {
		cout << "Not Exist";
	}
	else if (count == 1) {
		cout << "(" << x << ", " << y << ")" << ": " << data;
	}

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值