zoukankan      html  css  js  c++  java
  • HDU 2612 (两边一起)

    /* Find a way

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. Input The input contains multiple test cases. Each test case include, first two integers n, m. (2<=n,m<=200). Next n lines, each line included m character. ‘Y’ express yifenfei initial position. ‘M’ express Merceki initial position. ‘#’ forbid road; ‘.’ Road. ‘@’ KCF Output For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet. Sample Input 4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...# Sample Output 66 88 66*/

    解题思路:

    1、最少步数-》广度优先搜索

    2、Y与M分别进行广度优先搜索 

    3、注意在Y与M 广度优先搜索时分别初始化vis、queue

    Accepted

    import java.util.*;
    
    public class Main{
        static class node{
            int x;
            int y;
            int len;
            public node(int x,int y,int len) {
                this.x = x;
                this.y = y;
                this.len = len;
            }
        }
        static int[][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
        static boolean[][] vis = new boolean[205][205];
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            while(scanner.hasNext()) {
                char[][] maze = new char[205][205];
                int[][] step = new int[205][205];
                int yx = 0,yy = 0,mx = 0,my = 0;
                
                int n = scanner.nextInt();
                int m = scanner.nextInt();
                for(int i=0;i<n;i++) {
                    String string  = scanner.next();
                    for(int j=0;j<m;j++) {
                        maze[i][j] = string.charAt(j);
                        if(maze[i][j]=='Y') {yx = i; yy = j;}
                        if(maze[i][j]=='M') {mx = i; my = j;}    
                    }
                }
                
                Queue<node> queue = new LinkedList<node>();
                vis = new boolean[205][205];
                vis[yx][yy] = true;
                queue.add(new node(yx, yy, 0));
                while(!queue.isEmpty()){
                    node temp = queue.poll();
                    for(int i=0;i<4;i++) {
                        int nx = temp.x + dir[i][0];
                        int ny = temp.y + dir[i][1];
                        if(nx<0||nx>=n||ny<0||ny>=m||vis[nx][ny]||maze[nx][ny]=='#') continue;
                        vis[nx][ny] = true;
                        if(maze[nx][ny]=='@') 
                            step[nx][ny] += (temp.len+1);
                        
                        queue.add(new node(nx, ny, (temp.len+1)));
                    }
                }
                
                queue = new LinkedList<node>();
                vis = new boolean[205][205];
                vis[mx][my] = true;
                queue.add(new node(mx, my, 0));
                while(!queue.isEmpty()){
                    node temp = queue.poll();
                    for(int i=0;i<4;i++) {
                        int nx = temp.x + dir[i][0];
                        int ny = temp.y + dir[i][1];
                        if(nx<0||nx>=n||ny<0||ny>=m||vis[nx][ny]||maze[nx][ny]=='#') continue;
                        vis[nx][ny] = true;
                        if(maze[nx][ny]=='@') 
                            step[nx][ny] += (temp.len+1);
                            
                        queue.add(new node(nx, ny, (temp.len+1)));
                    }
                }
                
                int min = 1000000000;
                for(int i=0;i<n;i++)
                    for(int j=0;j<m;j++)
                        if(step[i][j]!=0)
                            min = Math.min(min, step[i][j]);
                        
                System.out.println(min*11);    
            }
        }
    }

     Time Limit Exceeded

    import java.util.*;
    
    public class O{
        static class node{
            int x;
            int y;
            int len;
            public node(int x,int y,int len) {
                this.x = x;
                this.y = y;
                this.len = len;
            }
        }
        static int[][] dir = {{1,0},{0,1},{-1,0},{0,-1}};
        static char[][] maze = new char[205][205] ;
        static boolean[][] vis;
        static int[] num = new int[40000];
        static int[] step = new int[40000];
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            while(scanner.hasNext()) {
                int yx = 0,yy = 0,mx = 0,my = 0,index = 0;
                int n = scanner.nextInt();
                int m = scanner.nextInt();
                
                for(int i=0;i<n;i++) {
                    String string = scanner.next();
                    for(int j=0;j<m;j++) {
                        maze[i][j] = string.charAt(j);
                        if(maze[i][j]=='@') num[index++] = n*i+j;
                        if(maze[i][j]=='Y') {yx = i;yy = j;}
                        if(maze[i][j]=='M') {mx = i;my = j;}
                    }    
                }
                
                for(int i=0;i<index;i++) {
                
                    Queue<node> queue = new LinkedList<node>();
                    vis = new boolean[205][205];
                    queue.add(new node(yx, yy, 0));
                    vis[yx][yy] = true;
                    while(!queue.isEmpty()) {
                        node temp = queue.poll();
                        for(int j=0;j<4;j++) {
                            int nx = temp.x + dir[j][0];
                            int ny = temp.y + dir[j][1];
                            if(nx<0||nx>=n||ny<0||ny>=m||vis[nx][ny]||maze[nx][ny]=='#') continue;
                            vis[nx][ny] = true;
                            if(nx==num[i]/n&&ny==num[i]%n) {
                                step[i] = temp.len+1;
                                break;
                            }
                            queue.add(new node(nx, ny, (temp.len+1)));
                        }
                            
                    }
                    
                    queue = new LinkedList<node>();
                    vis = new boolean[205][205];
                    queue.add(new node(mx, my, 0));
                    vis[yx][yy] = true;
                    while(!queue.isEmpty()) {
                        node temp = queue.poll();
                        for(int j=0;j<4;j++) {
                            int nx = temp.x + dir[j][0];
                            int ny = temp.y + dir[j][1];
                            if(nx<0||nx>=n||ny<0||ny>=m||vis[nx][ny]||maze[nx][ny]=='#') continue;
                            vis[nx][ny] = true;
                            if(nx==num[i]/n&&ny==num[i]%n) {
                                step[i] += temp.len+1;
                                break;
                            }
                            queue.add(new node(nx, ny, (temp.len+1)));
                        }
                            
                    }
                    
                }
                Arrays.sort(step,0,index);
                System.out.println(step[0]*11);        
            }
            
            
        }
    }
  • 相关阅读:
    一、反射机制介绍_Class 对象获取
    六、.XPATH 技术_快速获取节点
    五、.DOM4J 方式解析 XML 数据
    四、.JDOM 解析 XML 数据
    三、SAX 方式解析 XML 数据
    二、DOM方式解析XML
    一、Schema验证XML
    三、线程同步Synchronized
    二、线程状态
    JDK8Lambda和方法的引用
  • 原文地址:https://www.cnblogs.com/Lemon1234/p/10660855.html
Copyright © 2011-2022 走看看