Maze Problem with weight 1 and 2

本文介绍了一个模拟救援场景的问题:一名学生需要尽快从监狱中救出被囚禁的Hong。监狱地图由道路、障碍和墙壁组成,学生可以清除障碍来缩短救援时间。文章提供了问题描述、输入输出格式及示例,并附带了Java实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Hong was kidnapped by a bad woman. She was put in a prison. She transmitted distress signals to S students to save her as soon as possible.

The prison can be considered as a N\times MN×M matrix. Each grid can be road, barrier or wall. In each unit time, a student can move to an adjacent road(two grid are adjacent if and only if they have public side) or change an adjacent barrier to road. Wall can not be passed.

These S students are originally at S different positions and depart at t = 0 . Each of them wants to save Hong as soon as possible. Hong wants to know when she will be saved.

Input

The first line contains two integers N and M .

Each of the next N lines contains M chars, 'R' is a grid of road, 'B' is a grid of barrier, 'W' is a grid of wall, 'H' is the position of Hong, 'S' is the position of a student.

Output

Output a single integer, shows the minimal time Hong can be saved.

We guarantee that there must be a way to save Hong.

Sample Input

3 3
SBW
WRB
RBH

Copy

Sample output

6

Copy

Limit

1 second for each test case. The memory limit is 256MB.

For 20% test cases, N,M \leq 200N,M≤200.

For 50% test cases, there is no barrier.

For 100% test cases, N,M \leq 2000N,M≤2000.

 

Solution

import java.io.*;
import java.util.*;
class Node implements Comparable<Node>{
   int x, y, dist = 0;
   Node(int x, int y, int dist){this.x = x; this.y = y; this.dist = dist;}
   @Override
   public int compareTo(Node node){
	  return dist - node.dist;
   }
}
public class Main{
   static int[] dx = {-1, 1,0, 0 };
   static int[] dy = {0, 0, 1, -1};
   static int N = 0;
   static int M = 0;
   static char[][] state = null;
   public static void main(String[] args)throws Exception{
	  InputStream sys = System.in;
	 // InputStream fil = new FileInputStream(new File("C:\\Users\\ASUS\\desktop\\data4.txt"));
	  long b = System.currentTimeMillis();
	  BufferedReader in = new BufferedReader(new InputStreamReader(sys));
	  PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	  StringTokenizer st;
	  st = new StringTokenizer(in.readLine()); //buffer: 31KiB
	  N = Short.valueOf(st.nextToken());
	  M = Short.valueOf(st.nextToken());
	  state = new char[N][M];//2000*2000*8 bits 30MiB W:0 R:1 S:2 B:3
	  int H_x = 0, H_y = 0;
	  char[] line;
	  for(short n = 0; n < N; ++n) {
		 line = in.readLine().toCharArray();
		 for(short m = 0; m < M; ++m) {
			if (line[m] == 'H') {
			   H_x = n;
			   H_y = m;
			   state[n][m] = 'R';
			}else{
			   state[n][m] = line[m];
			}
		 }
	  }
	  PriorityQueue<Node> queue = new PriorityQueue<Node>();
	  Node H = new Node(H_x, H_y, 0);
	  queue.add(H);
	  Node p = null;
	  boolean Found = false;
	  while(!queue.isEmpty() && !Found) {
		 p = queue.poll();
		 for(int i = 0; i < 4; ++i) {
			int x = p.x + dx[i];
			int y = p.y + dy[i];
			if (x < 0 || x >= N || y < 0 || y >= M) continue;
			if (state[x][y] == 'W') continue;
			if (state[x][y] == 'S') {
			   out.println(p.dist + 1);
			   Found = true;
			   break;
			}else if (state[x][y] == 'B') {
			   queue.add(new Node(x, y, p.dist + 2));
			}else {
			   queue.add(new Node(x, y, p.dist + 1));
			}
			state[x][y] = 'W';
		 }
	  }
	  //+" ("+D.x+","+D.y+")"
	  out.flush();
	 // System.out.println((System.currentTimeMillis() - b )*1.0/1000.0+"s");
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值