zl程序教程

您现在的位置是:首页 >  其它

当前栏目

CSP-201912-2-回收站选址

CSP 回收站 选址
2023-09-27 14:26:26 时间

回收站选址(传送门)

一道水题,读懂题慢慢写就好了

样例1输入
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1
样例1输出
0
0
1
0
0
样例2输入
2
0 0
- 100000 10
样例2输出
0
0
0
0
0
样例3输入
11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11
样例3输出
0
2
1
0
0

满分代码

#include <bits/stdc++.h>

using namespace std;

int n,coun=0;
int a[1001][2];
int c[6];

bool isExist(int ax,int ay){
	for(int i = 0; i < n; i++){
		if(a[i][0] == ax && a[i][1] == ay){
			return true;
		}
	}
	return false;
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
		cin >> a[i][0] >> a[i][1];
	}
	
	for(int i = 0; i < n; i++){
		if( isExist(a[i][0]+1,a[i][1]) && isExist(a[i][0]-1,a[i][1]) && isExist(a[i][0],a[i][1]+1) && isExist(a[i][0],a[i][1]-1) ){
			coun = 0;
			if(isExist(a[i][0]+1,a[i][1]+1)){
				coun++;
			}
			if(isExist(a[i][0]+1,a[i][1]-1)){
				coun++;
			}
			if(isExist(a[i][0]-1,a[i][1]+1)){
				coun++;
			}
			if(isExist(a[i][0]-1,a[i][1]-1)){
				coun++;
			}
			c[coun]++;
		}
	}
	
	for(int i = 0; i < 5; i++){
		cout << c[i] << endl;
	}
    return 0;
}

第二次做

#include <bits/stdc++.h>

using namespace std;

vector<pair<int,int> > v;
map<pair<int,int>, int> m;
int num[5];

int n;

int main(){
	cin >> n;
	memset(num,0,sizeof num);
	
	for(int i=0; i<n; i++) {
		int x,y;
		cin >> x >> y;
		v.push_back({x,y});
		m[{x,y}] = 1;
	}
	
	for(int i=0; i<v.size(); i++) {
		int x = v[i].first;
		int y = v[i].second;
		
		if(m.count({x+1,y}) && m.count({x-1,y}) && m.count({x,y+1}) && m.count({x,y-1})) {
			int c = 0;
			if(m.count({x+1,y+1})) {
				c++;
			}
			if(m.count({x+1,y-1})) {
				c++;
			}
			if(m.count({x-1,y+1})) {
				c++;
			}
			if(m.count({x-1,y-1})) {
				c++;
			}
			num[c]++;
		}
	}
	
	for(int v : num) {
		cout << v << endl;
	}
	
	return 0;
}

这里是题目O(∩_∩)O,欢迎大家留言,有空的话可以点个赞哦(#^ . ^#)

       试题编号:
201912-2
试题名称:回收站选址
时间限制:1.0s
内存限制:256.0MB
问题描述: