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

【题意】

让你找到(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. SQLServer 存储过程 SET NOCOUNT { ON | OFF } 的使用

    在JDBC中调用 sql server 的存储过程时“该语句没有返回结果集”异常: 解决方法:在存储过程首行加上   SET NOCOUNT ON   使返回的结果中不包含有关受 Transact-S ...

  2. Helios Service Release 2安装SVN

    Eclipse Helios Service Release 2安装SVN地址:subclipse http://subclipse.tigris.org/update_1.6.x 1.使用方式:he ...

  3. js中return的作用及用法

    这里面的return含有一些细节知识: 例如:onClick='return add_onclick()'与 onClick='add_onclick()'的区别 JAVASCRIPT在事件中调用函数 ...

  4. LoadRunner监控Linux配置教程

    LoadRunner监控Linux资源时弹出如下错误: Monitor name :UNIX Resources. Cannot initialize the monitoring on 192.16 ...

  5. $CF19A\ World\ Football\ Cup$

    炒鸡\(6\)批的模拟题. 注意的是输入 把握好空格 大小写. 根据题目的这句话来排序 积分榜是按照以下原则制作的:胜利一个队得3分,平分1分,失败0分. 首先,球队按积分顺序排在积分榜上,分数相等比 ...

  6. python orm / 表与model相互转换

    orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为了保证一致的 ...

  7. [Qt Creator 快速入门] 第2章 Qt程序编译和源码详解

    一.编写 Hello World Gui程序 Hello World程序就是让应用程序显示"Hello World"字符串.这是最简单的应用,但却包含了一个应用程序的基本要素,所以 ...

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  9. 全面学习ORACLE Scheduler特性(2)管理jobs

    1.2  管理Jobs 1.2.1  启用Jobs 前面创建JOB时,由于未显式的指定ENABLED参数,因此即使指定了START_DATE,不过默认情况下JOB不会自动执行.对于这种情况,DBMS_ ...

  10. Github 文件选择性上传

    用过Github的人都知道.gitignore文件的存在,但是实际用起来还是有一些需要注意的地方,尤其是对于新手来说,稍不注意就会出错.   一.Github选择性忽略特定文件的方式 1.全局设置 一 ...