牛客2018多校训练-----Touring cities

本文探讨了ACM竞赛中的一道题目,涉及在带有特殊路径的网格中寻找最短路径,确保覆盖所有节点的问题。介绍了如何通过分析网格边长的奇偶性和特殊路径的影响来快速得出解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述
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
备注:
The air line may start and end in the same city.
 

  n*m的网格,从(1,1)回到(1,1),经过每个网格至少一次,每次只能向上下左右移动。有k条快捷路径,可直接从一个格子移到另一个格子。问最少移动几次。

思路:

 

黑白棋盘格遍历问题的变形,找边长为奇数偶数的规律。

也可以直接找规律

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,p;
        cin>>n>>m>>p;
        int x,y;
        int flag=1;
        while(p--)
        {
            int x1,x2,y1,y2;
            cin>>x1>>y1>>x2>>y2;
            if((x1!=x2||y1!=y2)&&(x1+y1)%2==0&&(x2+y2)%2==0) 
                flag=0;
        }
        if(n%2==0||m%2==0) 
            printf("%d\n",n*m);
        else 
            printf("%d\n",n*m+flag);
    }
    return 0;
}
————————————————
版权声明:本文为优快云博主「对你说的对」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/jinghui_7/article/details/81624592

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值