首页 > 其他 > 详细

POJ 3221 Diamond Puzzle

时间:2014-08-07 23:03:25      阅读:527      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣

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:博客写的太简单了,因为时间比较紧。所以以后时间充足的话还是详细点好。

题意:由当前状态得到最终状态需要最小步数。只允许在空格处移动=-=,从磨状态开始搜

所以就记录空格所在位子以及由此空格可以移动到的位置。比如:空格在位置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\n",0);
        else
            printf("%d\n",ans[s1]==0?-1:ans[s1]);
    }
    return 0;
}


POJ 3221 Diamond Puzzle,布布扣,bubuko.com

POJ 3221 Diamond Puzzle

原文:http://blog.csdn.net/u013582254/article/details/38427129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!