zoukankan      html  css  js  c++  java
  • ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)

    Problem Description

    There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.

       At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)


       Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.

    Input

    There are multiple test cases. Please process till EOF.

       For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.

       The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.

    Output

     For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.

    Sample Input

    1 2 3 4 5 6
    1 2 3 4 5 6
    1 2 3 4 5 6
    1 2 5 6 4 3
    1 2 3 4 5 6
    1 4 2 5 3 6

    Sample Output

    0
    3
    -1

     

    这个题目可以用bfs遍历向前、向后、向左、向右转 ,这样如果用一个数组a[6]记录一种状态,那么最多也只有6!种状态,数量不是很多,可以直接暴力bfs。不过需要记录每个状态是否被访问过。



    代码:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <cmath>
      6 #include <algorithm>
      7 #include <set>
      8 #include <map>
      9 #include <queue>
     10 #include <string>
     11 #include <vector>
     12 #define inf 0x3fffffff
     13 #define esp 1e-10
     14 using namespace std;
     15 struct node1
     16 {
     17     int dice[6];
     18     int val;
     19 };
     20 struct node
     21 {
     22     node1 qt;
     23     int step;
     24 };
     25 node a;
     26 node1 b;
     27 int bfs()
     28 {
     29     set < int > s;
     30     s.insert(a.qt.val);
     31     queue < node > q;
     32     q.push(a);
     33     while (!q.empty())
     34     {
     35         node f, k;
     36         f = q.front();
     37         q.pop();
     38         if (f.qt.val == b.val) return f.step;
     39         //first
     40         k = f;
     41         swap (k.qt.dice[0], k.qt.dice[5]);
     42         swap (k.qt.dice[4], k.qt.dice[1]);
     43         swap (k.qt.dice[5], k.qt.dice[4]);
     44         k.qt.val = k.qt.dice[0];
     45         for (int y = 1; y < 6; ++y)
     46         {
     47             k.qt.val = 10*k.qt.val + k.qt.dice[y];
     48         }
     49         if (s.find(k.qt.val) == s.end())
     50         {
     51             k.step ++;
     52             q.push(k);
     53             s.insert(k.qt.val);
     54             k.step --;
     55         }
     56         //second
     57         k = f;
     58         swap (k.qt.dice[0], k.qt.dice[5]);
     59         swap (k.qt.dice[4], k.qt.dice[1]);
     60         swap (k.qt.dice[0], k.qt.dice[1]);
     61         k.qt.val = k.qt.dice[0];
     62         for (int y = 1; y < 6; ++y)
     63         {
     64             k.qt.val = 10*k.qt.val + k.qt.dice[y];
     65         }
     66         if (s.find(k.qt.val) == s.end())
     67         {
     68             k.step ++;
     69             q.push(k);
     70             s.insert(k.qt.val);
     71             k.step --;
     72         }
     73         //third
     74         k = f;
     75         swap (k.qt.dice[0], k.qt.dice[2]);
     76         swap (k.qt.dice[1], k.qt.dice[3]);
     77         swap (k.qt.dice[1], k.qt.dice[0]);
     78         k.qt.val = k.qt.dice[0];
     79         for (int y = 1; y < 6; ++y)
     80         {
     81             k.qt.val = 10*k.qt.val + k.qt.dice[y];
     82         }
     83         if (s.find(k.qt.val) == s.end())
     84         {
     85             k.step ++;
     86             q.push(k);
     87             s.insert(k.qt.val);
     88             k.step --;
     89         }
     90         //forth
     91         k = f;
     92         swap (k.qt.dice[0], k.qt.dice[2]);
     93         swap (k.qt.dice[1], k.qt.dice[3]);
     94         swap (k.qt.dice[2], k.qt.dice[3]);
     95         k.qt.val = k.qt.dice[0];
     96         for (int y = 1; y < 6; ++y)
     97         {
     98             k.qt.val = 10*k.qt.val + k.qt.dice[y];
     99         }
    100         if (s.find(k.qt.val) == s.end())
    101         {
    102             k.step ++;
    103             q.push(k);
    104             s.insert(k.qt.val);
    105             k.step --;
    106         }
    107     }
    108     return -1;
    109 }
    110 int main()
    111 {
    112     //freopen ("test.txt", "r", stdin);
    113     while (scanf ("%d", &a.qt.dice[0]) != EOF)
    114     {
    115         for (int i = 1; i < 6; ++i)
    116             scanf ("%d", &a.qt.dice[i]);
    117         a.step = 0;
    118         a.qt.val = a.qt.dice[0];
    119         for (int y = 1; y < 6; ++y)
    120         {
    121             a.qt.val = 10*a.qt.val + a.qt.dice[y];
    122         }
    123         for (int i = 0; i < 6; ++i)
    124             scanf ("%d", &b.dice[i]);
    125         b.val = b.dice[0];
    126         for (int y = 1; y < 6; ++y)
    127         {
    128             b.val = 10*b.val + b.dice[y];
    129         }
    130         printf ("%d
    ", bfs());
    131     }
    132     return 0;
    133 }
    View Code
    把每一道题当作难题去做。
  • 相关阅读:
    asp.net linux 环境部署, jexus
    SAP选择屏幕下拉框实现
    SAP 选择屏幕的上方 (sscrfields) 按钮设置
    SAP笔记
    SAP导出内表数据到excel
    SAP笔记---非-现存任务/请求XXX上的请求锁定
    ABAP知识点笔记
    关联带出字段内容
    REUSE_ALV_GRID_DISPLAY详解
    django发送邮件send_mail&send_mass_mail
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4014507.html
Copyright © 2011-2022 走看看