题目来源: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. 30分钟快速掌握AngularJs

    [后端人员耍前端系列]AngularJs篇:30分钟快速掌握AngularJs   一.前言 对于前端系列,自然少不了AngularJs的介绍了.在前面文章中,我们介绍了如何使用KnockoutJs来 ...

  2. 【图像处理】Bilinear Image Scaling

    Bilinear image scaling is about the same as nearest neighbor image scaling except with interpolation ...

  3. 跟我一起学extjs5(13--运行菜单命令在tabPanel中显示模块)

    跟我一起学extjs5(13--运行菜单命令在tabPanel中显示模块)         上面设计好了一个模块的主界面,以下通过菜单命令的运行来把这个模块增加到主界面其中. 在MainModule. ...

  4. scala 函数编程

     scala 函数编程  Effective Scala.pdf: http://www.t00y.com/file/76767869 Functional_Programming_in_Scal ...

  5. STL的一些泛型算法

    源地址:http://blog.csdn.net/byijie/article/details/8142859 从福州大学资料里摘下来的我现在能理解的泛型算法 algorithm min(a,b) 返 ...

  6. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  7. 32位Linux文件限制大小

    线上程序不断重新启动,查看log发现是进程由于SIGXFSZ信号退出.对过大的文件进行操作的时候会产生此信号,一般仅仅在32位机器上出现,文件限制大小为2G.用lsof查看进程打开的文件,果然有一个文 ...

  8. veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响

    veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响 续接veridata实验系列  上篇:"veridata实验举例(5)改动主键上的列值.update ...

  9. WP开发使用BingMaps地图服务

    原文:WP开发使用BingMaps地图服务 WP8使用BingMaps地图在 SOAP服务如何计算路径 首先需要用到3个服务 1.GeoCode服务-转换地址到地理的经纬度(WebServices地址 ...

  10. Python重写C语言程序100例--Part6

    ''' [程序41] 题目:学习static定义静态变量的使用方法 1.程序分析: 2.程序源码: ''' # python没有这个功能了,仅仅能这样了:) def varfunc(): var = ...