Test SRM Level Three: LargestCircle, Brute Force
题目来源: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的更多相关文章
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意 ...
- DVWA实验之Brute Force(暴力破解)- High
DVWA实验之Brute Force(暴力破解)- High 有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2046/p/10928380.ht ...
- DVWA实验之Brute Force(暴力破解)- Low
DVWA实验之Brute Force-暴力破解- Low 这里开始DVWA的相关实验~ 有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2 ...
- DVWA(二): Brute Force(全等级暴力破解)
tags: DVWA Brute Force Burp Suite Firefox windows2003 暴力破解基本利用密码字典使用穷举法对于所有的账号密码组合全排列猜解出正确的组合. LEVEL ...
- 小白日记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 [ ...
- 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 ...
- 安全性测试入门:DVWA系列研究(一):Brute Force暴力破解攻击和防御
写在篇头: 随着国内的互联网产业日臻成熟,软件质量的要求越来越高,对测试团队和测试工程师提出了种种新的挑战. 传统的行业现象是90%的测试工程师被堆积在基本的功能.系统.黑盒测试,但是随着软件测试整体 ...
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- hdu6215 Brute Force Sorting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...
随机推荐
- Spark&Spark性能调优实战
Spark特别适用于多次操作特定的数据,分mem-only和mem & disk.当中mem-only:效率高,但占用大量的内存,成本非常高;mem & disk:内存用完后,会自己主 ...
- ps中图层混合模式、多图层叠加、不透明度、填充、图层样式详解
图像领域中,通过进行一下想法的时候,都要通过用ps看下是不是合理,而ps中图层是必用的一个功能,下面详解一下图层有关的叠加原理. 基本顺序是图层从下往上继续, 先计算图层的填充,再计算样式.最后计算不 ...
- 在Windows系统上实现轻量级的线程间及进程间消息队列
Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段. 利用Windows的Naming Object可以实现一套简单的Inter-Thread ...
- Threejs 的场景查看 - 几个交互事件库助你方便查看场景
Threejs 的场景查看 - 几个交互事件库助你方便查看场景 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&q ...
- 在MVC应用程序中动态加载PartialView
原文:在MVC应用程序中动态加载PartialView 有时候,我们不太想把PartialView直接Render在Html上,而是使用jQuery来动态加载,或是某一个事件来加载.为了演示与做好这个 ...
- ON、WHERE、HAVING的差别
ON .WHERE.HAVING都能通过限制条件筛选数据,但他们的使用及其不同.以下我们来分析三者之间的差别. 1. ON 和WHERE 全部的查询都回产生一个中间暂时报表,查询结果就是从 ...
- 分享3一个博客HTML5模板
1.材类别:半透明 博客html模板 个人博客 半透明html5博客主题,半透明,博客,博客html模板,个人博客,html5,灰色,半透明html5博客主题是一款适合用于个人博客主题,风格非常不错. ...
- 一个linux常见命令的列表
这是一个linux常见命令的列表. 那些有• 标记的条目,你可以直接拷贝到终端上而不需要任何修改,因此你最好开一个终端边读边剪切&拷贝. 所有的命令已在Fedora和Ubuntu下做了测试 命 ...
- Java流的理解
最近做了一下Socket编程,其中有socket.getInputStream和socket.getOutputStream的问题. 想传输文件,感觉应该用FileInputStream和FileOu ...
- C++ 在字符串中插入子串+推断字符串是否由空格组成
// Example3.cpp : 定义控制台应用程序的入口点. #include "StdAfx.h" #include <string> #include < ...