题目:给出一些可以走的点,这些点是一段一段的形式给出的,比如第5行的第1个格子到第4个格子是可以走的点。第二比较特别的是数据特别大,10^9的规模!果断是数组以及链表很难解决的东西呀!尤其是标记这部分!所以,map是必备的。用map不仅去标记是否允许走,还可以表示是否走过,并且存储可行点到起点的最短距离。
代码:
#include <cstdio>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define X first
#define Y second
const int N = 100010;
int sx, sy, ex, ey;
int n;
int r[N], a[N], b[N];
pair <int, int> p;
map <pair<int, int>, int> mp;
int ar1[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int ar2[] = { 1, -1, 1, -1, 0, 0, -1, 1 };
int main()
{
while ( scanf("%d%d%d%d", &sx, &sy, &ex, &ey) != EOF ) {
mp.clear();
p.first = sx, p.second = sy;
scanf("%d", &n);
for ( int i = 0; i < n; ++i ) {
scanf("%d%d%d", &r[i], &a[i], &b[i]);
for ( int j = a[i]; j <= b[i]; ++j ) {
p.first = r[i], p.second = j;
mp[p] = -1;
}
}
queue < pair<int, int> > q;
p.X = sx, p.Y = sy;
mp[p] = 0;
q.push(p);
while ( !q.empty() ) {
p = q.front(); q.pop();
for( int i = 0; i < 8; ++i ) {
int x = p.X + ar1[i], y = p.Y + ar2[i];
pair <int, int> tmp;
tmp.X = x, tmp.Y = y;
if ( mp.count(tmp) && mp[tmp] == -1 ) {
mp[tmp] = mp[p]+1;
q.push(tmp);
}
}
}
p.X = ex, p.Y = ey;
printf("%d\n", mp[p]);
}
}