当前位置:网站首页>The 8th Blue Bridge Cup 2017 - frog jumping cup
The 8th Blue Bridge Cup 2017 - frog jumping cup
2022-04-23 05:34:00 【Han Tiao】
Title Description
It takes at least a few steps to ask , To jump into another target situation ?
Examples
Input :
WWBB
WWBB
Output :
2
Their thinking :
1.BFS Traverse all States , Storage status and its corresponding steps ;
2. If it is found to be consistent with the results, exit .
matters needing attention :
1. Use a dictionary to store a certain state and the corresponding number of steps , Easy to find duplicate status , Reduce time complexity ;
2. Do not use lists to store status , Because the list cannot be used as a dictionary key , And the application string ;
3.“ Jump cup ” The process can be implemented with a list .
import java.util.*;
public class Main {
public static int answer;
public static int[] X={
-3,-2,-1,1,2,3};
public static Map<String,Integer> map=new HashMap<>();// Store string , Can achieve the effect of checking whether it is repeated
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
String s1=scan.next();// The initial state
String s2= scan.next();// Target state
// Find the location of the empty cup
int ii=0;
for (int i = 0; i < s1.length(); i++) {
if(s1.charAt(i)=='*'){
ii=i;
break;
}
}
// Search for
node start=new node();
start.count=0; start.x=ii; start.s=s1;
Deque<node> que=new LinkedList<>();
que.offer(start);
while(!que.isEmpty()){
node now=que.poll();
if(now.s.equals(s2)){
answer= now.count;
break;
}
for (int i = 0; i < 6; i++) {
int newx= now.x+X[i];
if(newx>=0&&newx<s1.length()){
// Create a modified string
StringBuilder sb=new StringBuilder(now.s);
char temp= sb.charAt(now.x);
sb.setCharAt(now.x, sb.charAt(newx));
sb.setCharAt(newx,temp);
String news= sb.toString();
if(map.get(news)!=null){
// Duplicate string appears
continue;
}else{
map.put(news, 1);
node next=new node();
next.x=newx;
next.count= now.count+1;
next.s=news;
que.offer(next);
}
}
}
}
// Output the answer
System.out.println(answer);
scan.close();
}
}
class node{
int x;// Coordinates of empty cup
int count;// They count
String s;// The current state of the string
}
版权声明
本文为[Han Tiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534299924.html
边栏推荐
- selenium预先加载cookie的必要性
- Fast application fuzzy search
- selenium預先加載cookie的必要性
- Create a tabbar component under the components folder, which is public
- Excel sets row and column colors according to cell contents
- If I am PM's performance, movie VR ticket purchase display
- JS time format conversion
- CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
- [machine learning] scikit learn introduction
- Parameter analysis of open3d material setting
猜你喜欢
Some pits used by uni
what is wifi6?
双击.jar包无法运行解决方法
Understand the relationship between promise async await
Camera imaging + homography transformation + camera calibration + stereo correction
Fast application fuzzy search
Quick app bottom navigation bar
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
After NPM was upgraded, there was a lot of panic
QSS, qdateedit, qcalendarwidget custom settings
随机推荐
Processus d'exécution du programme exécutable
数据安全入门产品——数据库审计系统详解
OSI层常用协议
JVM memory and memory overflow exceptions (personal summary)
踩坑:nacos利用startup.cmd -m standalone启动错误
Log introduction and building web application
Multi process model in egg -- egg document Porter
Create process memory management copy_ Mm - processes and threads (IX)
Output string in reverse order
[untitled] Notepad content writing area
The prefix of static of egg can be modified, including boots
Data bus realizes the communication between brother components
After NPM was upgraded, there was a lot of panic
Click the Add button - a box appears (similar to adding learning experience - undergraduate - Graduate)
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
STL learning notes 0x0001 (container classification)
Nécessité de précharger les cookies dans le sélénium
Necessity of selenium preloading cookies
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
[the background color changes after clicking a line]