1. 程式人生 > >Triangles(簡單題)

Triangles(簡單題)

很簡單的題目,通過觀察可以發現,每一行的奇數個都是尖頭向下的三角形,偶數個都是尖頭向上的。 所以我們不妨分一下類,對於尖頭向上的就向下擴充套件,反之向上擴充套件。如果可以拓展就更新答案。

細節參見程式碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1000000000;
const int maxn = 100+5;
int n,m,kase = 0;
char s[maxn][2*maxn];
int main() {
    while(~scanf("%d",&n)&&n) {
        getchar();
        int ans = 0;
        for(int i=1;i<=n;i++) gets(s[i]+1);
        for(int i=1;i<=n;i++) {
            int v = 0;
            for(int j=i;j<=2*n-i;j++) {
                ++v;
                if(s[i][j] == '#') continue;
                if(v%2) {
                    int l = j-1, r = j+1, rol = i-1, cur = 1;
                    while(true) {
                        if(l < rol || r > 2*n-rol) break;
                        if(rol < 1) break;
                        bool ok = true;
                        for(int k=l;k<=r;k++) {
                            if(s[rol][k] == '#') { ok = false; break; }
                        }
                        if(ok) cur += (r-l+1), rol--, --l , ++r ;
                        else break;
                    }
                    ans = max(ans,cur);
                }
                else {
                    int l = j-1, r = j+1, rol = i+1, cur = 1;
                    while(true) {
                        if(l < rol || r > 2*n-rol) break;
                        if(rol > n) break;
                        bool ok = true;
                        for(int k=l;k<=r;k++) {
                            if(s[rol][k] == '#') { ok = false; break; }
                        }
                        if(ok) cur += (r-l+1), rol++, --l, ++r;
                        else break;
                    }
                    ans = max(ans,cur);
                }
            }
        }
        printf("Triangle #%d\n",++kase);
        printf("The largest triangle area is %d.\n\n",ans);
    }
    return 0;
}


相關推薦

Triangles簡單

很簡單的題目,通過觀察可以發現,每一行的奇數個都是尖頭向下的三角形,偶數個都是尖頭向上的。 所以我們不妨分一下類,對於尖頭向上的就向下擴充套件,反之向上擴充套件。如果可以拓展就更新答案。 細節參見程式碼: #include<bits/stdc++.h> usin

uva 10474 Where is the Marble?簡單

content mil stdlib.h std lib [0 數據 main pre 我非常奇怪為什麽要把它歸類到回溯上,明明就是簡單排序,查找就OK了。wa了兩次,我還非常不解的懷疑了為什麽會 wa,原來是我居然把要找的數字也排序了,當時僅僅是想著能快一點查找。所以

每日一之 hiho228周 Parentheses Matching 簡單

描述 Given a string of balanced parentheses output all the matching pairs. 輸入 A string consisting of only parentheses ‘(’ and ‘)’. The parenthes

1027 Colors in Mars 20 分簡單

1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a

每日一之 hiho228周 Parentheses Matching 簡單

描述 Given a string of balanced parentheses output all the matching pairs. 輸入 A string consisting of on

7. 反轉整數簡單

給定一個 32 位有符號整數,將整數中的數字進行反轉。 示例 1: 輸入: 123 輸出: 321  示例 2: 輸入: -123 輸出: -321 示例 3: 輸入: 120 輸出: 21 注意: 假設我們的環境只能儲存 32 位有符號整數,其數值範圍

dfs+bfs專題簡單

bfs + 輸出路徑 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <queue>

849B Tell Your World簡單

Tell Your World 題意:給你n個點的座標,問是否有兩條斜率相同但不重合的直線能夠覆蓋所有的點 思路:直線的斜率肯定為(點1和點2組成)直線1,(點2和點3組成)直線2和(點1和點3組成)直線3這三條直線中的一個 因此可以直接列舉三個斜率判

杭電1004簡單

Problem Description Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ f

LeetCode之Roman & Integer 的轉換簡單

羅馬數字和現在使用的阿拉伯整數之間的轉換在LeetCode上一共有兩道題目:Roman to Integer 和Integer to Roman 如標題所說的,這是個簡單的字串處理的題目,基本上知道了規則就可以直接寫出程式碼解決了。 羅馬數字基本規則 做好

HDU 1846 Brave Game巴什博弈超簡單

sca space == div 法則 由於 out con 希望 題目傳送:http://acm.hdu.edu.cn/showproblem.php?pid=1846 Problem Description 十年前讀大學的時候,中國每年都要從國外引進一些電影大片,其中有

HDU 1711 Number Sequence KMP簡單

bmi AMM several rip case ive sts -- 沒有 Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe

python 每日一練之0004➕file方法簡單

前文 前幾天考試太多一直沒時間寫今天繼續 正文 路漫漫~今天做的第四題發現好像是蠻簡單的,但是看網上大佬們選擇的方法都是正則表示式,。。。我這裡貼出一個簡單的方法吧。為那些看不懂正則的小白們貼一貼 # -*- coding: utf-8 -*- def main(words): with

HDU - 1176 免費餡餅dp 倒數塔 簡單

題目連結 簡單的dp題; 題意:每分鐘能得到一個位置上的餅,問最終最多能得到多少; 把每分鐘的看成一層數塔,然後從最後一分鐘往下推; 每次比較往前走,不走,往後走三個位置;   #include <iostream> #include <cstdi

zcmu-1141: 鬆哥的困惑VII披著dp外衣的簡單

1141: 鬆哥的困惑VII Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 285  Solved: 121 [Submit][Status][Web B

LeetCode庫第七簡單系列

題目及要求:給定一個 32 位有符號整數,將整數中的數字進行反轉。示例 1:輸入: 123 輸出: 321  示例 2:輸入: -123 輸出: -321 示例 3:輸入: 120 輸出: 21 注意:假設我們的環境只能儲存 32 位有符號整數,其數值範圍是 [−231, 

LeetCode庫第二十簡單系列

題目及要求:給定一個只包括 '(',')','{','}','[',']' 的字串,判斷字串是否有效。有效字串需滿足:左括號必須用相同型別的右括號閉合。左括號必須以正確的順序閉合。注意空字串可被認為是有效字串。示例 1:輸入: "()" 輸出: true 示例 2:輸入: "

LeetCode庫第二十六簡單系列

簡單系列的第二十一題對於Python有序連結暫時沒有弄明白是怎麼操作的,只是看了別人大神的程式碼過了,所以暫時不寫。 現在由二十六題開始繼續做題。 題目: 給定一個排序陣列,你需要在原地刪除重複出現的元素,使得每個元素只出現一次,返回移除後陣列的新長度。 不要使用額外

Party簡單,但很奇妙

n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, lef

LeetCode庫第十三簡單系列

題目及要求羅馬數字包含以下七種字元:I, V, X, L,C,D 和 M。字元 數值 I 1 V 5 X 10 L 50 C 100 D