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.

示例1

输入

复制

3
2 2 1
1 1 2 2
3 3 1
1 1 3 3
3 3 0

输出

复制

4
9
10

 

说的染色大致是这种情况

 

#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
bool is_prime(int x){if(x<2)return false;int m=sqrt(x+0.5);rep(i,2,m+1)if(x%i==0)return false;return true;}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
            int n,m,k;
            scanf("%d %d %d",&n,&m,&k);
            int w1=n&1,w2=m&1;
            //printf("w1:%d w2:%d n:%d m:%d k:%d\n",w1,w2,n,m,k);
            int r=0;
            rep(i,0,k){
                int x1,y1,x2,y2;
                scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                if(x1==x2&&y1==y2)continue;
                int b1=(((x1&1)&&(y1&1))||(!(x1&1)&&!(y1&1)));
                int b2=(((x2&1)&&(y2&1))||(!(x2&1)&&!(y2&1)));
                if(b1&b2)r=1;
            }
            if(w1&&w2&&!r)  printf("%d\n",n*m+1);
            else    printf("%d\n",n*m);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81607161