HDU 5012 BFS水

2014 ACM/ICPC Asia Regional Xi‘an Online

对于一个筛子,规定了以底面的四个边为轴,可以进行翻转,给出起始状态,求最少步骤到目标状态。

简单BFS

#include "stdio.h"
#include "string.h"
#include "math.h"
#include "queue"
using namespace std;

struct node
{
    int s[7];
    int status,step;
};

int aim,a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,ans;
int hash[1001000];

int dfs()
{
    queue<node>q;
    node cur,next;
    int i;
    cur.s[1]=a1;
    cur.s[2]=a2;
    cur.s[3]=a3;
    cur.s[4]=a4;
    cur.s[5]=a5;
    cur.s[6]=a6;
    cur.status=0;

    for (i=1;i<=6;i++)
        cur.status=cur.status*10+cur.s[i];
    hash[cur.status]=1;
    cur.step=0;

    q.push(cur);
    while (!q.empty())
    {
        cur=q.front();
        q.pop();

        next.s[1]=cur.s[6]; next.s[2]=cur.s[5]; next.s[3]=cur.s[3]; next.s[4]=cur.s[4]; next.s[5]=cur.s[1]; next.s[6]=cur.s[2];
        next.status=0;
        for (i=1;i<=6;i++)
            next.status=next.status*10+next.s[i];
        if (hash[next.status]==0) { hash[next.status]=1; next.step=cur.step+1; q.push(next); if (next.status==aim) return next.step;}

        next.s[1]=cur.s[5]; next.s[2]=cur.s[6]; next.s[3]=cur.s[3]; next.s[4]=cur.s[4]; next.s[5]=cur.s[2]; next.s[6]=cur.s[1];
        next.status=0;
        for (i=1;i<=6;i++)
            next.status=next.status*10+next.s[i];
        if (hash[next.status]==0) { hash[next.status]=1; next.step=cur.step+1; q.push(next);if (next.status==aim) return next.step;}

        next.s[1]=cur.s[4]; next.s[2]=cur.s[3]; next.s[3]=cur.s[1]; next.s[4]=cur.s[2]; next.s[5]=cur.s[5]; next.s[6]=cur.s[6];
        next.status=0;
        for (i=1;i<=6;i++)
            next.status=next.status*10+next.s[i];
        if (hash[next.status]==0) { hash[next.status]=1; next.step=cur.step+1; q.push(next);if (next.status==aim) return next.step;}

        next.s[1]=cur.s[3]; next.s[2]=cur.s[4]; next.s[3]=cur.s[2]; next.s[4]=cur.s[1]; next.s[5]=cur.s[5]; next.s[6]=cur.s[6];
        next.status=0;
        for (i=1;i<=6;i++)
            next.status=next.status*10+next.s[i];
        if (hash[next.status]==0) { hash[next.status]=1; next.step=cur.step+1; q.push(next);if (next.status==aim) return next.step;}
    }

    return -1;

}

int main()
{
    while (scanf("%d%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5,&a6)!=EOF)
    {
        scanf("%d%d%d%d%d%d",&b1,&b2,&b3,&b4,&b5,&b6);
        if (a1==b1 && a2==b2 && a3==b3 && a4==b4 && a5==b5 && a6==b6)
        {
            printf("0\n");
            continue;
        }
        aim=b1*100000+b2*10000+b3*1000+b4*100+b5*10+b6;

        ans=-1;
        memset(hash,0,sizeof(hash));
        ans=dfs();
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-09 21:27:59

HDU 5012 BFS水的相关文章

hdu 5012 bfs --- 慎用STL 比如MAP判重

http://acm.hdu.edu.cn/showproblem.php?pid=5012 发现一个问题 如果Sting s = '1'+'2'+'3'; s!="123"!!!!!!  而是有乱码 先贴一份自己的TLE 代码, 超时应该是因为: 1.cin 2.map判重 map find太花时间 3.string花时间 4.其实不用两个都旋转,只要旋转一个就行,这样可以省下很多时间 包括少用了make_pair pair的判重等等....哎  二笔了  太暴力了 #include

HDU 5012 bfs暴搜

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 243    Accepted Submission(s): 135 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number wa

hdu 5012 bfs 康托展开

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 491    Accepted Submission(s): 290 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number w

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 4707 bfs

bfs基础算法水题 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<queue> using namespace std; const int Max = 1e5+50; int dist[Max]; vector<int> t

hdu 1175 bfs 转弯题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 和之前的1728类似.就是判断转弯数,建立一个用于记录转弯数的数组.. 还有就是对于特殊情况要进行考虑,比如起点与终点相同的情况,对于本题来说是不可以消去的应该输出NO.还有就是起点或终点是零这也是不行的,因为0代表没有棋子... 还有在判断能不能走的时候要小心,对于判断条件一定要小心,不要图赶快写.. 错误的地方都写在注释中了.. 代码: // hdu 1175 bfs 转弯数 //1.起点

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

HDU 2092 整数解 --- 水题

x+y = n, x*y = m; y = n - x; x * ( n - x) = m nx - x^2 = m; x^2 - nx + m = 0; △ = sqrt(n^2 - 4m) 要有整数解即△需要为可开方数即可. /* HDU 2092 整数解 --- 水题 */ #include <cstdio> #include <cmath> int main() { double n, m; while (scanf("%lf%lf", &n,