uva 11198 - Dancing Digits 隐式图 bfs

简介:

    求最少次数,一般就是bfs或者dfs或者数学公式,这题用bfs加上hash很好写。

    因为比较懒,就用了string类型,于是多出char和string的转换,浪费了很多时间,1.416s才过。

    写hash时不要忘记加绝对值


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
string t;
bool prim[16]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0};
int vis[40321];
bool used[10];
const int fac[8]={5040,720,120,24,6,2,1,1};
string now,ne;
inline int abs(int a){return a>0?a:-a;}
int hash(string s)
{
    memset(used,0,sizeof(used));
    int hh=0,i,j,t;
    for(i=0;i<7;i++)
    {
        s[i]=abs(s[i]);
        t=0;
        used[s[i]]=1;
        for(j=1;j<s[i];j++)
            if(!used[j])t++;
        hh+=t*fac[i];
    }
    return hh;
}
queue<string> q;
char next[10];
int bfs()
{
    while(!q.empty())q.pop();
    q.push(now);
    vis[hash(now)]=1;
    int i,j,k,t,tt;
    while(!q.empty()&&!vis[0])
    {
        now=q.front();q.pop();
        next[8]=now[8]+1;
        for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
            {
                if(now[i]*now[j]>0)continue;
                if(!prim[abs(now[i])+abs(now[j])])continue;
                for(k=t=0;t<8;t++)
                {
                   if(t==i)continue;
                   if(t==j)
                   {
                       tt=k;
                       next[k++]=now[i];
                   }
                   next[k++]=now[t];
                }
                ne=next;
                t=hash(ne);
                if(!vis[t])
                {
                    vis[t]=ne[8];
                    q.push(ne);
                }
                swap(next[tt],next[tt+1]);
                ne=next;
                t=hash(ne);
                if(!vis[t])
                {
                    vis[t]=ne[8];
                    q.push(ne);
                }
            }
        }
    }
    return vis[0]-1;
}
char N[10];
int main()
{
    int C=0,i,t;
    while(scanf("%d",&t)&&t)
    {
        N[0]=t;
        memset(vis,0,sizeof(vis));
        for(i=1;i<8;i++)
        {
         scanf("%d",&t);
         N[i]=t;
        }
        N[8]=1;
        now=N;
        printf("Case %d: %d\n",++C,bfs());
    }
}


目录
相关文章
|
9月前
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
35 0
|
9月前
UVa11679 - Sub-prime
UVa11679 - Sub-prime
34 0
HDU-1061,Rightmost Digit(快速幂)
HDU-1061,Rightmost Digit(快速幂)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
|
机器学习/深度学习
|
移动开发 JavaScript
POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judge   Description Sudoku is a very simple task.
1121 0
poj 1154 letters (dfs回溯)
http://poj.org/problem?id=1154 #include using namespace std; int bb[26]={0},s,r,sum=1,s1=1; char aa[25][25]; int dir[4][2]={-1,0,1,0,0,-1,0,1};...
659 0
扩展KMP - HDU 4333 Revolving Digits
 Revolving Digits Problem's Link   Mean:  给你一个字符串,你可以将该字符串的任意长度后缀截取下来然后接到最前面,让你统计所有新串中有多少种字典序小于、等于、大于原串。
782 0
|
人工智能 BI 算法
uva 1121 - Subsequence
点击打开链接uva 1121 思路:二分查找 分析: 1 题目要求找到一个最短的子序列长度并且这个子序列的和大于等于给定的s 2 如果按照常规的做法枚举起点和终点的话肯定是会超时的。
910 0