Usaco magic squares solution report

Source: Internet
Author: User

In the detection 8! = 40320, I found that space is not an influence factor. As long as no repeated operations are performed, the time and space are sufficient.

For reference:

1. Processing of conversions:

/* The set of transformations, in order */

Static int tforms [3] [8] = {8, 7, 6, 5, 4, 3, 2, 1}, {4, 1, 2, 3, 6, 7, 8, 5 },

{1, 7, 2, 4, 5, 3, 6, 8 }};

Here is another way of thinking, which is easier to handle. The idea is to directly understand the conversion as a ing of Different Copy locations (positions after initial position conversion. For example, in the first conversion, 0th is to copy 7th bits (8-1 ). The first conversion is actually this array, that is, the ing of the copy position.

2. Expand conto

Conto expansion is a good theorem: it is used to find a sequence that is arranged, for example, 45213 in the full arrangement of the five numbers. Can directly move here: http://blog.csdn.net/fairyroad/article/details/7555773

CodeWell written.

/* ID: thestor1lang: C ++ task: msquare */# include <iostream> # include <cmath> # include <cstdio> # include <cstring> # include <vector> # include <cassert> # include <string> # include <algorithm> # include <stack> # include <set> using namespace STD; const int size = 8; const int max = 40320; // 8! = 40320 struct square {int h; int square [size]; char op; int Prev; Square () {H =-1;} int Hash () {If (h> 0) {return h;} int h = 0; For (INT I = 0; I <size; ++ I) {H = (H <3) + square [I];} return H ;}}; square squares [Max]; bool equal (square & S1, square & S2) {return s1.hash () = s2.hash ();} bool equal (int * S1, int * S2) {for (INT I = 0; I <size; ++ I) {If (S1 [I]! = S2 [I]) {return false;} return true;} void copy (int * s, int * D) {for (INT I = 0; I <size; ++ I) {d [I] = s [I] ;}} void a (int * Square) {for (INT I = 0; I <= 3; ++ I) {int TMP = square [I]; Square [I] = square [7-I]; Square [7-I] = TMP ;}} void B (int * Square) {int TMP = square [3]; for (INT I = 3; I> = 1; -- I) {square [I] = square [I-1];} square [0] = TMP; TMP = square [4]; for (INT I = 4; I <= 6; ++ I) {square [I] = square [I + 1];} square [7] = TMP;} void C (int * Square) {int TMP = square [1]; Square [1] = square [6]; square [6] = square [5]; Square [5] = square [2]; Square [2] = TMP;} int main () {file * fin = fopen ("msquare. in "," R "); file * fout = fopen (" msquare. out "," W "); // freopen (" log.txt "," W ", stdout); Square targetsq; // int target [size]; for (INT I = 0; I <size; ++ I) {fscanf (FIN, "% d", & targetsq. square [I]);} set <int> SQS; int Top = 0; (INT I = 0; I <size; ++ I) {squares [Top]. square [I] = I + 1;} SQS. insert (squares [Top]. hash (); top ++; int final =-1; for (INT I = 0; I <top; ++ I) {// fprintf (stdout, "I: % d, top: % d \ n ", I, top); Square sq = squares [I]; If (equal (SQ, targetsq) {final = I; break;} copy (sq. square, squares [Top]. square); A (squares [Top]. square); If (SQS. find (squares [Top]. hash () = SQS. end () {squares [Top]. prev = I; squares [Top]. OP = 'a '; SQS. insert (squares [Top]. hash (); top ++;} copy (sq. square, squares [Top]. square); B (squares [Top]. square); If (SQS. find (squares [Top]. hash () = SQS. end () {squares [Top]. prev = I; squares [Top]. OP = 'B'; SQS. insert (squares [Top]. hash (); top ++;} copy (sq. square, squares [Top]. square); C (squares [Top]. square); If (SQS. find (squares [Top]. hash () = SQS. end () {squares [Top]. prev = I; squares [Top]. OP = 'C'; SQS. insert (squares [Top]. Hash (); top ++ }}stack <char> seq; while (final! = 0) {seq. push (squares [Final]. OP); Final = squares [Final]. prev;} fprintf (fout, "% d \ n", seq. size (); While (! Seq. empty () {fprintf (fout, "% C", seq. top (); seq. pop ();} fprintf (fout, "\ n"); Return 0 ;}

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.