牛客网暑期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...
摘要由CSDN通过智能技术生成

链接: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.

输入

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的网格,并且有K条飞行通道,问你遍历全图需要的步数。

思路:本以为是曼哈顿回路,然后画了画图发现,如果有一条边的长度是偶数那么可以

1/72
63
54

这样旋转,Emmmm,大一点

1/1923
1854
1767
1698
151011
141312

这样走,总长度为N*M.

然后都为奇数这样走

1/172/163
14154
13125
10116
987

总长度为N*M+1

特殊情况,题目中提到的桥梁

假如有一对桥梁,满足位置坐标之和是偶数就可以这么走

1/161514
2降落1213
34起飞11
6510
789

总长度还是M*N

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>

using namespace std;

#define inf 0x3f3f3f3f

const int maxn=1e9+7;

int main()
{
    int t;
    int n,m,k;
    int a,b,c,d;
    int ans;
    scanf("%d",&t);
    while (t--)
    {
        ans=1;
        scanf("%d %d %d",&n,&m,&k);
        for (int i = 0; i < k; ++i)
        {
            scanf("%d %d %d %d",&a,&b,&c,&d);
            if((a-b)%2==0&&(c-d)%2==0&&(a!=c||b!=d))
                ans=0;
        }
        if(n*m%2==0) ans=0;
        printf("%d\n",n*m+ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值