九度_清华2012_玛雅人的密码

题目链接

http://ac.jobdu.com/problem.php?pid=1482

说实话,看到这个题目完全没有思路,所以去看了很多代码,都是说用广搜,但是自己还是想了很久,然后看懂了学长的代码,照着抄理解了下。

然后意识到,真的是学的东西完全不会用,所以决定要努力,从现在开始也不晚。

#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node{
		string str;
		int step;
	};
	int vis[1600000];
queue<node> que; 
int n;
string s;
int judge(string s)
{
	if(s.find("2012")!=-1)
		return 1;
	return 0;
}
int getnum(string s)
{
	int x=0;
	for(int i=0;i<n;i++)
	{
		x*=3;
		x+=s[i]-'0'; 
	}
	return x;
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
	//	cin.ignore();
		cin>>s;
		//cout<<judge(s)<<endl;
		while(!que.empty())  
            que.pop();  
        memset(vis,0,sizeof vis); 
		node test;
		test.str=s;
		test.step=0;
		que.push(test);
		int flag=0;
		while(!que.empty())
		{
			test=que.front();
			que.pop();
			if(judge(test.str))
			{
				flag=1;
				cout<<test.step<<endl;
				break;
			}
			for(int i=0;i<n-1;i++)
			{
				swap(test.str[i],test.str[i+1]);
				int num=getnum(test.str);
				if(!vis[num])
				{
					vis[num]=1;
					node p;
					p.str=test.str;
					p.step=test.step+1;
					que.push(p);
				}
				swap(test.str[i],test.str[i+1]);
			}
		}
		if(!flag)
			cout<<"-1"<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值