【链接】 我是链接,点我呀:)

【题意】

让你找到(x0,y0)到(x1,y1)的一条最短路
走过的点必须在所给的n个横向路径上

【题解】

因为n条横向路径上的点最多不会超过10的5次方个,所以我们可以把这10的5次方个点全都
和数字1~10^5一一对应。
然后对于这每一个点,分别于相邻的8个点连边。
然后在无向图上做一个广搜就能找到起点到终点的最短路啦

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = (int)1e5;
static class Task{ int x0,y0,x1,y1,n;
int dx[] = {0,0,1,-1,-1,-1,1,1};
int dy[] = {1,-1,0,0,-1,1,-1,1};
HashMap<Long,Integer> dic = new HashMap<>();
int dis[] = new int[N+10];
ArrayList<Integer> g[] = new ArrayList[N+10];
Queue<Integer> q = new LinkedList<Integer>(); int cnt = 0; void add(long temp) {
if (!dic.containsKey(temp)) {
dic.put(temp, ++cnt);
}
} long changetohash(int x,int y) {
long temp = x;
temp = temp << (31);
temp = temp + y;
return temp;
} public void solve(InputReader in,PrintWriter out) {
for (int i = 1;i <= N;i++) g[i] = new ArrayList<>();
x0 = in.nextInt();y0 = in.nextInt();x1 = in.nextInt();y1 = in.nextInt();
n = in.nextInt();
for (int i = 1;i <= n;i++) {
int row,cl,cr;
row = in.nextInt();
cl = in.nextInt();cr = in.nextInt();
for (int j = cl;j <= cr;j++) {
long temp = changetohash(row,j);
add(temp);
}
}
n = cnt;
Iterator it = dic.keySet().iterator();
while (it.hasNext()) {
long value = (long)it.next();
long x = value>>>31;
long y = 1<<31;y--;
y = value&y;
int X = dic.get(value);
for (int i = 0;i < 8;i++) {
int tx = (int)x + dx[i];
int ty = (int)y + dy[i]; if (dic.containsKey(changetohash(tx, ty))) {
int Y = dic.get(changetohash(tx, ty));
g[X].add(Y);
}
}
}
dis[dic.get(changetohash(x0, y0))] = 1;
q.add(dic.get(changetohash(x0, y0)));
while (!q.isEmpty()) {
int x = q.poll();
for (int i = 0;i < (int)g[x].size();i++) {
int y = g[x].get(i);
if (dis[y]==0) {
dis[y] = dis[x]+1;
q.add(y);
}
}
}
out.println(dis[dic.get(changetohash(x1, y1))]-1);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 242C】King's Path的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 534B】Covered Path

    [题目链接]:http://codeforces.com/contest/534/problem/B [题意] 你在t秒内可以将车的速度任意增加减少绝对值不超过d; 然后要求在一开始车速为v1,t秒之 ...

  3. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【25.64%】【codeforces 570E】Pig and Palindromes

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 758A】Holiday Of Equality

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【codeforces 752F】Santa Clauses and a Soccer Championship

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】

    思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...

  2. 在Ubuntu中设置DNS域名服务器

    在Ubuntu中设置DNS域名服务器主要有四种方法: 一.设置全局静态DNS $ sudo vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的),插入: n ...

  3. nginx 多进程 + io多路复用 实现高并发

    一.nginx 高并发原理 简单介绍:nginx 采用的是多进程(单线程) + io多路复用(epoll)模型 实现高并发 二.nginx 多进程 启动nginx 解析初始化配置文件后会 创建(for ...

  4. mvn 配置

    <!-- 阿里云仓库1 -->    <mirror>        <id>alimaven-1</id>        <name>al ...

  5. python自动化测试学习笔记-unittest参数化

    做接口测试的时候,当一个参数需要输入多个值的时候,就可以使用参数来实现: python中unittest单元测试,可以使用nose_parameterized来实现: 首先需要安装:pip  inst ...

  6. GIT学习之路最终日 标签管理+总结

    本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 6.1 创建标签 命令git tag (name)用于新建一个标签,默认为HEAD,也可以指定一个commit id: git tag -a ...

  7. Python安装第三方包(setup.py)

    在github上下载了records文件到本地. 解压文件 cmd切换到文件setup.py的目录下 先执行 python setup.py build 再执行python setup.py inst ...

  8. Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)

    Oracle 安装过程中,先决条件检查遇到如下错误: 正在检查网络配置要求...  检查完成.此次检查的总体结果为: 失败 <<<<  问题: 安装检测到系统的主 IP 地址是 ...

  9. 基于Web的Kafka管理器工具之Kafka-manager的编译部署详细安装 (支持kafka0.8、0.9和0.10以后版本)(图文详解)(默认端口或任意自定义端口)

    不多说,直接上干货! 至于为什么,要写这篇博客以及安装Kafka-manager? 问题详情 无奈于,在kafka里没有一个较好自带的web ui.启动后无法观看,并且不友好.所以,需安装一个第三方的 ...

  10. Canvas入门笔记-实现极简画笔

    今天学习了Html5 Canvas入门,已经有大神写得很详细了http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html#8 在学习过后 ...