2012年清华:玛雅人的密码

题目描述:

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

输入:

输入包含多组测试数据,每组测试数据由两行组成。
第一行为一个整数N,代表字符串的长度(2<=N<=13)。
第二行为一个仅由0、1、2组成的,长度为N的字符串。

输出:

对于每组测试数据,若可以解出密码,输出最少的移位次数;否则输出-1。

样例输入:
5
02120
样例输出:
1
思路:广搜,字符串长度为13,可用Tire为字符串做hash.
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct Trie{
    Trie *next[3];
    Trie()
    {
        for(int i=0;i<3;i++)
            next[i]=NULL;
    }
}memory[50005];
int tot;
Trie *root;
void Insert(char buf[])
{
    int len=strlen(buf);
    Trie *p=root;
    for(int i=0;i<len;i++)
    {
        int k=buf[i]-'0';
        if(p->next[k]==NULL)
        {
            p->next[k]=&memory[tot++];
        }            
        p=p->next[k];
    }
}
bool Find(char buf[])
{
    int len=strlen(buf);
    Trie *p=root;
    for(int i=0;i<len;i++)
    {
        int k=buf[i]-'0';
        if(p->next[k]==NULL)
        {
            return false;
        }
        p=p->next[k];
    }
    return true;
}
struct Node{
    char s[15];
    int step;
    Node(){}
    Node(char s[],int step)
    {
        strcpy(this->s,s);
        this->step=step;
    }
};
int len;
char buf[15];
void bfs()
{
    queue<Node> que;
    que.push(Node(buf,0));
    Insert(buf);
    while(!que.empty())
    {
        Node now=que.front();que.pop();
        if(strstr(now.s,"2012")!=NULL)
        {
            printf("%d
",now.step);
            return ;
        }
        for(int i=0;i<len-1;i++)
        {
            char s[15]="";
            strcpy(s,now.s);
            char tmp=s[i];
            s[i]=s[i+1];
            s[i+1]=tmp;
            if(!Find(s))
            {
                que.push(Node(s,now.step+1));
                Insert(s);
            }
        }
    }
    printf("-1
");
}
int main()
{    
    while(scanf("%d",&len)!=EOF)
    {
        scanf("%s",buf);
        tot=0;
        memset(memory,0,sizeof(memory));//将结构体中的成员变量全部置为0
        root=&memory[tot++];
        bfs();
    }
    return 0;
}

 字符串hash

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MOD=100007;
struct Node{
    char s[15];
    int step;
    Node(){}
    Node(char s[],int step)
    {
        strcpy(this->s,s);
        this->step=step;
    }
};
int vis[100007];
int len;
char buf[15];
int Hash(char s[])
{
    unsigned int x=1;
    for(int i=0;s[i];i++)
    {
        x*=10;
        x+=(s[i]-'0');
    }
    return x%MOD;
}
void bfs()
{
    memset(vis,0,sizeof(vis));
    queue<Node> que;
    que.push(Node(buf,0));
    vis[Hash(buf)]=1;
    while(!que.empty())
    {
        Node now=que.front();que.pop();
        if(strstr(now.s,"2012")!=NULL)
        {
            printf("%d
",now.step);
            return ;
        }
        for(int i=0;i<len-1;i++)
        {
            char s[15]="";
            strcpy(s,now.s);
            char tmp=s[i];
            s[i]=s[i+1];
            s[i+1]=tmp;
            int code=Hash(s);
            if(!vis[code])
            {
                que.push(Node(s,now.step+1));
                vis[code]=1;
            }
        }
    }
    printf("-1
");
}
int main()
{    
    while(scanf("%d",&len)!=EOF)
    {
        scanf("%s",buf);
        bfs();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5429049.html