[Nowcoder 2018ACM多校第八场E] Touring cities

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013578420/article/details/81701026

题目大意:
给一个上下左右四联通的n*m的网格图, 同时额外加k条边, 求从(1,1)出发遍历所有点至少一次再回到(1,1)的路径长度。 ( n m 100 , k 10 )

题目思路:
通过不断模拟构造方案, 可以发现n*m是偶数时, 答案是n*m, 且其他情况下最坏情况也只能是n*m+1。 考虑判断图中是否存在哈密尔回路, 考虑棋盘图的特性, 将图黑白染色, 设起点为黑点, 只用棋盘上的边一定是黑点走向白点, 而在n*m为奇数的情况下黑点数比白点数多1, 如果新加的边中连接的是两个不同的黑点, 则有解。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>

#define ll long long
#define fi first
#define se second

using namespace std;

int n, m, k;

int main()
{

    int T;
    scanf("%d", &T);
    while (T --){
        scanf("%d %d %d", &n, &m, &k);
        bool ok = n % 2 == 0 || m % 2 == 0;
        while (k --){
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if (x1 == x2 && y1 == y2) continue;
            ok |= (x1 + y1) % 2 == 0 && (x2 + y2) % 2 == 0;
        }

        printf("%d\n", ok ? n * m : n * m + 1);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013578420/article/details/81701026
今日推荐