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

【题意】

让你找到(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. mybatis中各种数据的映射类型

    Mybatis对应的java和数据库的数据类型,最后有图片 Mybatis                                  java                          ...

  2. Spring MVC的学习笔记

    基于注解形式配置Spring MVC 一.注册并初始化DispatcherServlet,由Servlet容器自动检测并启动 注解形式 public class MyWebApplicationIni ...

  3. 在Chrome与火狐中,输入框input类型为number时,如何去除掉的自带的上下默认箭头

    如何移除input='number'时浏览器自带的上下箭头: CSS样式: /* 去除input[type=number]浏览器默认的icon显示 */ input::-webkit-outer-sp ...

  4. 题解报告:hdu 5695 Gym Class(拓扑排序)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=5695 Problem Description 众所周知,度度熊喜欢各类体育活动.今天,它终于当上了梦寐以求的体育课老 ...

  5. rman 问题

    1. RMAN Repeatedly Fail To Backup Archivelogs with RMAN-20242 Cause: There is a mis-match between th ...

  6. VB.NET 小程序 4

    Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...

  7. Java编程思想读书笔记_第6章

    如何创建一个包: 比如创建一个包名为com.huawei 则在一个目录下如(d:\java)创建目录com/huawei 然后在huawei目录下创建一个文件Assist.java package c ...

  8. 利用反射重写toString()方法

    为了方便输出对象,Object类提供了toString()方法.但是该方法的默认值是由类名和哈希码组成的,实用性并不强.通常需要重写该方法以提供更多的信息.本实例使用反射输出类的包.类的名字.类的公共 ...

  9. apache hadoop 伪分布式安装

    1. 准备工作 1.1. 软件准备 1.安装VMWare 2.在VMWare上安装CentOS6.5 3.安装XShell5,用来远程登录系统 4.通过rpm -qa | grep ssh 检查cen ...

  10. Probabilistic locking in SQLite

    In SQLite, a reader/writer lock mechanism is required to control the multi-process concurrent access ...