Mr.Clutch


私信TA

用户名:uq_63396757599

访问量:5249

签 名:

等  级
排  名 2459
经  验 2202
参赛次数 0
文章发表 20
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

bfs, 我以青蛙位置做bfs,也可以以空杯子位置做bfs
注意事项:

参考代码:

import java.io.*;
import java.util.*;

// https://www.dotcpp.com/oj/problem1878.html
// bfs, 我以青蛙位置做bfs,也可以以空杯子位置做bfs
public class Main {
	static class Frog{
		char c;
		int count;  // 计数
		int index;  // 字符串下标
		String path = "";
		Frog(char c, int index, int count, String path){
			this.c = c;
			this.count = count;
			this.index = index;
			this.path = path;
		}
		
	}
	static BufferedReader cin;
	static PrintWriter out;
	static String start;
	static String end;
	static HashSet<String> map;
	static int n;
	public static void main(String[] args) throws IOException{
		cin = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(new OutputStreamWriter(System.out));
		map = new HashSet<>();
		start = cin.readLine();
		end = cin.readLine();
		n = start.length()-1;
		Queue<Frog> que = new LinkedList<>();
		for(int i = 0; i < start.length(); i++){  // 寻找初始节点
			if(start.charAt(i) == 'W'  || start.charAt(i) == 'B') {  // 是青蛙
				que.add(new Frog(start.charAt(i), i, 0, start));
			}
		}
		map.add(start);
		Frog t = null;
		while(!que.isEmpty()) {
			t = que.poll();
			start = t.path;
			if(start.equals(end))
				break;
			int index = t.index;
			int count = t.count;
			char c = t.c;
			if(index > 0 && start.charAt(index-1)=='*') {  // 青蛙向左跳一格
				StringBuilder nextS = new StringBuilder(start);
				nextS.setCharAt(index-1, c);
				nextS.setCharAt(index, '*');
				String s = nextS.toString();
				if(!map.contains(s)) {
					map.add(s);
					for(int i = 0; i < start.length(); i++){  // 寻找节点
						if(start.charAt(i) == 'W'  || start.charAt(i) == 'B') 
							que.add(new Frog(start.charAt(i), i, count+1, s));
					}
				}
				
			}
			if(index > 1 && start.charAt(index-2)=='*') {  // 青蛙向左跳两格
				StringBuilder nextS = new StringBuilder(start);
				nextS.setCharAt(index-2, c);
				nextS.setCharAt(index, '*');
				String s = nextS.toString();
				if(!map.contains(s)) {
					map.add(s);
					for(int i = 0; i < start.length(); i++){  // 寻找节点
						if(start.charAt(i) == 'W'  || start.charAt(i) == 'B')
							que.add(new Frog(start.charAt(i), i, count+1, s));
					}
				}
			}
			if(index > 2 && start.charAt(index-3)=='*') {  // 青蛙向左跳三格
				StringBuilder nextS = new StringBuilder(start);
				nextS.setCharAt(index-3, c);
				nextS.setCharAt(index, '*');
				String s = nextS.toString();
				if(!map.contains(s)) {
					map.add(s);
					for(int i = 0; i < start.length(); i++){  // 寻找节点
						if(start.charAt(i) == 'W'  || start.charAt(i) == 'B') 
							que.add(new Frog(start.charAt(i), i, count+1, s));
					}
				}
			}
			if(index < n && start.charAt(index+1)=='*') {  // 青蛙向右跳一格
				StringBuilder nextS = new StringBuilder(start);
				nextS.setCharAt(index+1, c);
				nextS.setCharAt(index, '*');
				String s = nextS.toString();
				if(!map.contains(s)) {
					map.add(s);
					for(int i = 0; i < start.length(); i++){  // 寻找节点
						if(start.charAt(i) == 'W'  || start.charAt(i) == 'B')  
							que.add(new Frog(start.charAt(i), i, count+1, s));
					}
				}
			}
			if(index < n-1 && start.charAt(index+2)=='*') {  // 青蛙向右跳两格
				StringBuilder nextS = new StringBuilder(start);
				nextS.setCharAt(index+2, c);
				nextS.setCharAt(index, '*');
				String s = nextS.toString();
				if(!map.contains(s)) {
					map.add(s);
					for(int i = 0; i < start.length(); i++){  // 寻找节点
						if(start.charAt(i) == 'W'  || start.charAt(i) == 'B')  
							que.add(new Frog(start.charAt(i), i, count+1, s));
					}	
				}
				
			}
			if(index < n-2 && start.charAt(index+3)=='*') {  // 青蛙向右跳两格
				StringBuilder nextS = new StringBuilder(start);
				nextS.setCharAt(index+3, c);
				nextS.setCharAt(index, '*');
				String s = nextS.toString();
				if(!map.contains(s)) {
					map.add(s);
					for(int i = 0; i < start.length(); i++){  // 寻找节点
						if(start.charAt(i) == 'W'  || start.charAt(i) == 'B')
							que.add(new Frog(start.charAt(i), i, count+1, s));
					}	
				}
				
			}
			
		}
		out.println(t.count);
		out.flush();
		out.close();
		

	}
	

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区