【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 ...
随机推荐
- nodejs常用命令
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install m ...
- easyui-filebox 文件上传
参考文章:http://blog.csdn.net/fsdad/article/details/73200618 easyui论坛:http://www.jeasyui.com/forum/index ...
- Wannafly挑战赛29A御坂美琴
传送门 这套题很有意思2333 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些数,用map记录一下,判断是否所有数都能被分出来就是了 //m ...
- JavaScript编程艺术-第7章代码汇总(2)
[7.4节] 重回“JavaScript美术馆”代码 ***亲测可用*** HTML: JS:
- ssh&远程桌面连接工具finalshell
无意间发现的一款工具,有兴趣的可以看看点我进入官网 百度云盘 链接:https://pan.baidu.com/s/1wMuGav64e2zV91QznBkvag 密码:zpyb软件特点直接搬运的官方 ...
- layer父页获取弹出层输入框里面的值
主要是因为修改功能,原来页面填写数据如图 改为 其中点击填写明细弹出框 填写完毕后点击确认返回,同事这里因为她是存的多表,所以点击确认就直接保存数据了,改的这个功能原本保存是整体保存,我就不想改原来的 ...
- R语言曲线拟合函数(绘图)
曲线拟合:(线性回归方法:lm) 1.x排序 2.求线性回归方程并赋予一个新变量 z=lm(y~x+I(x^2)+...) 3.plot(x,y) #做y对x的散点图 4.lines(x ...
- URL解析-URLComponents
let components = URLComponents(url: fakeUrl, resolvingAgainstBaseURL: false)! http://10.100.140.84/m ...
- 安卓app测试之Monkeyscript
MonkeyScript是一组可以被Monkey识别的命令集合 优点:MonkeyScript可以完成重复固定的操作 使用:adb shell monkey -f <scriptfile> ...
- 梦想CAD控件安卓控件事件
MxDrawActivity.commandEvent 命令调用事件. 参数 说明 int iCommand 命令ID,这个ID用户自已来取的,只要多个命令ID不重复就可以 代码实现如下: publi ...