题意:给你一个骰子的初始状态和可以进行的四种操作,求从初始状态到目标状态的最少操作次数

 

题目本身很简单,bfs即可。但是因为骰子有六个面,搜索判重和记录状态比较麻烦。这时候就需要神器STL了。

  1 #include <iostream>
  2 #include <map>
  3 #include <queue>
  4 #include <vector>
  5 #include <algorithm>
  6 #include <cstdio>
  7 #include <cstring>
  8 using namespace std;
  9 
 10 struct node
 11 {
 12     vector<int> seq;
 13     int step;
 14     node(vector<int> x,int m):seq(x),step(m)
 15     {}
 16 };
 17 
 18 int a[10],b[10];
 19 vector<int> A;
 20 vector<int> y;
 21 //queue<node> Q;
 22 map<vector<int>,int> M;
 23 bool ok;
 24 
 25 void writeln(vector<int> x,node y)
 26 {
 27     for (int i=0;i<6;i++)
 28         cout<<x[i]<<" ";
 29     cout<<" - "<<y.step<<endl;
 30 }
 31 
 32 bool satisfy()
 33 {
 34     for (int i=0;i<6;i++)
 35         if (y[i]!=b[i])     return false;
 36     ok=true;
 37     return true;
 38 }
 39 
 40 void lft()
 41 {
 42     vector<int> t;
 43     t=y;
 44     y[2]=t[0];    y[3]=t[1];    y[1]=t[2];
 45     y[0]=t[3];    y[4]=t[4];    y[5]=t[5];
 46 }
 47 void rht()
 48 {
 49     vector<int> t;
 50     t=y;
 51     y[3]=t[0];    y[2]=t[1];    y[0]=t[2];
 52     y[1]=t[3];    y[4]=t[4];    y[5]=t[5];
 53 }
 54 void fnt()
 55 {
 56     vector<int> t;
 57     t=y;
 58     y[4]=t[0];    y[5]=t[1];    y[2]=t[2];
 59     y[3]=t[3];    y[1]=t[4];    y[0]=t[5];
 60 }
 61 void bak()
 62 {
 63     vector<int> t;
 64     t=y;
 65     y[5]=t[0];    y[4]=t[1];    y[2]=t[2];
 66     y[3]=t[3];    y[0]=t[4];    y[1]=t[5];
 67 }
 68 
 69 bool same()
 70 {
 71     for (int i=0;i<6;i++)
 72         if (a[i]!=b[i])     return false;
 73     return true;
 74 }
 75 
 76 int main()
 77 {
 78     //freopen("in.txt","r",stdin);
 79 
 80     while (cin>>a[0])
 81     {
 82         A.clear();
 83         M.clear();
 84         //Q.clear();
 85         queue <node> Q;
 86         ok=false;
 87 
 88         A.push_back(a[0]);
 89         for (int i=1;i<6;i++)
 90         {
 91             cin>>a[i];
 92             A.push_back(a[i]);
 93         }
 94         for (int i=0;i<6;i++)
 95             cin>>b[i];
 96         if (same())
 97         {
 98             cout<<0<<endl;
 99             continue;
100         }
101 
102         Q.push(node(A,1));
103         //int nm=1;
104         M.insert(pair<vector<int>,int>(A,1));
105 
106         while (!Q.empty())
107         {
108             node tmp=Q.front();
109             Q.pop();
110             int st=tmp.step;
111             st++;
112             y=tmp.seq;
113             //writeln(y,tmp); ////////
114             if (satisfy())
115             {
116                 cout<<st-2<<endl;
117                 break;
118             }
119             /*
120             if (st>55)
121             {
122                 cout<<-1<<endl;
123                 break;
124             }*/
125             for (int tm=1;tm<=4;tm++)
126             {
127                 if (tm==1)
128                 {
129                     y=tmp.seq;
130                     lft();
131                     if (!M.count(y))
132                     {
133                         M.insert(pair<vector<int>,int>(y,1));
134                         Q.push(node(y,st));
135                     }
136                 }
137                 if (tm==2)
138                 {
139                     y=tmp.seq;
140                     rht();
141                     if (!M.count(y))
142                     {
143                         M.insert(pair<vector<int>,int>(y,1));
144                         Q.push(node(y,st));
145                     }
146                 }
147                 if (tm==3)
148                 {
149                     y=tmp.seq;
150                     fnt();
151                     if (!M.count(y))
152                     {
153                         M.insert(pair<vector<int>,int>(y,1));
154                         Q.push(node(y,st));
155                     }
156                 }
157                 if (tm==4)
158                 {
159                     y=tmp.seq;
160                     bak();
161                     if (!M.count(y))
162                     {
163                         M.insert(pair<vector<int>,int>(y,1));
164                         Q.push(node(y,st));
165                     }
166                 }
167             }
168         }
169         if (!ok)    cout<<-1<<endl;
170     }
171 
172 }
View Code

相关文章:

  • 2022-01-25
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2021-05-26
  • 2021-07-27
  • 2021-09-18
  • 2022-01-22
  • 2022-12-23
  • 2021-07-03
相关资源
相似解决方案