2014 select 5012 dice (BFS template)

Source: Internet
Author: User
1/* 2 Question: Given two sieves, each of which has six faces and each digit belongs to [], and they are different from each other! 3. Q. a. the sieve is rotated at least as required by the question. The number is the same as that before and after B! 4 5. Train of Thought: BFS is straightforward. It maps each status to a number to ensure that this status will not be added to the queue again! 6 */7 # include <iostream> 8 # include <cstdio> 9 # include <cstring> 10 # include <algorithm> 11 # include <queue> 12 using namespace STD; 13 14 int A [7], SS; 15 int vis [654330]; 16 17 struct node {18 int K [7]; 19 node () {} 20 node (INT A1, int A2, int A3, int A4, int A5, int A6) {21 K [1] = A1; 22 K [2] = a2; 23 K [3] = A3; 24 K [4] = A4; 25 K [5] = A5; 26 K [6] = A6; 27} 28 int step; 29}; 30 31 queue <node> q; 32 33 34 int Sum (node X) {35 int S = 0; 36 for (INT I = 1; I <= 6; ++ I) 37 S = S * 10 + X. K [I]; 38 return s; 39} 40 41 bool BFS () {42 while (! Q. empty () Q. pop (); 43 node cur (A [1], a [2], a [3], a [4], a [5], a [6]); 44 cur. step = 0; 45 Q. push (cur); 46 vis [sum (cur)] = 1; 47 While (! Q. empty () {48 cur = Q. front (); 49 If (sum (cur) = SS) {50 printf ("% d \ n", cur. step); 51 return true; 52} 53 Q. pop (); 54 node * Nt = new node (cur. K [5], cur. K [6], cur. K [3], cur. K [4], cur. K [2], cur. K [1]); 55 int v = sum (* NT); 56 If (! Vis [v]) {57 vis [v] = 1; 58 NT-> step = cur. step + 1; 59 Q. push (* NT); 60} 61 62 Nt = new node (cur. K [6], cur. K [5], cur. K [3], cur. K [4], cur. K [1], cur. K [2]); 63 v = sum (* NT); 64 if (! Vis [v]) {65 vis [v] = 1; 66 NT-> step = cur. step + 1; 67 Q. push (* NT); 68} 69 70 Nt = new node (cur. K [3], cur. K [4], cur. K [2], cur. K [1], cur. K [5], cur. K [6]); 71 v = sum (* NT); 72 If (! Vis [v]) {73 vis [v] = 1; 74 NT-> step = cur. step + 1; 75 Q. push (* NT); 76} 77 78 Nt = new node (cur. K [4], cur. K [3], cur. K [1], cur. K [2], cur. K [5], cur. K [6]); 79 v = sum (* NT); 80 If (! Vis [v]) {81 vis [v] = 1; 82 NT-> step = cur. step + 1; 83 Q. push (* NT); 84} 85} 86 return false; 87} 88 89 int main () {90 while (scanf ("% d", & A [1], & A [2], & A [3], & A [4], & A [5], & A [6])! = EOF) {91 Ss = 0; 92 for (INT I = 1; I <= 6; ++ I) {93 int X; 94 scanf ("% d ", & X); 95 Ss = SS * 10 + X; 96} 97 memset (VIS, 0, sizeof (VIS); 98 If (! BFS () 99 printf ("-1 \ n"); 100} 101 return 0; 102}

 

2014 select 5012 dice (BFS template)

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.