lanqiao OJ 102 青蛙跳杯子

简介: lanqiao OJ 102 青蛙跳杯子

1.青蛙跳杯子 - 蓝桥云课 (lanqiao.cn)

这个题跟跳蚱蜢那个题一摸一样用map判重就行了 希望今年也能出类似的题

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std ;
struct node{
  string s ; int t ;
  node(string ss ,int tt) {
    s = ss , t = tt ;
  }
};
string st1 , st2 ;
queue<node> q;
map<string,bool>  mp ;
void bfs(){
  while(!q.empty()){
    node now = q.front() ;
    q.pop() ;
    if(now.s == st2) {
      cout << now.t << endl ;
      break ;
    }
    int i  ;
    for(i = 0 ; i < st1.size(); i ++){
      if(now.s[i] == '*') break ;
    } 
      
    for(int j = i-3 ; j <= i +3 ; j ++){
      if(j < 0 || j >= st1.size() || i == j) continue ;
      string ss = now.s ;
      char tmp = ss[i];
      ss[i] = ss[j] ;
      ss[j] = tmp ;
      if(mp[ss]) continue ;
      else {
        mp[ss] = true ;
        q.push(node(ss,now.t + 1)) ;
      }
    }
  }
}
 
int main(){
  cin >> st1 >> st2 ;
//  cout << st1 << endl  << st2 << endl ;
  q.push(node(st1,0)) ;
  mp[st1] = true;
  bfs();
  return 0 ;
}
目录
相关文章
|
3天前
lanqiao OJ 689 四阶幻方
lanqiao OJ 689 四阶幻方
10 0
|
3天前
lanqiao OJ 1388 寒假作业
lanqiao OJ 1388 寒假作业
15 0
|
3天前
lanqiao OJ 1505 剪邮票
lanqiao OJ 1505 剪邮票
10 0
|
3天前
lanqiao OJ 364 跳石头
lanqiao OJ 364 跳石头
18 6
|
3天前
lanqiao OJ 389 摆花
lanqiao OJ 389 摆花
8 2
|
3天前
lanqiao OJ 525 传球游戏
lanqiao OJ 525 传球游戏
11 2
|
3天前
lanqiao OJ 99 分巧克力
lanqiao OJ 99 分巧克力
8 1
|
2天前
lanqiao OJ 2097 青蛙过河
lanqiao OJ 2097 青蛙过河
6 0
|
1天前
lanqiao oj 1121 蓝桥公园(floyd)
lanqiao oj 1121 蓝桥公园(floyd)
6 0
|
1天前
lanqiao oj 1050 补给
lanqiao oj 1050 补给
8 0