[ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)
Description
As you know, in one month it is Christmas and this year you are honored to make the big star that will be stuck on the top of the Christmas tree. But when you get the triangle-patterned silver paper you realize that there are many holes in it. Your little sister has already cut out smaller triangles for the normal Christmas stars. Your only chance is to find an algorithm that tells you for each piece of silver paper the size of the largest remaining triangle.
Given a triangle structure with white and black fields inside you must find the largest triangle area of white fields, as shown in the following figure. 
Input
For each triangle, the number of the characters `#' and `-' per line is odd and decreases from 2n - 1 down to 1.
The input is terminated by a description starting with n = 0.
Output
Output a blank line after each test case.
Sample Input
5
#-##----#
-----#-
---#-
-#-
-
4
#-#-#--
#---#
##-
-
0
Sample Output
Triangle #1
The largest triangle area is 9. Triangle #2
The largest triangle area is 4.
题目意思是:给你一个高为n的三角形,其中#表示黑色,-表示白色,问这个里面最大的3角形的面积(其中每个小的3角形面积为1)。算法其实很简单,就是遍历每一个白色小三角形,如果它是朝上的(行列相加为奇数)就把它看为最大三角形的顶部,向下逐层搜索,看最大能为几层;同理当其朝下时,就逐层向上搜索,求最大层。然后用maxNum维护一个最终的最大层(注意这个数最好初始化为0!!!)。将输入3角形转换为从(1,1)开始的0-1矩阵(这样可以省去边界处理),要注意数组要开的大一点!!!
#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
using namespace std;
int n,tab[][];
int proNum,maxNum;
bool lu(int I,int J){//向上找
for(int i=;i<*proNum+;i++)
if(!tab[I-proNum][J-proNum+i])return ;
return ;
}
bool ld(int I,int J){//向下找
for(int i=;i<*proNum+;i++)
if(!tab[I+proNum][J-proNum+i])return ;
return ;
}
void dfs(int I,int J){
if((I+J)%==){//朝下,向上找
while(lu(I,J)){
proNum++;
}
}else{//朝上,向下找
while(ld(I,J)){
proNum++;
}
}
}
void solve(){
maxNum=;//初始为0(针对全黑情况)
for(int i=;i<=n;i++){
for(int j=i;j<=*n-i;j++){
if(tab[i][j]){//尝试将所有白三角形当成顶点看其最大层数
proNum=;
dfs(i,j);
if(proNum>maxNum)maxNum=proNum;//更新
}
}
}
}
int main(){
string str;int casee=;
while(cin>>n && n){
memset(tab,,sizeof(tab));
for(int i=;i<=n;i++){
cin>>str;
for(int j=;j<str.size();j++){
if(str[j]=='-')tab[i][j+i]=;
}
}
solve();
cout<<"Triangle #"<<casee++<<'\n';
cout<<"The largest triangle area is "<<maxNum*maxNum<<".\n\n";
}return ; }
[ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)的更多相关文章
- 搜索入门_简单搜索bfs dfs大杂烩
dfs题大杂烩 棋盘问题 POJ - 1321 和经典的八皇后问题一样. 给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...
- 《ElasticSearch6.x实战教程》之简单搜索、Java客户端(上)
第五章-简单搜索 众里寻他千百度 搜索是ES的核心,本节讲解一些基本的简单的搜索. 掌握ES搜索查询的RESTful的API犹如掌握关系型数据库的SQL语句,尽管Java客户端API为我们不需要我们去 ...
- ElasticSearch 5学习(4)——简单搜索笔记
空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...
- [转载]SharePoint 2013搜索学习笔记之搜索构架简单概述
Sharepoint搜索引擎主要由6种组件构成,他们分别是爬网组件,内容处理组件,分析处理组件,索引组件,查询处理组件,搜索管理组件.可以将这6种组件分别部署到Sharepoint场内的多个服务器上, ...
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- 简单搜索dfs, 简单的修剪搜索
选择最合适的语言做一个项目是非常重要的.但,熟练的掌握自己的武器,这也是非常重要的. ========================================================= ...
- 分布式搜索ElasticSearch构建集群与简单搜索实例应用
分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- js算法初窥03(简单搜索及去重算法)
前面我们了解了一些常用的排序算法,那么这篇文章我们来看看搜索算法的一些简单实现,我们先来介绍一个我们在实际工作中一定用到过的搜索算法--顺序搜索. 1.顺序搜索 其实顺序搜索十分简单,我们还是以第一篇 ...
随机推荐
- Python 2.7_First_try_爬取阳光电影网_20161206
之前看过用Scrapy 框架建立项目爬取 网页解析时候用的Xpath进行解析的网页元素 这次尝试用select方法匹配元素 1.入口爬取页面 http://www.ygdy8.com/index.ht ...
- JSTL和EL的区别
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1.2 ...
- RESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))
无法将类型为"Microsoft.Office.Interop.Excel.ApplicationClass"的 COM 对象强制转换为接口类型"Microsoft.Of ...
- Hosting static website on AWS
http://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html#root-d ...
- [2015hdu多校联赛补题]hdu5302 Connect the Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...
- .NET实现高效过滤敏感查找树算法(分词算法):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- VS2010无法修改资源文件
最近,因为公司开发的需要,对开发环境进行全面的升级,在这其中也遇到了不少问题,在之后将陆续整理出来,以便以后查看. 之前开发环境:VS2008,ArcGIS9.3,ArcEngine9.3,Oracl ...
- MVC4下配置log4net 五部曲
第一步:把log4net.dll 编译成Framework 4.0 第二步:找到项目的Properties下的AssemblyInfo.在最下面添加:[assembly: log4net.Config ...
- Android编译环境搭建(0818-0819)
1 在虚拟机VMware上安装64位Ubuntu14.04LTS 首先需要安装虚拟机并激活.然后新建虚拟机,选择使用下载好的Ubuntu镜像.注意需要将光驱改为自己下载的,而不是autoinst.is ...
- NC WebService接口开发流程
一.定义类: 接口类 包定义在public下,接口类名为I开头,Service结尾 实现类 包定义在private下,实现类名以ServiceImpl结尾 VO类 若有VO类,也放在public下 U ...