-
Notifications
You must be signed in to change notification settings - Fork 0
/
squareRotate.cpp
77 lines (74 loc) · 1.74 KB
/
squareRotate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @file squareRotate.cpp
* @author Long ([email protected])
* @brief Given a rectangle table with 6 square pieces. Each square piece is
* numbered from 1 to 6.
* At each step, we choose a square (left or right), then rotate it clockwise.
*
* | 4 | 1 | 3 | | 1 | 2 | 3 | | 1 | 5 | 2 |
* | | | | <= | | | | => | | | |
* | 5 | 2 | 6 | |4 | 5 | 6 | | 4 | 6 | 3 |
*
* From starting state of table, fine the minimum number of steps to transform into
* ending state of table.
* @version 0.1
* @date 2023-04-13
* @copyright Copyright (c) 2023
*/
#include <iostream>
#include <queue>
using namespace std;
#define LEN 6
int left_pos[6] = {3, 0, 2, 4, 1, 5};
int right_pos[6] = {0, 4, 1, 3, 5, 2};
string Rotate_Left(string a)
{
string b = "";
for (int i = 0; i < LEN; i++)
b += a[left_pos[i]];
return b;
}
string Rotate_Right(string a)
{
string b = "";
for (int i = 0; i < LEN; i++)
b += a[right_pos[i]];
return b;
}
int main()
{
string a, b;
int x;
a = "123456";
b = "412653";
// for (int i = 0; i < LEN; i++)
// {
// cin >> x;
// a += to_string(x);
// }
// for (int i = 0; i < LEN; i++)
// {
// cin >> x;
// b += to_string(x);
// }
pair<string, int> d, k, e;
d.first = a;
d.second = 0;
queue<pair<string, int>> c;
c.push(d);
while (c.size())
{
k = c.front();
c.pop();
if (k.first == b)
{
cout << k.second;
return 0;
}
e.second = k.second + 1;
e.first = Rotate_Left(k.first);
c.push(e);
e.first = Rotate_Right(k.first);
c.push(e);
}
}