Hdu5012dice (Implicit Graph Search)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.