Question: hdu5012dice (Implicit Graph Search)
I will give you a two-sided sequence number that specifies the serial number of each plane. Now I will give you the color (A1, A2, A3, A4, A5, A5) (B1, b2, B3, B4, B5, B6 ). It is now required that the sequence is the same as the B sequence through the following four rotations. If yes, the minimum number of rotated steps cannot be output-1.
Problem-solving ideas: BFS + map heavy judgment. The four rotating states should be clarified.
Code:
# Include <cstdio> # include <cstring> # include <map> using namespace STD; const int n = 6; const int maxn = 30; const int dir [4] [N] = {3, 2, 0, 1, 4, 5}, {2, 3, 1, 0, 4, 5 }, {5, 4, 2, 3, 0, 1}, {4, 5, 2, 3, 1, 0}; struct state {int dice [N];} st [maxn]; int ans; int P [maxn]; Map <int, int> vis; int Hash (State A) {int sum = 0; for (INT I = 0; I <n; I ++) sum = sum * 10 +. dice [I]; return sum;} int BFS () {int front, Rear; front = 0; Rear = 1; vis. clear (); P [0] = 0; vis [Hash (ST [0])] = 1; while (front <rear) {If (Hash (ST [Front]) = ans) return front; For (INT I = 0; I <4; I ++) {for (Int J = 0; j <n; j ++) st [rear]. dice [J] = sT [Front]. dice [dir [I] [J]; int TMP = hash (ST [rear]); If (! Vis [TMP]) {vis [TMP] = 1; p [rear] = P [Front] + 1; Rear ++ ;}} front ++ ;} return-1 ;}int main () {int num; while (scanf ("% d", & St [0]. dice [0], & St [0]. dice [1], & St [0]. dice [2], & St [0]. dice [3], & St [0]. dice [4], & St [0]. dice [5])! = EOF) {ans = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & num ); ans = ans * 10 + num;} int flag = BFS (); If (flag =-1) printf ("-1 \ n "); elseprintf ("% d \ n", P [flag]);} return 0 ;}
Hdu5012dice (Implicit Graph Search)