密码锁

it2025-03-05  22

 密码锁

题目描述

玛雅人有一种密码,如果字符串中出现连续的2012四个数字就能解开密码。给一个长度为N的字符串,(2=<N<=13)该字符串中只含有0,1,2三种数字,问这个字符串要移位几次才能解开密码,每次只能移动相邻的两个数字。例如02120经过一次移位,可以得到20120,01220,02210,02102,其中20120符合要求,因此输出为1.如果无论移位多少次都解不开密码,输出-1。

输入

第一行输入N,第二行输入N个数字,只包含0,1,2

输出

 

样例输入

5 02120 5 02120

样例输出

1 1 /*#include <stdio.h> #include <iostream> #include <map> #include <string> #include <queue> using namespace std; map<string, int> M;//M[str]表示str经历的交换次数 queue<string> Q;//队列,用于bfs string Swap(string str, int i) {//将字符串i位与i+1位交换 string newStr=str; char tmp=newStr[i]; newStr[i]=newStr[i+1]; newStr[i+1]=tmp; return newStr; } bool Judge(string str) {//判断字符串中是否含有"2012" if(str.find("2012", 0)==string::npos) return false; else return true; } int BFS(string str) {//广度优先搜索 string newStr; M.clear();//清空map while(!Q.empty()) Q.pop();//清空队列 Q.push(str);//初始字符串作为起点放入队列 M[str]=0;//初始字符串经历的交换次数是0 while(!Q.empty()) { str=Q.front(); Q.pop();//取出队首,存入str for(unsigned i=0; i<str.size()-1; i++)//尝试进行一次交换 { newStr=Swap(str, i);//新字符串由str交换i位和i+1位得到 if(M.find(newStr)==M.end())//如果这个字符串没出现过 { M[newStr]=M[str]+1;//现在出现过了,且交换次数比他爹多1 if(Judge(newStr)==true) return M[newStr];//符合要求,收工 else Q.push(newStr);//不合要求,那继续bfs,把它加入队列 } else continue;//出现过的字符串,不用进行处理 } } return -1;//遍历完成,没发现符合要求的字符串,返回-1 } int main() { int n; string str; while(scanf("%d", &n)!=EOF) { cin>>str;//读取字符串(前面读取n好像没啥用) if(Judge(str)==true) printf("0\n");//一开始就符合要求的话 else//初始字符串不符合要求,那就bfs { int ans=BFS(str); printf("%d\n", ans); } } return 0;//大功告成 }*/ #include <bits/stdc++.h> using namespace std; map<string ,int>m; queue<string> q; string strswap(string str,int i){ swap(str[i],str[i+1]); return str; } bool judge(string str){ if(str.find("2012", 0)==string::npos) return false; else return true; } int bfs(string str){ m.clear(); while(!q.empty()) q.pop(); if(judge(str)) return 0; q.push(str); m[str]=0; string str1; while(!q.empty()){ str=q.front(); q.pop(); int len=str.size(); for(int i=0;i<len-1;i++){ str1=strswap(str,i); if(m.find(str1)==m.end()){ m[str1]=m[str]+1; if(judge(str1)) return m[str1]; else q.push(str1); } else continue; } } return -1; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; while(cin>>n&&n){ string str; cin>>str; cout<<bfs(str)<<endl; } return 0; }

  

转载于:https://www.cnblogs.com/qing123tian/p/11107585.html

最新回复(0)