zoukankan      html  css  js  c++  java
  • HDU 5024

    题目大意:

    在2个图上显示为'.'的位置建两座房间,保证这两间房子中间只转一个90度的弯,可以斜着走,问能建成房子的最远的路程长度为多少

    暴力枚举

    因为有8个方向,但横竖走和斜着走是不会产生90度角的,所以分成两部分进行考虑,每次找到一个联通的点作为转角,朝四个方向不断扩展长度,直到不能扩展为止,把最长长度的两个方向上的长度相加得到那一个点作为转角的最值

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 #define N 102
     7 char mat[N][N];
     8 int maxn,n;
     9 int dir1[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    10 int dir2[4][2] = {{1,1},{-1,1},{1,-1},{-1,-1}};
    11 void solve1(int x,int y)
    12 {
    13     int tmp[4];
    14     for(int i=0;i<4;i++){
    15         int xx=x,yy=y;
    16         tmp[i]=0;
    17         while(true){
    18             xx+=dir1[i][0];
    19             yy+=dir1[i][1];
    20             if(mat[xx][yy]!='#'&&xx>=0&&xx<n&&yy>=0&&yy<n){
    21                 tmp[i]++;
    22             }
    23             else break;
    24         }
    25     }
    26     sort(tmp,tmp+4);
    27     maxn = max(maxn,tmp[2]+tmp[3]);
    28 }
    29 
    30 void solve2(int x,int y)
    31 {
    32     int tmp[4];
    33 
    34     for(int i=0;i<4;i++){
    35         int xx=x,yy=y;
    36         tmp[i]=0;
    37         while(true){
    38             xx+=dir2[i][0];
    39             yy+=dir2[i][1];
    40             if(mat[xx][yy]!='#'&&xx>=0&&xx<n&&yy>=0&&yy<n){
    41                 tmp[i]++;
    42             }
    43             else break;
    44         }
    45         //if(x==0&&y==1) cout<<i<<" "<<tmp[i]<<endl;
    46     }
    47     sort(tmp,tmp+4);
    48     maxn = max(maxn,tmp[2]+tmp[3]);
    49 }
    50 
    51 int main()
    52 {
    53     while(~scanf("%d",&n)){
    54         if(n==0)
    55             break;
    56 
    57         maxn = 0;
    58 
    59         for(int i=0;i<n;i++)
    60             for(int j=0;j<n;j++)
    61                 cin>>mat[i][j];
    62 
    63         for(int i=0;i<n;i++)
    64             for(int j=0;j<n;j++)
    65                 if(mat[i][j]!='#'){
    66                     solve1(i,j);
    67 
    68                     solve2(i,j);
    69 
    70                 }
    71 
    72         printf("%d
    ",maxn+1);
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    题解 UVA120 【煎饼 Stacks of Flapjacks】
    信息编码表示

    二叉树
    逻辑运算&位运算
    POJ2425 Ubiquitous Religions(并查集板题)
    CF1426E Rock, Paper, Scissors 题解
    POJ2478 Farey Sequence
    dubbo+zookeeper报错 KeeperErrorCode = Unimplemented for /dubbo
    代码无法提交到GitHub: Remote URL test failed: git@github.com: Permission denied (publickey)
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/3988453.html
Copyright © 2011-2022 走看看