题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858

思路:

如果直接用Brute Force搜索所有可能的圆的话,那么搜索空间将很大,所以我用了一个priority_queue结构,将搜索的顺序变为按圆的半径从大到小搜索,所以当搜索到符合条件的圆时,即可停止搜索。这样可以大大减少搜索范围,不过对于最坏的情况,也就是没有符合条件的圆时,还是会将所有的可能情况都搜索到。

代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <cmath> using namespace std; typedef pair <int, pair<int, int> > entry;
#define make_entry(radius, row, col) ( make_pair( (radius), make_pair( (row), (col) ) ) ) class LargestCircle
{
public:
int radius(vector <string> grid);
}; bool checkCircle(int cusRadius, int cus_row, int cus_col, vector<string> & grid);
int LargestCircle::radius(vector<string> grid)
{
int i, j;
int rows, cols, cus_row, cus_col;
priority_queue <entry> PQ;
int maxRadius, cusRadius; rows = grid.size();
cols = grid[0].size();
for (i = 0; i < rows - 1; i++) {
for (j = 0; j < cols - 1; j++) {
PQ.push(make_pair(min(min(i + 1, j + 1), min(rows - i - 1, cols - j - 1)),
make_pair(i, j)));
}
} maxRadius = 0; while ( !PQ.empty() ) {
cusRadius = PQ.top().first;
cus_row = PQ.top().second.first;
cus_col = PQ.top().second.second;
PQ.pop(); if ( checkCircle(cusRadius, cus_row, cus_col, grid) ) {
maxRadius = cusRadius;
break;
}
--cusRadius;
if (cusRadius > 0) {
PQ.push(make_entry(cusRadius, cus_row, cus_col));
}
} return maxRadius;
} /**
* 检查该圆是否合法
*/
bool checkCircle(int cusRadius, int cus_row, int cus_col, vector<string> & grid)
{
int i, j;
double pre_bias, next_bias;
int pre_cell, next_cell;
int next_row, next_col; next_bias = 0;
for (i = 0; i < cusRadius; i++) {
pre_bias = next_bias; /* pre_bias值与上一个循环中的 next_bias值相等 */
next_bias = sqrt( pow( (double)cusRadius, 2 ) - pow( (double)(cusRadius-i - 1), 2 ) ); pre_cell = (int)pre_bias;
next_cell = (int)next_bias;
if ( abs( next_bias - next_cell > 0.00001)) {
++next_cell;
} for (j = 0; j < next_cell - pre_cell; j++) {
next_row = cus_row - (cusRadius - i) + 1;
next_col = cus_col - pre_cell - j;
if ('#' == grid[next_row][next_col] ||
'#' == grid[next_row][2 * cus_col + 1 - next_col] || // 右对称点
'#' == grid[2 * cus_row + 1 - next_row][next_col] || // 下对称点
'#' == grid[2 * cus_row + 1 - next_row][2 * cus_col + 1 - next_col]) { //中心对称
return false;
}
}
} return true;
}

Test SRM Level Three: LargestCircle, Brute Force的更多相关文章

  1. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  2. DVWA实验之Brute Force(暴力破解)- High

    DVWA实验之Brute Force(暴力破解)- High   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2046/p/10928380.ht ...

  3. DVWA实验之Brute Force(暴力破解)- Low

    DVWA实验之Brute Force-暴力破解- Low     这里开始DVWA的相关实验~   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2 ...

  4. DVWA(二): Brute Force(全等级暴力破解)

    tags: DVWA Brute Force Burp Suite Firefox windows2003 暴力破解基本利用密码字典使用穷举法对于所有的账号密码组合全排列猜解出正确的组合. LEVEL ...

  5. 小白日记46:kali渗透测试之Web渗透-SqlMap自动注入(四)-sqlmap参数详解- Enumeration,Brute force,UDF injection,File system,OS,Windows Registry,General,Miscellaneous

    sqlmap自动注入 Enumeration[数据枚举] --privileges -U username[CU 当前账号] -D dvwa -T users -C user --columns  [ ...

  6. nginx 1.3.9/1.4.0 x86 Brute Force Remote Exploit

    测试方法: 本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! #nginx 1.3.9/1.4.0 x86 brute force remote exploit # copyri ...

  7. 安全性测试入门:DVWA系列研究(一):Brute Force暴力破解攻击和防御

    写在篇头: 随着国内的互联网产业日臻成熟,软件质量的要求越来越高,对测试团队和测试工程师提出了种种新的挑战. 传统的行业现象是90%的测试工程师被堆积在基本的功能.系统.黑盒测试,但是随着软件测试整体 ...

  8. HDU 6215 Brute Force Sorting(模拟链表 思维)

    Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  9. hdu6215 Brute Force Sorting

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...

随机推荐

  1. HDU 3911 Black And White 分段树 题解

    Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little ...

  2. 近期在调用 calendar.js 的时候出现中文乱码! 解决方式

    近期写一个小项目的时候:在调用 calendar.js  的时候出现中文乱码! 如图所看到的: 原因在于: 我的jsp 页面,指定的是 UTF-8 编码,然而,调用的 calendar.js 的编码确 ...

  3. nginx 区分pc和mobile 到不同的404页面

    if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry|vivo)') { set $mobile_request '1'; } ...

  4. PCI 总线学习笔记

    转载请注明出处:http://blog.csdn.net/lg2lh/article/details/8042008 PCI的基本协议这里就不介绍了,由于一般的芯片协议都是集成好的,我仅仅须要大体了解 ...

  5. FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区

    FreeNAS 9.1.1 发布,网络存储系统 - 开源中国社区 FreeNAS 9.1.1 发布,网络存储系统

  6. poj3694(tarjan缩点+lca)

    传送门:Network 题意:给你一个连通图,然后再给你n个询问,每个询问给一个点u,v表示加上u,v之后又多少个桥. 分析:方法(1219ms):用并查集缩点,把不是桥的点缩成一个点,然后全图都是桥 ...

  7. premake 使用clang替换gcc

    接着前文:premake在Ubuntu和GCC环境下创建简单的C++工程 由于clang支持gcc所有参数,所以使得在premake中替换gcc变得很简单.基本上就是通过传递参数或者设置环境变量的方式 ...

  8. Android LCD(三):Samsung LCD接口篇

    关键词:android LCD控制器 Framebuffer PWM  平台信息: 内核:linux2.6/linux3.0 系统:android/android4.0  平台:samsung exy ...

  9. Smarty中模板eq相等 ne、neq不相等, gt大于, lt小于

    eq相等   ne.neq不相等,   gt大于, lt小于 gte.ge大于等于   lte.le 小于等于   not非   mod求模   is [not] div by是否能被某数整除   i ...

  10. Source not found for StandardEngine(ContainerBase).initInternal() line: 1078

    总是这样 在复制完一个项目,并重新起了个名字后. 再打开网页就怎么也打开不了.   第一反应是tomcat出问题了. 于是有了这样的问题: Source not found for StandardE ...