牛客网暑期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.

感觉被其他队伍带了节奏。。。明明这个比较简单,但是b的过题数多好多。

题意:有一个m*n的矩阵,每个位置代表一个城市,然后还有k个航线,连接着(x1,y1)和(x2,y2)这两个城市,坐飞机花费的时间为一个单位,每个城市也可以去到相邻的城市(上下左右),花费的时间也为一个单位,问从(1,1)开始旅行完所有的城市再回到(1,1)要多久。

思路:若m,n中有一个为偶数,则不用花费其他的时间,即刚好从(1,1)出发,经过所有的城市一次,再回到(1,1),也就是用了m*n个单位时间。若m,n两个都为奇数,则可从(1,1)出发进过所有的城市一次再回到(2,2,)点,即最多用了m*n+1个单位时间,若其中有 (x1+y1)%2 && (x2+y2)%2 的航线,则只需要用m*n个单位时间。(为什么可以自己画画。。。)

代码:

#include<iostream>
#include<cstdio>
typedef long long ll;
using namespace std;
 
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int k,n,m,cnt=0;
        scanf("%d %d %d",&n,&m,&k);
        for(int i=0;i<k;i++)
        {
            int t1,t2,t3,t4;
            scanf("%d %d %d %d",&t1,&t2,&t3,&t4);
            if((t1-t2)%2==0&&(t3-t4)%2==0&&(t1!=t3||t2!=t4))
                cnt++;
        }
        if(cnt||n%2==0||m%2==0)
            cout<<n*m<<endl;
        else
            cout<<n*m+1<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值