TOJ 1885 Triangles
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
今天组队练习的题目,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的更多相关文章
- Count the number of possible triangles
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)
Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...
- acdream.Triangles(数学推导)
Triangles Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Stat ...
- UVA 12651 Triangles
You will be given N points on a circle. You must write a program to determine how many distinctequil ...
- hdu 1885 Key Task
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1885 Key Task Description The Czech Technical Univers ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- 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 ...
- 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 ...
随机推荐
- background-image属性的设置
对于图片,首先我们先想到是背景图片.因为我们许许多的装饰都是用背景图片来实现的.既然这样,那么就从CSS控制背景图片讲起吧.1.CSS控制背景图片: 对于一个网页,我们开始设计的时候,可能没有过 ...
- Visual Studio 2013 新增功能:“Browser Link”
今天新装了 Visual Studio 2013, 使用 VS2013 打开一个现有的 WEB 网站, 在调试网站的时候出现在了脚本错误,一个文件名叫 "browserLink", ...
- C#设计模式系列:适配器模式(Adapter Pattern)
一.引言 在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象”放在新的环境中应用.但是新环境要求的接口是这些现存对象所不满足的.如何应对这种“迁移的变化”?如何既能利用现有对象的良好实现,同 ...
- NPOI 2.1.3.1导入Excel
引入NPOI 2.1.3.1的包 项目引入 using NPOI.XSSF.UserModel;using NPOI.SS.UserModel; 控制器方法: public ActionResult ...
- php统计目录大小
function dirSize($directroy) { $dir_size=0; $dir_handle = @opendir($directroy); if($dir_handle) { wh ...
- 【Es】jest操作elasticsearch
https://blog.csdn.net/niuchenliang524/article/details/82869319 操作es的客房端有多个,在此例出三种(具体区别自行百度),本文讲的是jes ...
- GitHub 十大 CI 工具
简评:GitHub 上最受欢迎的 CI 工具. 持续集成(Continuous integration)指的是,频繁地(一天多次)将代码集成到主干. 持续集成工具让产品可以快速迭代,同时还能保持高质量 ...
- 「模拟赛20190327」 第二题 DP+决策单调性优化
题目描述 小火车虽然很穷,但是他还是得送礼物给妹子,所以他前往了二次元寻找不需要钱的礼物. 小火车准备玩玩二次元的游戏,游戏当然是在一个二维网格中展开的,网格大小是\(n\times m\)的,某些格 ...
- upsource代码审查
upsource 从零搭建代码审查平台,需要的不仅是把代码审查的工具搭起来,还要结合公司情况制定一系列的代码审查规范.下面是对选择的upsource web端代码审查工具的安装及介绍.详细的请看这篇文 ...
- [Beta阶段]第一次Scrum Meeting
Scrum Meeting博客目录 [Beta阶段]第一次Scrum Meeting 基本信息 名称 时间 地点 时长 第一次Scrum Meeting 19/04/29 大运村寝室6楼 70min ...