POJ 1471 Triangles(dfs)

题目链接

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.

题意 :

给出如图的三角形,找出最大的白色三角形面积。

解题思路:

  搜索,遍历整个三角形,如果当前是白色三角形,把它看做顶点进行搜索,注意看图,如果(x + y)是奇数时,是倒三角,只能向下搜索,偶数时向上搜索。这样找出最大三角形的高度Max,结果为Max^2。

//代码比较乱,大家将就看吧。。。。
/*
                                    _ooOoo_
                                   o8888888o
                                   88" . "88
                                   (| -_- |)
                                    O\ = /O
                                ____/`---'\____
                              .   ' \\| |// `.
                               / \\||| : |||// \
                             / _||||| -:- |||||- \
                               | | \\\ - /// | |
                             | \_| ''\---/'' | |
                              \ .-\__ `-` ___/-. /
                           ___`. .' /--.--\ `. . __
                        ."" '< `.___\_<|>_/___.' >'"".
                       | | : `- \`.;`\ _ /`;.`/ - ` : | |
                         \ \ `-. \_ __\ /__ _/ .-` / /
                 ======`-.____`-.___\_____/___.-`____.-'======
                                    `=---='

                 .............................................
*/
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define pi 3.1415926
#define INF 123456789
using namespace std;
char str[505][505];
int flag[505][505], n;
int dfs(int x, int y){
    int b = y, f = 0, e = y, q;
    int xx = x;
    int sum1 = 1;
    if((y + x) % 2 == 1){                       //对当前顶点进行奇偶判断
        while(1){
            for(q = b - 1; q <= e + 1; q ++){ 
                if(flag[xx + 1][q] != 1){       //注意这里,向下搜就遍历当前行的下一行,~~~~(>_<)~~~~ 我以为遍历当前行WA了好多次
                    f = 1;
                    break;
                }
            }
            if(f) break;
            else
                sum1 ++;
            if(b == 0 || e == 2 * n - 1 || xx == n - 1)  // 这里我判断了一下是否越界,但好像画蛇添足了,因为不加也能过。。。。
                break;
            b --; e ++; xx ++;
        }
    }
    else{
        while(1){
            for(q = b - 1; q <= e + 1 ; q ++){
                if(flag[xx - 1][q] != 1){   //这里一样
                    f = 1;
                    break;
                }
            }
            if(f) break;
            else
                sum1 ++;
            if(b == 0 || e == 2 * n - 1 || xx == 0)
                break;
            b --; e ++; xx --;
        }
    }
    return sum1;
}
int main(){
    int i, j, Max, sum, tt = 0, qq, s ,summ;
    while(~scanf("%d", &n), n){
        getchar();
        for(i = 0; i < n; i ++){
            gets(str[i]);
        }
        memset(flag, 0, sizeof(flag));
        for(i = 0; i < n; i ++){
            for(j = i; j < strlen(str[i]); j ++){
                if(str[i][j] == '-')
                    flag[i][j] = 1;
                else if(str[i][j] == '#')
                    flag[i][j] = 2;
            }
        }
        for(i = 0, Max = 0; i < n; i ++){
            for(j = i; j < strlen(str[i]); j ++){
                if(flag[i][j] == 1){
                    sum = dfs(i, j);
                    if(sum > Max)   Max = sum;
                }
            }
        }
        printf("Triangle #%d\n", ++ tt);
        printf("The largest triangle area is %d.\n\n", Max*Max);
    }
    return 0;
}
/*
3
##-#-
 ---
  #
3
-----
 -#-
  -
5
###-####-
 #---###
  --#--
   --#
    #
5
#-##----#
 -----#-
  ---#-
   -#-
    -
4
#-#-#--
 #---#
  ##-
   -
28
#--####-##-#---#-##--#-#-#-##-####-----#-#-#--####---##
 ####-##--#####--##--#---####-#--#--#-##---##--##-#-##
  #####---#-######--##--######-###-##--####-#-####---
   ---##--#---##-#--##-###-----#-##--#-#-####-#----#
    --#-#---##-----##--#--####-##-#----#-##-##-#-##
     #---####-#-----#-#--##-#---#-#---#--##-##--##
      -#-####----#-#-#-##---#-#---##--##-###--#--
       ###-###----##-###------#--#--##--#-#----#
        #-#-##-#--#-----#-#--#-###----##---##--
         --#----#-#-#-#-#--##-###---#--##-###-
          #-##----#--#--##---#--######-#----#
           #-#-#####---##---#--#-#--#-#--##-
            -#---#-#--##-###--##-#--##-####
             --#-##-##-#----##-#-#####-##-
              -#--##--#--#---#------##--#
               ##--##-##---#-#--#-#--###
                -----###--#----#-#-##-#
                 ###------###---##--#-
                  ----------########-
                   ###-##-##-#-#-##-
                    ----#--#######-
                     #-##-#-#--###
                      #-#-#---#--
                       #---###--
                        ---##-#
                         --###
                          -#-
                           -
*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值