【Codeforces 242C】King's Path
【链接】 我是链接,点我呀:)
【题意】
让你找到(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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 534B】Covered Path
[题目链接]:http://codeforces.com/contest/534/problem/B [题意] 你在t秒内可以将车的速度任意增加减少绝对值不超过d; 然后要求在一开始车速为v1,t秒之 ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 758A】Holiday Of Equality
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- PCB Genesis或Incam 右键导入TGZ 实现方法
使用Genesis导入TGZ方式很多 的,比如有:写个脚本框选TGZ的的方式实现TGZ导入,将TGZ拖入脚本界面实现TGZ导入, 给Engineering Toolkit窗口句柄注册拖拽事件实现TGZ ...
- Windows 和 Linux 上Redis的安装守护进程配置
# Windows 和 Linux 上Redis的安装守护进程配置 Redis 简介 Redis是目前最常用的非关系型数据库(NOSql)之一,常以Key-Value的形式存储.Redis读写速度 ...
- 修复mysql的表
数据损坏原因 MySQL表损坏一般是数据损坏,引起损坏的原因可能是由于磁盘损坏.系统崩溃或者MySQL服务器被崩溃等外部原因.例如有人使用kill -9终止进程,导致MySQL进程未能正常关闭,那么就 ...
- json和Jsonp 使用总结(1)
1.Json的使用 $.getJSON("subPreview", { jsonDatas: JSON.stringify(jsonData) }, function(data) ...
- linux学习之路4 系统目录架构
linux树状文件系统结构 bin(binary) 保存可执行文件 也就是保存所有命令 boot 引导目录 保存所有跟系统有关的引导程序 其中Vmlinux文件最为重要,是系统内核 dev 保存所有的 ...
- ACM_Uppercase(水题)
Uppercase Time Limit: 2000/1000ms (Java/Others) Problem Description: 给出一句中间无特殊符号的英语句子,要求将各单词的首字母改大写. ...
- MyElipse如何添加Emmet插件
把这个jar文件放到myeclipse2014安装目录下dropins文件夹中,然后重启myeclipse即可. 可到window-->perferences里查看,如果成功则会看到emmet选 ...
- Java编程思想读书笔记_第6章(访问权限)
四种访问权限: public private 包访问权限 protected 如果没有明确指定package,则属于默认包 package access.dessert; public class C ...
- UVM基础之---------Reporting Classes
Reporting 类提供了一组工具用于格式化报告输出 report机制大概包括四个主要的类uvm_report_object,uvm_report_handler, uvm_report_serve ...
- 初始MongoDB------将MongoDB创建为Windows服务
上一遍我写的是关于基本的MongoDB的安装,可能不是很详细,也写得很不好,不过这次我们会详细的说说,如果将MongoDB部署在你的Windows电脑上. 1.配置环境变量 如果每次都要在CMD进入M ...