HDU 5012 Dice DFS

简单DFS

  1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
  2 #include <stdio.h>
  3 #include <iostream>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <stack>
  7 #include <queue>
  8 #include <vector>
  9 #include <algorithm>
 10 #define ll long long
 11 #define Max(a,b) (((a) > (b)) ? (a) : (b))
 12 #define Min(a,b) (((a) < (b)) ? (a) : (b))
 13 #define Abs(x) (((x) > 0) ? (x) : (-(x)))
 14 using namespace std;
 15 
 16 const int INF = 0x3f3f3f3f;
 17 const int MAXN = 220;
 18 const double eps = 1e-9;
 19 
 20 int a[7];
 21 int b[7];
 22 bool ans;
 23 int cnt;
 24 
 25 void lleft(){
 26     int temp_a1 = a[1];
 27     int temp_a2 = a[2];
 28     int temp_a3 = a[3];
 29     int temp_a4 = a[4];
 30     a[1] = temp_a4;
 31     a[2] = temp_a3;
 32     a[3] = temp_a1;
 33     a[4] = temp_a2;
 34 }
 35 
 36 void rright(){
 37     int temp_a1 = a[1];
 38     int temp_a2 = a[2];
 39     int temp_a3 = a[3];
 40     int temp_a4 = a[4];
 41     a[1] = temp_a3;
 42     a[2] = temp_a4;
 43     a[3] = temp_a2;
 44     a[4] = temp_a1;
 45 }
 46 
 47 void ffront(){
 48     int temp_a1 = a[1];
 49     int temp_a2 = a[2];
 50     int temp_a5 = a[5];
 51     int temp_a6 = a[6];
 52     a[1] = temp_a6;
 53     a[2] = temp_a5;
 54     a[5] = temp_a1;
 55     a[6] = temp_a2;
 56 }
 57 
 58 void bback(){
 59     int temp_a1 = a[1];
 60     int temp_a2 = a[2];
 61     int temp_a5 = a[5];
 62     int temp_a6 = a[6];
 63     a[1] = temp_a5;
 64     a[2] = temp_a6;
 65     a[5] = temp_a2;
 66     a[6] = temp_a1;
 67 }
 68 
 69 bool same(){
 70     if(a[1] == b[1] && a[2] == b[2] && a[3] == b[3] && a[4] == b[4] && a[5] == b[5] && a[6] == b[6])
 71         return true;
 72     return false;
 73 }
 74 
 75 void dfs(int count){
 76     if(count > 5){
 77         return;
 78     }
 79     if(same()){
 80         if(count < cnt){
 81             cnt = count;
 82         }
 83         return;
 84     }
 85 
 86     int a1 = a[1];
 87     int a2 = a[2];
 88     int a3 = a[3];
 89     int a4 = a[4];
 90     int a5 = a[5];
 91     int a6 = a[6];
 92 
 93     lleft();
 94     dfs(count + 1);
 95     a[1] = a1;
 96     a[2] = a2;
 97     a[3] = a3;
 98     a[4] = a4;
 99     a[5] = a5;
100     a[6] = a6;
101     rright();
102     dfs(count + 1);
103     a[1] = a1;
104     a[2] = a2;
105     a[3] = a3;
106     a[4] = a4;
107     a[5] = a5;
108     a[6] = a6;
109     ffront();
110     dfs(count + 1);
111     a[1] = a1;
112     a[2] = a2;
113     a[3] = a3;
114     a[4] = a4;
115     a[5] = a5;
116     a[6] = a6;
117     bback();
118     dfs(count + 1);
119 }
120 
121 int main(){
122     int i, j, t, n, m, k;
123     while(EOF != scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])){
124         scanf("%d%d%d%d%d%d",&b[1],&b[2],&b[3],&b[4],&b[5],&b[6]);
125         cnt = INF;
126         ans = false;
127         dfs(0);
128         if(cnt != INF)
129             printf("%d
",cnt);
130         else
131             printf("-1
");
132     }
133     return 0;
134 }
原文地址:https://www.cnblogs.com/wushuaiyi/p/3988252.html