日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 2612 (两边一起)

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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); }} }

?

轉載于:https://www.cnblogs.com/Lemon1234/p/10660855.html

總結

以上是生活随笔為你收集整理的HDU 2612 (两边一起)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。