牛客网暑期ACM多校训练营(第八场) E.Touring cities (找规律)

链接:https://www.nowcoder.com/acm/contest/146/E
来源:牛客网

Niuniu wants to tour the cities in Moe country. Moe country has a total of n*m cities. The positions of the cities form a grid with n rows and m columns. We represent the city in row x and column y as (x,y). (1 ≤ x ≤ n,1 ≤ y ≤ m) There are bidirectional railways between adjacent cities. (x1,y1) and (x2,y2) are called adjacent if and only if |x1-x2|+|y1-y2|=1. There are also K bidirectional air lines between K pairs of cities. It takes Niuniu exactly one day to travel by a single line of railway or airplane. Niuniu starts and ends his tour in (1,1). What is the minimal time Niuniu has to travel between cities so that he can visit every city at least once?
Note that the air line may start and end in the same city.
输入描述:
The first line contains oneinteger T(T≤20), which means the number of test cases.
Each test case has the format as described below.
n m K
ax1 ay1 bx1 by1
ax2 ay2 bx2 by2

axK ayK bxK byK
(0 ≤ K ≤ 10. 2 ≤ n,m ≤ 100, 1 ≤ n*m ≤ 100)
There is one bidirectional air line between (axi,ayi) and (bxi,byi). (1 ≤ axi,bxi ≤ n , 1 ≤ ayi,byi ≤ m)

输出描述:
For each test case,print one number in a single line, which is the minimal number of days Niuniu has to travel between cities so that he can visit every city at least once.
示例1
输入

复制
3
2 2 1
1 1 2 2
3 3 1
1 1 3 3
3 3 0
输出

复制
4
9
10

题意

给你一个 n x m 的矩阵区域,要求从 ( 1 , 1 ) 出发经过所有的格子后回到 ( 1 , 1 ) 最少要多少步(每一步只能选择当前点的上向左右点),但是在 n x m 的区域中有一些双向通道可以从 ( x 1 , y 1 ) 经过一步直接到达 ( x 2 , y 2 ) ,再给你k个这样的通道,求这样的情况下的最小步数

思路

我们先考虑一下如果没有通道的话 n x m 的组合所需要的步数是多少步,尝试画画图考虑4x4和4x5的情况
这里写图片描述
这里写图片描述
我们发现当n和m只要有一个是偶数边的时候我们可以选择为偶数的那一边进行弯折的操作这样可以保证最后经过所有点后可以直接回到 ( 1 , 1 ) 这个时候答案就是 n m 并且和有没有通道没有关系
那么我们再来考虑一下 n m 都是奇数的情况考虑一下5x5的情况
这里写图片描述
只有我们的所经过的最后一个点为 ( 2 , 2 ) 的时候才是会是最优,他抵达不了 ( 1 , 2 ) ( 2 , 1 )
这个时候的答案就是 m n + 1 ,那么再考虑的就是n和m都为奇数时通道在哪可以优化步数,把 n m + 1 优化成 n m ,我们先只用考虑只有一个通道而且通道的一端就是 ( 1 , 1 ) ,那么对于5x5的图来说那些点可以优化成n*m呢,通过画图会发现是如图上的这些点可以
这里写图片描述
这些点都有什么特点呢,这些点的特点就是他们的坐标的奇偶性相同,再尝试一下这些点里面任选两个点作为通道到是否能实现 n m
这里写图片描述
发现在这些点里面任取两个点就可以实现 n m ,那么我们直接认为当n和m都为奇数的时候只要出现通道的两端的点的坐标的奇偶性一致是就能实现 n m ,那么实际上的原理是什么呢,如果有这样的通道出现,实际上改变了的就是折线( | 这样类型的折线)个数的奇偶性,折线个数为奇数的时候是能完成 n m ,而折线个数为偶数的时候就是 n m + 1
这里写图片描述
所以最后的代码就很简单的是

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, m, k;
    int t;
    cin >> t;
    while (t--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        int flag=0;
        for(int i=0; i<k; i++)
        {
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1==x2&&y1==y2)
                continue;
            if(x1%2==1&&y1%2==1&&x2%2==1&&y2%2==1) flag=1;
            if(x1%2==0&&y1%2==0&&x2%2==1&&y2%2==1) flag=1;
            if(x1%2==0&&y1%2==0&&x2%2==0&&y2%2==0) flag=1;
            if(x1%2==1&&y1%2==1&&x2%2==0&&y2%2==0) flag=1;
        }
        if(n%2==0||m%2==0||flag==1)
            printf("%d\n",n*m);
        else
            printf("%d\n",n*m+1);
    }
}

猜你喜欢

转载自blog.csdn.net/ftx456789/article/details/81605986