HDU 2612 (两边一起)
生活随笔
收集整理的這篇文章主要介紹了
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*/
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); }} }?
轉載于:https://www.cnblogs.com/Lemon1234/p/10660855.html
總結
以上是生活随笔為你收集整理的HDU 2612 (两边一起)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运维选择题(五)
- 下一篇: USB-Blaster无法安装怎么办 U