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