147. Black-white king
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output
On the chessboard of size NxN leaves only three figures. They are black king, white king and black-white king. The black-white king is very unusual chess piece for us, because it is invisible. Black and white kings decided to conclude a treaty against black-white king (they don't see it, but know that it is somewhere near at chessboard). To realize there plans black and white must meet face to face, what means that they must occupy two neighboring cells (generally each cell has 8 neighbors). The black-white king wants to prevent them from meeting. To do this he must intercept one of the kings before they'll meet, that is to attack one of the kings (make a move to it's cell). If the opponent will make a move on the cell of black-white king, nothing will happen (nobody kill anybody). Your task is to find out have the black-white king chances to win or not. Consider that white and black kings choose the one of the shortest ways to meet. Remember, that they don't see the black-white king. The black-white king also has a strategy: he moves in such a way, that none of the parts of his way can be shortened (for example, he cannot move by zigzag).
In the case of positive answer (i.e. if the probability of black-white king to win is nonzero) find the minimal number of moves necessary to probable victory. Otherwise find the minimal total number of moves of black and white kings necessary to meet. Remember the order of moves: white king, black king, and black-white king. Any king can move to any of the 8 adjacent cells.
Input
First line of input data contains the natural number N (2<=N<=10^6). The second line contains two natural numbers P1, Q1 (0<P1,Q1<N+1) - coordinates of black king, third line contains P2, Q2 (0<P2,Q2<N+1) - coordinates of white king, forth line contains P3, Q3 (0<P3,Q3<N+1) - coordinates of black-white king. Positions of all kings are different.
Output
Write to the first line word "YES" if the answer id positive, and "NO" - otherwise. To the second line write a single number - the numerical answer to the task.
Sample test(s)
Input
5
1 1
5 3
2 3
Output
YES
1
这道题看起来很像水题,解起来很像水题,但是有两点 1 黑白王的最短路是指步数最短不是指单纯的路程最短 2 一开始就在一个格子上则yes,0
其中第一点很坑,即使经过队友开导我现在也抱着这是坑题和题意不明的心态
注意黑白王的运动状态可能是以初始点为中心,2*步数+1为正方形边长的空心正方形
这里有几组测试数据,直接找个ac程序对拍吧,比如我写在下面的
10
1 10
1 1
5 5
5
1 1
5 3
2 3
10
1 1
5 5
3 3
5
10 10
5 5
3 4
3
1 1
2 2
3 3
200
1 1
20 100
25 17
500
1 1
200 200
100 100
10
1 1
4 3
2 5
100
10 40
40 30
25 25
21
1 10
21 10
1 5
25
1 10
21 10
21 1
4
3 1
1 2
1 2
66
4 57
31 35
17 38
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int n,p1,q1,p2,q2,p3,q3;
bool ins(int x,int y1,int y2,int x3,int x4,int y3,int y4){
// printf("x %d y1 %d y2 %d x3 %d y3 %d x4 %d y4 %d\n",x,y1,y2,x3,y3,x4,y4);
if(x<=x4&&x>=x3&&((y1<=y3&&y2>=y3)||(y1<=y4&&y2>=y4)))return true;
if(x==x3||x==x4){
if(max(y1,y3)<=min(y2,y4))return true;
}
return false;
}
int pos(int x){
if(x<1)return 1;
if(x>n)return n;
return x;
}
int calc(){
int sumstep=abs(p1-p2);
int maxstep=abs(p1-p2)/2-1;
if(maxstep<=0)return -1;
int x1=p2==p1?0:(p2-p1)/abs(p2-p1);
int xx,ymax,ymin;
for(int i=1;i<=maxstep;i++){
// printf("%d:\n%d %d %d %d\n",i,pos(q1-i),pos(q1+i),pos(q2-sumstep+i),pos(q2+sumstep-i));
xx=p1+x1*i;
ymin=max(pos(q1-i),pos(q2-sumstep+i));
ymax=min(pos(q1+i),pos(q2+sumstep-i));
if(ins(xx,ymin,ymax,p3-i,p3+i,q3-i,q3+i))return i;
xx=p2-x1*i;
ymin=max(pos(q2-i),pos(q1-sumstep+i));
ymax=min(pos(q2+i),pos(q1+sumstep-i));
if(ins(xx,ymin,ymax,p3-i,p3+i,q3-i,q3+i))return i;
}
return -1;
}
int main(){
//freopen("data.txt","w",stdout);
scanf("%d%d%d%d%d%d%d",&n,&p1,&q1,&p2,&q2,&p3,&q3);
if((p1==p3&&q1==q3)||(p2==p3&&q2==q3)){puts("YES\n0");return 0;}
if(abs(p1-p2)<abs(q1-q2)){
swap(p1,q1);swap(p2,q2);swap(p3,q3);
}
int ans=calc();
if(ans==-1)printf("NO\n%d\n",abs(p1-p2)-1);
else {
printf("YES\n%d\n",ans);
}
return 0;
}