题目传送门
题目描述
暴力遍历
分析
代码
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
struct POI
{
int x, y;
int cnt;
int score;
};
vector<POI> h;
int n;
int res[6];
bool check1(int x1, int y1, int x2, int y2)
{
if(x2 == x1-1 && y2 == y1) return true;
if(x2 == x1+1 && y2 == y1) return true;
if(x2 == x1 && y2 == y1 - 1) return true;
if(x2 == x1 && y2 == y1 + 1) return true;
return false;
}
bool check2(int x1, int y1, int x2, int y2)
{
if(x2 == x1 - 1 && y2 == y1 - 1) return true;
if(x2 == x1 + 1 && y2 == y1 + 1) return true;
if(x2 == x1 - 1 && y2 == y1 + 1) return true;
if(x2 == x1 + 1 && y2 == y1 - 1) return true;
return false;
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
POI p;
scanf("%d%d", &p.x, &p.y);
p.cnt = 0;
p.score = 0;
h.push_back(p);
}
for(int i = 0; i < n; i++)
{
int x = h[i].x;
int y = h[i].y;
for(int j = 0; j < n; j++)
{
if(j == i) continue;
int bx = h[j].x, by = h[j].y;
if(check1(x, y, bx, by)) h[i].cnt++;
if(check2(x, y, bx, by)) h[i].score++;
}
}
for(int i = 0; i < n; i++)
{
if(h[i].cnt == 4)
res[h[i].score]++;
}
for(int k = 0; k < 5; k++) printf("%d\n", res[k]);
return 0;
}
时间复杂度
参考文章