codeforces #687 div2 A.Prison Break(签到题)

A. Prison Break
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a prison that can be represented as a rectangular matrix with n rows and m columns. Therefore, there are n⋅m prison cells. There are also n⋅m prisoners, one in each prison cell. Let’s denote the cell in the i-th row and the j-th column as (i,j).

There’s a secret tunnel in the cell (r,c), that the prisoners will use to escape! However, to avoid the risk of getting caught, they will escape at night.

Before the night, every prisoner is in his own cell. When night comes, they can start moving to adjacent cells. Formally, in one second, a prisoner located in cell (i,j) can move to cells (i−1,j) , (i+1,j) , (i,j−1) , or (i,j+1), as long as the target cell is inside the prison. They can also choose to stay in cell (i,j).

The prisoners want to know the minimum number of seconds needed so that every prisoner can arrive to cell (r,c) if they move optimally. Note that there can be any number of prisoners in the same cell at the same time.

Input
The first line contains an integer t (1≤t≤104), the number of test cases.

Each of the next t lines contains four space-separated integers n, m, r, c (1≤r≤n≤109, 1≤c≤m≤109).

Output
Print t lines, the answers for each test case.

Example
inputCopy
3
10 10 1 1
3 5 2 4
10 2 5 1
outputCopy
18
4
6
题解:对于本题目来说,本题要求最短时间---------最短时间只可能出现监狱的四个角,只要求出距离出口最长的人的出逃时间即为最短时间,有两种思路:①把n*m的监狱分别以行和列去求最少时间,求出列的最大值和行的最大值,再求和,此时即为监狱的犯人逃出的最短时间②以n/2和m/2为分界线,分象限去求解即可

#include<bits/stdc++.h>
using namespace std;
int main()//第一种方法的代码
{
    
    
	std::ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
    
    
		long long n,m,c,r;
		cin>>n>>m>>c>>r;
		long long a=max(r-1,m-r);
		long long b=max(c-1,n-c);
		printf("%lld\n",a+b);
	}
//	system("pause");
	return 0;
}
//第二种的讨论比较麻烦,可以自行探索
//目前第二种思路不想写

猜你喜欢

转载自blog.csdn.net/weixin_46006714/article/details/110334882