zoukankan      html  css  js  c++  java
  • 2019焦作F 以及图论题时限内存经验

     http://codeforces.com/gym/102028/problem/F

    F. Honeycomb
    time limit per test
    4.0 s
    memory limit per test
    1024 MB
    input
    standard input
    output
    standard output

    A honeycomb is a mass wax cells built by honey bees, which can be described as a regular tiling of the Euclidean plane, in which three hexagons meet at each internal vertex. The internal angle of a hexagon is 120120 degrees, so three hexagons at a point make a full 360360degrees. The following figure shows a complete honeycomb with 33 rows and 44 columns.

    Here we guarantee that the first cell in the second column always locates in the bottom right side of the first cell in the first column, as shown above. A general honeycomb may, on the basis of a complete honeycomb, lose some walls between adjacent cells, but the honeycomb is still in a closed form. A possible case looks like the figure below.

    Hamilton is a brave bee living in a general honeycomb. Now he wants to move from a starting point to a specified destination. The image below gives a feasible path in a 3×43×4 honeycomb from the 11-st cell in the 22-nd column to the 11-st cell in the 44-th column.

    Please help him find the minimum number of cells that a feasible path has to pass through (including the starting point and the destination) from the specified starting point to the destination.

    Input

    The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 104104.

    For each test case, the first line contains two integers rr and cc indicating the number of rows and the number of columns of the honeycomb, where 2r,c1032≤r,c≤103.

    The following (4r+3)(4r+3) lines describe the whole given honeycomb, where each line contains at most (6c+3)(6c+3) characters. Odd lines contain grid vertices represented as plus signs ("+") and zero or more horizontal edges, while even lines contain two or more diagonal edges. Specifically, a cell is described as 66 vertices and at most 66 edges. Its upper boundary or lower boundary is represented as three consecutive minus signs ("-"). Each one of its diagonal edges, if exists, is a single forward slash ("/") or a single backslash ("") character. All edge characters will be placed exactly between the corresponding vertices. At the center of the starting cell (resp. the destination), a capital "S" (resp. a capital "T") as a special character is used to indicate the special cell. All other characters will be space characters. Note that if any input line could contain trailing whitespace, that whitespace will be omitted.

    We guarantee that all outermost wall exist so that the given honeycomb is closed, and exactly one "S" and one "T" appear in the given honeycomb. Besides, the sum of rcr⋅c in all test cases is up to 2×1062×106.

    Output

    For each test case, output a line containing the minimum number of cells that Hamilton has to visit moving from the starting cell ("S") to the destination ("T"), including the starting cell and the destination. If no feasible path exists, output -1 instead.

    Example
    input
    Copy
    1
    3 4
      +---+       +---+
     /          /     
    +       +---+       +---+
                     /     
      +   +   S   +---+   T   +
     /          /           /
    +       +---+       +   +
                     /     
      +---+       +---+       +
     /                       /
    +       +---+       +   +
                      /     
      +---+       +---+       +
                /          /
            +---+       +---+
    
    output
    Copy
    7

    题意就是走六边形迷宫。

    其实很简单一个题目,只是题面和样例看起来很吓人...bfs随便搞搞就有思路了,但是这个题目难度不在这里。而是大矩阵的图导致的各种时间和空间开销上的问题。

    1.从时间上看,getchar()一个一个取是最快的(374ms),其次是gets()(607ms),最慢的是cin.getline(),第三个在本题直接导致tle。(下图时间从小到大为方法从左到右)

    2.从空间上看,对于大的矩阵图,vis、mp等数组一定要开到全局,不然会爆栈,如果调试爆栈,把数组开到全局试试。

    3.memset初始化并没有两层for快,本题memset会tle。

  • 相关阅读:
    iOS Simulator功能介绍关于Xamarin IOS开发
    Unity中制作游戏的快照游戏支持玩家拍快照
    手机数据抓包入门教程
    Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数
    Hierarchy视图里的Transform和Camera组件
    用JAVA编写MP3解码器——GUI(FFT)(转)
    功率W与dBm的对照表及关系(转)
    单鞭天线的长度计算方法(转)
    STM32F10X SPI操作flash MX25L64读写数据(转)
    利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
  • 原文地址:https://www.cnblogs.com/youchandaisuki/p/10891364.html
Copyright © 2011-2022 走看看