题目描述如下:
A. Shortest Path with Obstacle
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
There are three cells on an infinite 2-dimensional grid, labeled AA, BB, and FF. Find the length of the shortest path from AA to BB if:
- in one move you can go to any of the four adjacent cells sharing a side;
- visiting the cell FF is forbidden (it is an obstacle).
Input
The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input. Then tt test cases follow. Before each test case, there is an empty line.
Each test case contains three lines. The first one contains two integers xA,yAxA,yA (1≤xA,yA≤10001≤xA,yA≤1000) — coordinates of the start cell AA. The second one contains two integers xB,yBxB,yB (1≤xB,yB≤10001≤xB,yB≤1000) — coordinates of the finish cell BB. The third one contains two integers xF,yFxF,yF (1≤xF,yF≤10001≤xF,yF≤1000) — coordinates of the forbidden cell FF. All cells are distinct.
Coordinate xx corresponds to the column number and coordinate yy corresponds to the row number (see the pictures below).
Output
Output tt lines. The ii-th line should contain the answer for the ii-th test case: the length of the shortest path from the cell AA to the cell BB if the cell FF is not allowed to be visited.
Example
input
Copy
7 1 1 3 3 2 2 2 5 2 1 2 3 1000 42 1000 1 1000 1000 1 10 3 10 2 10 3 8 7 8 3 7 2 1 4 1 1 1 1 344 1 10 1 1
output
Copy
4 6 41 4 4 2 334
Note
An example of a possible shortest path for the first test case.
An example of a possible shortest path for the second test case.
ac代码:
#include<bits/stdc++.h>
using namespace std;
const char little_define_array=3;
#define intc(n); int n;cin>>n;
#define qfor(i,n) for(i=0;i<n;i++)
#define qw(tc) while(tc--)
#define pb push_back
#define popb push_back
bool if_equal(int x,int y){
if(x==y){
return 1;
}
else{
return 0;
}
}
bool abf_q(int x,int y,int z){
if((x>y&&y>z)||(x<y&&y<z)||(y>x&&x>z)||(y<x&&x<z)){
return 1;
}
else if(x!=y){
return 0;
}
}
void run(){
int a[little_define_array];
int b[little_define_array];
int f[little_define_array];
cin>>a[0]>>a[1];
cin>>b[0]>>b[1];
cin>>f[0]>>f[1];
if(if_equal(a[0],b[0])){
if(if_equal(a[0],f[0])){
if(!(abf_q(a[1],b[1],f[1]))){
cout<<abs(a[1]-b[1])+2<<endl;
return;
}
}
}
else if(if_equal(a[1],b[1])){
if(if_equal(a[1],f[1])){
if(!(abf_q(a[0],b[0],f[0]))){
cout<<abs(a[0]-b[0])+2<<endl;
return;
}
}
}
else if(if_equal(a[0],b[0])){
cout<<abs(a[1]-b[1])<<endl;
return;
}
else if(if_equal(a[1],b[1])){
cout<<abs(a[0]-b[0])<<endl;
return;
}
cout<<abs(a[0]-b[0])+abs(a[1]-b[1])<<endl;
}
int main(){
intc(tc);
qw(tc){
run();
}
exit(0);
return 0;
}
来源:
Problem - 1547A - Codeforceshttps://codeforces.com/problemset/problem/1547/A
我们下回再见,欢迎大家关注我!