zoukankan      html  css  js  c++  java
  • 【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());
            }
        }
    }
    
  • 相关阅读:
    <c:if></c:if>用法-转载
    Windows下配置Apache服务器
    ScrureCRT访问CentOS时出现乱码的解决办法
    Windows平台下Git服务器搭建
    Group_Concat函数示例
    Mysql Federated Server 示例
    MySQL几个特别语法示例
    MySQL事件调度器
    Disruptor Java版和.NET版的区别
    委托的三种实现方式
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10533587.html
Copyright © 2011-2022 走看看