201912-2 回收站选址

stl解法

set:

#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

struct point
{
	int x,y;
	point(int _x, int _y):x(_x),y(_y){}
	bool operator < (const point &p) const
	{
		if(x == p.x) return y < p.y;
		return x < p.x;
	}
};
set<point> s;
bool istrue(int x, int y)
{
	if(s.find(point(x,y))!= s.end()) return true;
	return false;
}
int n, x, y;
int ans[10];
int main() 
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> x >> y;
		s.insert(point(x,y));
	}
	set<point>::iterator it;
	for(it = s.begin(); it != s.end(); it++)
	{
		int nx = (*it).x;
		int ny = (*it).y;
		if(istrue(nx,ny-1) && istrue(nx-1,ny) && istrue(nx + 1,ny) && istrue(nx,ny+1))
		{
			int cont = 0;
			if(istrue(nx-1,ny+1)) cont++;
			if(istrue(nx+1,ny+1)) cont++;
			if(istrue(nx-1,ny-1)) cont++;
			if(istrue(nx+1,ny-1)) cont++;
			ans[cont]++;
		}
	}
	for(int i = 0; i <= 4; i++)
	{
		cout << ans[i] << endl;
	}
    return 0;
}

pair

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

const int N = 1000;
pair<int,int> p[N];
int ans[10]; // 存放结果
map<pair<int, int>,int> ps;

int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		int x, y;
		cin >> x >> y;
		p[i] = make_pair(x,y);
		ps[p[i]] = 1;
	}
	for(int i = 0; i < n; i++)
	{
		int x = p[i].first;
		int y = p[i].second;
		if(ps[make_pair(x,y-1)] && ps[make_pair(x,y+1)] && ps[make_pair(x+1,y)] && ps[make_pair(x-1,y)])
		{
			int cont = 0;
			if(ps[make_pair(x-1,y+1)]) cont++;
			if(ps[make_pair(x-1,y-1)]) cont++;
			if(ps[make_pair(x+1,y+1)]) cont++;
			if(ps[make_pair(x+1,y-1)]) cont++;
			ans[cont]++;
		}
	}
	for(int i = 0; i <=4; i++)
	{
		cout << ans[i] << endl;
	}
	return 0;
}

太欠缺了 stl都吃不透还好意思说自己写算法喜欢用c++ 害 太丢人了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

moumoumouwang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值