[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.顺序搜索 其实顺序搜索十分简单,我们还是以第一篇 ...
随机推荐
- {C#}{GDI+}各种C#,GDI+的资料
GDI+各种功能: http://www.cnblogs.com/08shiyan/category/253906.html 字体:http://blog.sina.com.cn/s/blog_7c7 ...
- JQuery中$(document)是什么意思有什么作用
$(document).ready(fn):当DOM载入就绪可以查询及操纵时绑定一个要执行的函数,因为它可以极大地提高web应用程序的响应速度 首先我解释一下jQuery jQuery有一个用来作为D ...
- construction of tuples containing 0 or 1 items
[construction of tuples containing 0 or 1 items] the syntax has some extra quirks to accommodate the ...
- POJ 1873 - The Fortified Forest 凸包 + 搜索 模板
通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...
- margin设置为百分比的含义
<!DOCTYPE html> <html> <head> <title>magin为百分比</title> </head> & ...
- [转] Eclipse 编辑相关快捷键
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编辑的好帮手,能为用户提供内 ...
- javascript arguments与javascript函数重载
1.所 有的函数都有属于自己的一个arguments对象,它包括了函所要调用的参数.他不是一个数组,如果用typeof arguments,返回的是’object’.虽然我们可以用调用数据的方法来调用 ...
- shell脚本学习
1.注释 如果使用bash,则在脚本文件头注释:#/bin/bash2.将脚本文件加上可读与执行权限,就可以使用./shell.sh来执行,也可以使用sh shell.sh的方式来直接执行,sh是ba ...
- mac下java 开发环境搭建
mac配置java开发环境: jdk1.7 +sdk1.7+maven +tomcat 1.先安装jdk ,才能安装sdk . 2 mac中jdk1.7的默认位置:/Library/Java/Ja ...
- UML用例图中泛化、扩展、包括
在画用例图的时候,理清用例之间的关系是重点.用例的关系有泛化(generalization).扩展(extend)和包含(include).其中include和extend最易混淆.下面我们结合实例彻 ...