Description

It is always very nice to have little brothers or sisters. You can tease them, lock them in the bathroom or put red hot chili in their sandwiches. But there is also a time when all meanness comes back!

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

The input contains several triangle descriptions. The first line of each description contains an integer n (1 <= n <= 100), which gives the height of the triangle. The next n lines contain characters of the set {space, #, -} representing the rows of the triangle, where `#' is a black and `-' a white field. The spaces are used only to keep the triangle shape in the input by padding at the left end of the lines. (Compare with the sample input. The first test case corresponds to the figure.) 
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

For each triangle in the input, first output the number of the triangle, as shown in the sample output. Then print the line "The largest triangle area is a.", where a is the number of fields inside the largest triangle that consists only of white fields. Note that the largest triangle can have its point at the top, as in the second case of the sample input.

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,简单搜索,注意细节)的更多相关文章

  1. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

  2. 《ElasticSearch6.x实战教程》之简单搜索、Java客户端(上)

    第五章-简单搜索 众里寻他千百度 搜索是ES的核心,本节讲解一些基本的简单的搜索. 掌握ES搜索查询的RESTful的API犹如掌握关系型数据库的SQL语句,尽管Java客户端API为我们不需要我们去 ...

  3. ElasticSearch 5学习(4)——简单搜索笔记

    空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...

  4. [转载]SharePoint 2013搜索学习笔记之搜索构架简单概述

    Sharepoint搜索引擎主要由6种组件构成,他们分别是爬网组件,内容处理组件,分析处理组件,索引组件,查询处理组件,搜索管理组件.可以将这6种组件分别部署到Sharepoint场内的多个服务器上, ...

  5. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  6. 简单搜索dfs, 简单的修剪搜索

    选择最合适的语言做一个项目是非常重要的.但,熟练的掌握自己的武器,这也是非常重要的. ========================================================= ...

  7. 分布式搜索ElasticSearch构建集群与简单搜索实例应用

    分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...

  8. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  9. js算法初窥03(简单搜索及去重算法)

    前面我们了解了一些常用的排序算法,那么这篇文章我们来看看搜索算法的一些简单实现,我们先来介绍一个我们在实际工作中一定用到过的搜索算法--顺序搜索. 1.顺序搜索 其实顺序搜索十分简单,我们还是以第一篇 ...

随机推荐

  1. VC++ chap12 file

    file operation _______C语言对文件操作的支持 fopen accepts paths that are valid on the file system at the point ...

  2. 博客的开端,找对象不再new

    今天是第一次用blog,小白开始完善了!! 希望大家多多照顾一下.

  3. WPF 设置透明度和圆形图片

    1 设置效果为

  4. [转载] 散列表(Hash Table) 从理论到实用(下)

    转载自: 白话算法(6) 散列表(Hash Table) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...

  5. 数据库管理员<三>

    数据库管理员  介绍 每个 oracle 数据库应该至少有一个数据库管理员(dba),对于一个小的数据库,一个 dba 就够了,但是对于一个大的数据库 可能需要多个 dba 分担不同的管理职责.那么 ...

  6. apache的安装,启动和停止

    一.apache服务器的安装 安装步骤直接傻瓜式进行安装.并没有太大的难点.apache的配置是学习的重点和难点. 安装好后再浏览器地址栏输入http://localhost.若能够成功安装,则会显示 ...

  7. 【erlang】执行linux命令的两种方法

    os.cmd(Cmd) os模块提供了cmd函数可以执行linux系统shell命令(也可以执行windows命令).返回一个Cmd命令的标准输出字符串结果.例如在linux系统中执行os:cmd(& ...

  8. 【jQuery】: 定时刷新页面

    <%@page import="qflag.ucstar.seatmonitor.manager.SeatMonitorManager"%><%@ page la ...

  9. MVC之校验

    MVC校验 首先要在Models中创建几个属性 例子:Id.UserName.Age属性.然后创建控制器,然后添加一个试图,选择强类型,选择支架模板Create生成页面,然后将所有控件改为TextBo ...

  10. 如何利用java得到当前的时间和前一天的时间

    1.得到当前的时间 Calendar   cal   =   Calendar.getInstance(); String time=formatter.format(cal.getTime()); ...