zoukankan      html  css  js  c++  java
  • POJ 3221 Diamond Puzzle(BFS)

    Description

    A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below. And in this problem the faces produced by the tessellation are identified as they are numbered in the same figure. If two faces share a side, they are called neighboring faces. Thus, even-numbered faces have three neighboring faces, while odd-numbered faces have only two. At any point during the play of the puzzle, six of the seven faces hold a unique digit ranging from 1 to 6, and the other one is empty. A move in the puzzle is to move a digit from one face to a neighboring empty one.

    Starting from any configuration, some series of moves can always make the puzzle look identical to either one shown in Figures 2 and 3. Your task is to calculate the minimum number of moves to make it become the one inFigure 2.

    Input

    The input contains multiple test cases. The first contains an integer N (0 ≤ N ≤ 5,040), the number of test cases. Then follow N lines, each with a permutation of {0, 1, 2, 3, 4, 5, 6} describing a starting configuration of the puzzle. The ith digit in the permutation is the one in the face numbered i − 1. A zero means the face is empty.

    Output

    For each test cases, output the minimum number of moves the configuration takes to reach the one shown in Figure 2. If this is impossible, just output “-1” and nothing else.

    Sample Input

    3
    1324506
    2410653
    0123456

    Sample Output

    10
    -1
    0


    渣渣水平看了题解,果断bfs走起。

    ps:博客写的太简单了,由于时间比較紧。所以以后时间充足的话还是具体点好。

    题意:由当前状态得到终于状态须要最小步数。仅仅同意在空格处移动=-=。从终于状态開始搜

    所以就记录0所在位子以及由此0能够移动到的位置。

    比方:0在位置2,则3,0,1号位置都能够移动到此位置。

    另外的对于我来说就是STL里map的使用了,映射关系map<string,int>visit记录有木有被訪问过,map<string,int>ans记录到谋一状态所需的最小步数。

    以下贴挫码。

    思路来源:点击打开链接

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    #include<queue>
    #include<map>
    using namespace std;
    struct node{
        char s[10];
        int step;
    };
    map<string,int>visit;
    map<string,int>ans;
    char s1[10],s2[10]="0123456";
    int dr[8][5]={{2,4,6,-1},{2,6,-1},{1,3,0,-1},{2,4,-1},{3,5,0,-1},{4,6,-1},{1,5,0,-1}};//记录从0開始的每一个位置假设为0时其它能够移动到此位置的点的位置
    void bfs()
    {
        queue<node>q;
        node st,ed;
        strcpy(st.s,s2);
        st.step=0;
        visit[s2]=1;
        q.push(st);
        while(!q.empty())
        {
            st=q.front();
            q.pop();
    
            ans[st.s]=st.step;
            int temp;
            for(int i=0;i<7;i++)//找到0的位置
            {
                if(st.s[i]=='0')
                {
                   temp=i;
                   break;
                }
            }
            for(int i=0;dr[temp][i]!=-1;i++)//对于能够到0的位置依次搜索
            {
                char ss[10];
                strcpy(ss,st.s);
                ss[temp]=st.s[dr[temp][i]];//交换位置
                ss[dr[temp][i]]='0';
                if(!visit[ss])//没有被訪问过
                {
                    visit[ss]=1;
                    strcpy(ed.s,ss);
                    ed.step=st.step+1;
                    q.push(ed);
                }
            }
        }
    }
    int main()
    {
        bfs();
        int n;
        scanf("%d",&n);
        while(n--)
        {
            getchar();
            scanf("%s",s1);
            if(!strcmp(s1,s2))
                printf("%d
    ",0);
            else
                printf("%d
    ",ans[s1]==0?-1:ans[s1]);
        }
        return 0;
    }
    


  • 相关阅读:
    华为云亮相QCon2020深圳站,带你体会大厂的云原生玩法与秘诀
    没有它你的DevOps是玩不转的,你信不?
    开发实践丨用小熊派STM32开发板模拟自动售货机
    【基于C#的ASP.NET】错误篇1——用户系统+管理员系统CS1061: “ASP.manage_aspx”不包含“SqlDataSource1_Selecting”的定义
    阿里云高级技术专家白常明:边缘云的技术挑战和应用创新
    T级内存,创建效率提升10倍以上,阿里云 KVM异构虚拟机启动时间优化实践
    终极清单来了!阿里云双11爆款揭晓
    2020阿里云双110.73折起,爆款提前抢大促全攻略
    直面最大挑战双11 阿里数据中台为商家带来确定性保障
    OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6806435.html
Copyright © 2011-2022 走看看