zoukankan      html  css  js  c++  java
  • uva1214 Manhattan Wiring 插头DP

    There is a rectangular area containing n × m cells. Two cells are marked with “2”, and another two with “3”. Some cells are occupied by obstacles. You should connect the two “2”s and also the two “3”s with non-intersecting lines. Lines can run only vertically or horizontally connecting centers of cells without obstacles.

    Lines cannot run on a cell with an obstacle. Only one line can run on a cell at most once. Hence, a line cannot intersect with the other line, nor with itself. Under these constraints, the total length of the two lines should be minimized. The length of a line is defined as the number of cell borders it passes. In particular, a line connecting cells sharing their border has length 1.

    Fig. 1(a) shows an example setting. Fig. 1(b) shows two lines satisfying the constraints above with minimum total length 18.

    Figure 1: An example of setting and its solution

    Input

    The input consists of multiple datasets, each in the following format.

    n m
    row1
    rown

    n is the number of rows which satisfies 2 ≤ n ≤ 9. m is the number of columns which satisfies 2 ≤ m ≤ 9. Each rowi is a sequence of m digits separated by a space. The digits mean the following.

    0: Empty

    1: Occupied by an obstacle

    2: Marked with “2”

    3: Marked with “3”

    The end of the input is indicated with a line containing two zeros separated by a space.

    Output

    For each dataset, one line containing the minimum total length of the two lines should be output. If there is no pair of lines satisfying the requirement, answer “0” instead. No other characters should be contained in the output.

    题解:https://wenku.baidu.com/view/e5314c16bcd126fff7050bf7.html?from=search

    Sample Input
    5 5
    0 0 0 0 0
    0 0 0 3 0
    2 0 2 0 0
    1 0 1 1 1
    0 0 0 0 3
    2 3
    2 2 0
    0 3 3
    6 5
    2 0 0 0 0
    0 3 0 0 0
    0 0 0 0 0
    1 1 1 0 0
    0 0 0 0 0
    0 0 2 3 0
    5 9
    0 0 0 0 0 0 0 0 0
    0 0 0 0 3 0 0 0 0
    0 2 0 0 0 0 0 2 0
    0 0 0 0 3 0 0 0 0
    0 0 0 0 0 0 0 0 0
    9 9
    3 0 0 0 0 0 0 0 2
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0 3
    9 9
    0 0 0 1 0 0 0 0 0
    0 2 0 1 0 0 0 0 3
    0 0 0 1 0 0 0 0 2
    0 0 0 1 0 0 0 0 3
    0 0 0 1 1 1 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    9 9
    0 0 0 0 0 0 0 0 0
    0 3 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 2 3 2
    0 0
    Sample Output
    18
    2
    17
    12
    0
    52
    43
  • 相关阅读:
    mysql数据库的备份和恢复
    安装docker17.06.0版本报错和解决方法
    mysql随笔系列-1
    22_redis哨兵主备切换的数据丢失问题:异步复制、集群脑裂
    21_redis哨兵架构的相关基础知识的讲解
    19_对项目的主从redis架构进行QPS压测以及水平扩容支撑更高QPS
    18_在项目中部署redis的读写分离架构(包含节点间认证口令)
    17_redis replication的完整流运行程和原理的再次深入剖析
    16_redis主从复制原理、断点续传、无磁盘化复制、过期key处理
    15_redis replication以及master持久化对主从架构的安全意义
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8487573.html
Copyright © 2011-2022 走看看