当前位置:网站首页>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
边栏推荐
- Sword finger offer II 022 The entry node of the link in the linked list
- Escape characters \ splicing of data formats
- Data mining -- understanding data
- Three methods of list rendering
- deep learning object detection
- [triangle Yang Hui triangle printing odd even cycle JS for break cycle]
- 字符识别easyocr
- 相机成像+单应性变换+相机标定+立体校正
- Qwebsocket communication
- CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
猜你喜欢

Getting started with varnish

C, class library

Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator

Hongji | how does HR carry out self change and organizational change in the digital era?

uni使用的一些坑

Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios

Pytorch deep learning practice_ 11 convolutional neural network

弘玑|数字化时代下,HR如何进行自我变革和组织变革?

Use of uniapp native plug-ins

Frequently asked interview questions - 2 (computer network)
随机推荐
FileReader API file operation
Call the interface to get the time
Log introduction and building web application
The address value indicated by the pointer and the value of the object indicated by the pointer (learning notes)
Edit, cancel, pull up menu
Some pits used by uni
Frequently asked interview questions - 3 (operating system)
世界与个人发展
Establish excel bookkeeping book through setting context menu
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
Box collapse and margin collapse
OSI层常用协议
Executable program execution process
QSslSocket::connectToHostEncrypted: TLS initialization failed
Formal parameters, local variables and local static variables
What financial products will benefit during May Day?
[untitled] Notepad content writing area
Deep learning object detection
Interview Basics
refused connection