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.

Source

Southwestern Europe 1997

今天组队练习的题目,cjx因为要赶学校所以就木有做了。跟想的一样应该用dp。暴力应该也可以。

当n=5时:

1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7
0 0 1 2 3 4 5
0 0 0 1 2 3
0 0 0 0 1

因为输入的前面有空格所以要+k
(1->((n-i+1)*2-1)+k) i=[1...n]

输入:
0 1 0 0 1 1 1 1 0
   1 1 1 1 1 0 1
      1 1 1 0 1
         1 0 1
            1

仔细观察三角形第偶数个的小三角形(无法跟上,左上,右上)组成新三角形。
从上往下遍历:j=[1,3,5,7]
dp[i][j]=min(dp[i-1][j-1],dp[i-1][j+1])+1

0 1 0 0 1 1 1 1 0
   1 1 1 1 2 0 1
      2 1 2 0 1
         3 0 1
           1

同样第奇数个的小三角形(无法跟下,左下,右下)组成新三角形。
从下往上遍历:j=[2,4,6,8]
dp[i][j]=min(dp[i+1][j-1],dp[i+1][j+1])+1

0 1 0 0 1 1 1 1 0
   1 1 1 1 2 0 1
      2 1 2 0 1
         3 0 1
            1

输出:max(dp[i][j])^2

 #include <stdio.h>
#include <string.h>
#include <iostream>
#define MAXN 205
using namespace std; int n;
int dp[MAXN][MAXN]; int main()
{
while( scanf("%d",&n)!=EOF && n ){
memset(dp,,sizeof(dp));
int k=;
char ch;
for(int i=; i<=n; i++){
getchar();
for(int j=; j<=((n-i+)*-)+k; j++){
scanf("%c",&ch);
if(ch==' ' || ch=='#'){
dp[i][j]=;
}
if(ch=='-'){
dp[i][j]=;
}
}
k++;
}
for(int i=; i<=n; i++){
for(int j=i; j<=*n-i; j+=){
if(dp[i][j] && dp[i-][j]){
dp[i][j]=min( dp[i-][j-] , dp[i-][j+] )+;
}
}
}
for(int i=n-; i>=; i--){
for(int j=i+; j<=*n-i; j+=){
if(dp[i][j] && dp[i+][j]){
dp[i][j]=min( dp[i+][j-] , dp[i+][j+] )+;
}
}
}
int ans=-;
for(int i=; i<=n; i++){
for(int j=i; j<=*n-i; j++){
if(dp[i][j]>ans){
ans=dp[i][j];
}
}
}
printf("Triangle #%d\n",++c);
printf("The largest triangle area is %d.\n",ans*ans);
puts("");
}
return ;
}

TOJ 1885 Triangles的更多相关文章

  1. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  2. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  3. [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)

    Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...

  4. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  5. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  6. hdu 1885 Key Task

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...

  7. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  8. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  9. Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题

    D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

随机推荐

  1. iOS wkwebview https 加载不受信用的站点

    iOS 9.0以上直接设置WKNavigationDelegate代理 func webView(_ webView: WKWebView, didReceive challenge: URLAuth ...

  2. markdown syntax

    Markdown 语法 转载自https://zh.mweb.im/markdown.html 首先应该了解的 每一个 Markdwon 使用者都应该了解的,是 Markdown 最基本的版本,也就是 ...

  3. 死磕Java之聊聊ThreadLocal源码(基于JDK1.8)

    记得在一次面试中被问到ThreadLocal,答得马马虎虎,所以打算研究一下ThreadLocal的源码 面试官 : 用过ThreadLocal吗? 楼主答 : 用过,当时使用ThreadLocal的 ...

  4. OC 术语表

    术语表 本附录包含了很多会用到的非正式定义术语.有些术语与Obective-C语言有关,其他术语则有自己的语源,来自面向对象程序设计的规范.在后一种情况中,术语的含义只有明确应用于Obective-C ...

  5. DIV做的Table

    <style> div.table{ border:1px solid #d7d7d7; margin-left:0px; border-bottom-width:; width:1200 ...

  6. JsonToHtml

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. gitlab 服务器的搭建与使用全过程(二)

    <gitlab操作手册 1.0 > 此手册适用于 Mac 计算机 第一步:根据从管理员得到的用户名和初始密码登陆并修改密码.新密码不得少于8个字符 第二步:在自己的电脑上创建密钥,并提交提 ...

  8. SQl 根据某列去重 partition by

    主键为ID select * from [infotops] where Id in (select max(id) from [infotops] group by InfoId) -------- ...

  9. spring配置文件中导入约束的详细步骤

    这里先以<beans>元素为例: 首先在eclipse中引入相关约束: 点击OK后,这个约束就被引入到eclipse中了,这一步的意义在于:就算你处于脱机情况下(不能联网),也能给你提示. ...

  10. ggplot你不知道的细节

    例一 Michaelis-Menten动力学方程 这个例子中采用出自文献中的一组有关于浮萍氮摄取的数据,共2两个变量8个观测值,其中底物浓度与浮萍的氮取速率之间可以通过M-M动力学方程来进行描述.在这 ...