链接: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/7 | 2 |
6 | 3 |
5 | 4 |
这样旋转,Emmmm,大一点
1/19 | 2 | 3 |
18 | 5 | 4 |
17 | 6 | 7 |
16 | 9 | 8 |
15 | 10 | 11 |
14 | 13 | 12 |
这样走,总长度为N*M.
然后都为奇数这样走
1/17 | 2/16 | 3 |
14 | 15 | 4 |
13 | 12 | 5 |
10 | 11 | 6 |
9 | 8 | 7 |
总长度为N*M+1
特殊情况,题目中提到的桥梁
假如有一对桥梁,满足位置坐标之和是偶数就可以这么走
1/16 | 15 | 14 |
2 | 降落12 | 13 |
3 | 4 | 起飞11 |
6 | 5 | 10 |
7 | 8 | 9 |
总长度还是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;
}