BFS + 剪枝 之 hdu 5012 Dice
2014-09-17 13:49
477 查看
/* BFS 遍历所有状态,当寻找到要转到的状态,输出BFS的层数,即答案,否则输出 -1; 注意: 剪枝: 当BFS到某一种状态时,若此状态已在前面所寻找的状态中出现过,则不用再继续从此状态BFS了,因为此状态所能寻找的状态已经 在之前都处理过了。 */
#include <iostream> #include <cstdlib> #include <cstdio> #include <queue> #include <vector> #include <map> using namespace std; struct myNode { int Top, Bottom, Left, Right, Front, Back; }; typedef pair<myNode, int> P; myNode node[2]; vector<myNode> vec; void myDeal(int myCase, myNode myNd, myNode &t_Nd) { // top face, bottom face, left face, right face, front face and back face switch (myCase) { case 1: { t_Nd.Top = myNd.Right; t_Nd.Bottom = myNd.Left; t_Nd.Left = myNd.Top; t_Nd.Right = myNd.Bottom; t_Nd.Front = myNd.Front; t_Nd.Back = myNd.Back; return; } case 2: { t_Nd.Top = myNd.Left; t_Nd.Bottom = myNd.Right; t_Nd.Left = myNd.Bottom; t_Nd.Right = myNd.Top; t_Nd.Front = myNd.Front; t_Nd.Back = myNd.Back; return; } case 3: { t_Nd.Top = myNd.Back; t_Nd.Bottom = myNd.Front; t_Nd.Front = myNd.Top; t_Nd.Back = myNd.Bottom; t_Nd.Left = myNd.Left; t_Nd.Right = myNd.Right; return; } case 4: { t_Nd.Top = myNd.Front; t_Nd.Bottom = myNd.Back; t_Nd.Front = myNd.Bottom; t_Nd.Back = myNd.Top; t_Nd.Left = myNd.Left; t_Nd.Right = myNd.Right; return; } default: system("pause"); return; } } bool myJudge(const myNode n1, const myNode n2) { if ((n1.Top == n2.Top) && (n1.Bottom == n2.Bottom) && (n1.Front == n2.Front) && (n1.Back == n2.Back) && (n1.Left == n2.Left) && (n1.Right == n2.Right)) { return true; } return false; } bool Judge(myNode nd) { for (int i = 0; i < vec.size(); ++i) { if (myJudge(vec[i], nd)) { return true; } } return false; } void Solve() { queue<P> que; que.push(P(node[0], 0)); while (!que.empty()) { P p1 = que.front(); if (Judge(p1.first)){ //cout << "Yes" << endl; que.pop(); continue; } vec.push_back(p1.first); que.pop(); if (myJudge(p1.first, node[1])) { printf("%d\n", p1.second); return; } myNode t_Nd; for (int i = 1; i <= 4; ++i) { myDeal(i, p1.first, t_Nd); //cout << t_Nd.Top << " " << t_Nd.Bottom << " " << t_Nd.Left << " " << t_Nd.Right << " " << t_Nd.Front << " " << t_Nd.Back << endl; que.push(P(t_Nd, p1.second + 1)); } } printf("-1\n"); } int main() { //freopen("input.txt", "r", stdin); int n; while (~scanf("%d %d %d %d %d %d", &node[0].Top, &node[0].Bottom, &node[0].Left, &node[0].Right, &node[0].Front, &node[0].Back)) { scanf("%d %d %d %d %d %d", &node[1].Top, &node[1].Bottom, &node[1].Left, &node[1].Right, &node[1].Front, &node[1].Back); Solve(); if (!vec.empty()) vec.clear(); } return 0; }
相关文章推荐
- BFS + 剪枝 之 hdu 5012 Dice
- HDU 5012 Dice (bfs + 记忆化搜索)
- hdu 5012 Dice 2014 ACM/ICPC Asia Regional Xi'an Online bfs
- ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)
- HDU 5012 Dice (bfs)
- hdu 5012 dice
- HDU - 5012 Dice
- hdu 4474 转化为bfs + 一个巧妙的剪枝~
- HDU 5012 Dice 普通bfs
- hdu 5012 Dice 2014 ACM/ICPC Asia Regional Xi'an Online
- HDU 5012 Dice
- Dice - HDU 5012 搜索
- hdu5012 Dice
- HDU 3533 Escape (bfs + 预处理 + 剪枝)
- HDU-#5012 Dice(BFS)
- bfs-HDU 5012Dice
- HDU 5012 Dice (BFS)
- hdu 5012——Dice
- hdu 5012 Dice BFS 2014 ACM/ICPC Asia Regional Xi'an Online
- 【搜索】 HDU 5012 Dice