题意:给出n行m列的01字符,找出圆,其中圆的边是1表示。
分析:拿到题一脸懵逼,作者说方法很多,但我就是想不到,看了一下大佬的解法,只能卧槽,幸好题目的数据比较小,这题也是一道关于误差处理的。
#include<iostream>
#include<string.h>
#include<sstream>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
const double PI = acos(-1);
struct circle {
int x, y, r;
circle(int _r,int _x,int _y):r(_r),x(_x),y(_y){}
bool operator<(const circle &u) {
if (r != u.r)return r < u.r;
if (x != u.x)return x < u.x;
return y < u.y;
}
};
int mp[100 + 10][100 + 10];
int main()
{
int kase; int t=0;
cin >> kase;
while (kase--) {
int h, w;
vector<circle>ans;
cin >> w >> h;
char c;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
{
cin >> c;
mp[i][j] = c - '0';
}
for (int r = 5; r <= 50; r++) {
for (int x = r; x < w - r + 1; x++) {
for (int y = r; y < h - r + 1; y++) {
int k1 = 0, k2 = 0;
for (int i = 0; i < 100; i++) {
double angle = rand() / (RAND_MAX + 1.0) * 2 * PI;//随机枚举100个方向上的边界
int dx = int(x + r * cos(angle) + 0.5); int dy = int(y + r * sin(angle) + 0.5);//误差为0.5
if (dx >= 0 && dx <= w - 1 && dy >= 0 && dy <= h - 1 && mp[dy][dx])k1++;
k2++;
if(k2>10&&2*k1<k2){//如果枚举数量的一半的点都不符合条件直接退出
break;
}
}
if (k1 / (double)k2 > 0.8)ans.push_back(circle(r, x, y));
}
}
}
cout << "Case " << ++t << ": " << ans.size();
for (int i = 0; i < ans.size(); i++)cout << " " <<"("<<ans[i].r<<","<<ans[i].x<<","<<ans[i].y<<")";
cout << endl;
}
return 0;
}