C. King's Path
The black king is standing on a chess field consisting of 109 rows and 109 columns.
We will consider the rows of the field numbered with integers from 1 to 109 from top
to bottom. The columns are similarly numbered with integers from 1 to 109 from left
to right. We will denote a cell of the field that is located in the i-th row and j-th column
as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells
of the chess field are given as n segments. Each segment is described by three
integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number
bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square
(x0, y0) to square (x1, y1), provided that he only moves along the allowed cells.
In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in
one move. Two cells of a chess field are considered neighboring if they share at
least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1,
y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of
segments of allowed cells. Next n lines contain the descriptions of these segments.
The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi),
denoting that cells in columns from number ai to number bi inclusive in the ri-th row
are allowed. Note that the segments of the allowed cells can intersect and embed
arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed
that the king's initial and the final positions do not coincide. It is guaranteed that the total
length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get
from the initial position to the final one.
Examples
input
5 7 6 11 3 5 3 8 6 7 11 5 2 5output
4input
3 4 3 10 3 3 1 4 4 5 9 3 10 10output
6input
1 1 2 10 2 1 1 3 2 6 10output
-1
题意:
就是国际象棋,给你起点和终点,还有你可以走的路,问你是否能从起点
走到终点,如果能,就输出最短路,不能输出-1,国际象棋八个方向可以走。
思路:
数据范围有点大,可以考虑离散化,用map【N】存储的话
可以只离散化行,其实有更好的方法,map 的key值定义成坐标,
value标记量,这样就都不用离散化了。
之前离散化的代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<cstdio>
#include<algorithm>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
const int M=4e4+100;
const int mod=1e9+9;
struct Node {
int x,y,step;
Node(int xx,int yy,int s) {
x=xx;
y=yy;
step=s;
}
};
map<int,int>mc[N];
queue<Node>qc;
int xx[10]= {0,1,1,1,-1,-1,-1,0,0};
int yy[10]= {0,0,1,-1,0,-1,1,-1,1};
int tot,row[N];
int cal(int x){
return lower_bound(row+1,row+1+tot,x)-row;
}
bool judge(int x,int y,int n) {
if(mc[cal(x)][y]==0)return 0;
return 1;
}
int sol(int n,int bx,int by,int ex,int ey) {
if(mc[cal(bx)][by]==0||mc[cal(ex)][ey]==0)return -1;
while(qc.size())qc.pop();
qc.push(Node(bx,by,0));
mc[cal(bx)][by]==0;
while(qc.size()) {
Node tmp=qc.front();
qc.pop();
for(int i=1; i<=8; i++) {
int tmpx=tmp.x+xx[i];
int tmpy=tmp.y+yy[i];
int step=tmp.step+1;
if(tmpx==ex&&tmpy==ey) {
return step;
}
if(judge(tmpx,tmpy,n)) {
qc.push(Node(tmpx,tmpy,step));
mc[cal(tmpx)][tmpy]=0;
}
}
}
return -1;
}
struct query{
int ro,l,r;
}arr[N];
int main() {
#ifdef MYHOE
freopen("in.txt","r",stdin);
#endif
int bx,by,ex,ey;
int n,ans;
while(cin>>bx>>by>>ex>>ey) {
memset(row,0,sizeof(row));
cin>>n;
for(int i=0; i<=n; i++) {
mc[i].clear();
}
for(int i=1; i<=n; i++) {
cin>>arr[i].ro;
cin>>arr[i].l;
cin>>arr[i].r;
row[i]=arr[i].ro;
}
sort(row+1,row+1+n);
tot=unique(row+1,row+1+n)-row-1;
for(int i=1;i<=n;i++){
int id=lower_bound(row+1,row+1+tot,arr[i].ro)-row;
for(int j=arr[i].l;j<=arr[i].r;j++){
mc[id][j]=1;
}
}
cout<<sol(n,bx,by,ex,ey)<<endl;
}
return 0;
}
map优化后的代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<cstdio>
#include<algorithm>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
const int M=4e4+100;
const int mod=1e9+9;
struct Node {
int x,y,step;
Node(int xx,int yy,int s) {
x=xx;
y=yy;
step=s;
}
};
map<pair<int,int>,int>mc;
queue<Node>qc;
int xx[10]= {0,1,1,1,-1,-1,-1,0,0};
int yy[10]= {0,0,1,-1,0,-1,1,-1,1};
bool judge(int x,int y,int n) {
if(mc[make_pair(x,y)]==0)return 0;
return 1;
}
int sol(int n,int bx,int by,int ex,int ey) {
if(mc[make_pair(bx,by)]==0||mc[make_pair(ex,ey)]==0)return -1;
while(qc.size())qc.pop();
qc.push(Node(bx,by,0));
mc[make_pair(bx,by)]==0;
while(qc.size()) {
Node tmp=qc.front();
qc.pop();
for(int i=1; i<=8; i++) {
int tmpx=tmp.x+xx[i];
int tmpy=tmp.y+yy[i];
int step=tmp.step+1;
if(tmpx==ex&&tmpy==ey) {
return step;
}
if(judge(tmpx,tmpy,n)) {
qc.push(Node(tmpx,tmpy,step));
mc[make_pair(tmpx,tmpy)]=0;
}
}
}
return -1;
}
int main() {
#ifdef MYHOE
freopen("in.txt","r",stdin);
#endif
int bx,by,ex,ey;
int n,l,r,row,ans;
while(cin>>bx>>by>>ex>>ey) {
cin>>n;
mc.clear();
for(int i=1; i<=n; i++) {
cin>>row>>l>>r;
for(int j=l; j<=r; j++) {
mc[make_pair(row,j)]=1;
}
}
cout<<sol(n,bx,by,ex,ey)<<endl;
}
return 0;
}
THE END;