原题链接:蓝桥杯2017年第八届真题-青蛙跳杯子
解题思路:
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 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复