# 8 digital Algorithm Research

Source: Internet
Author: User

The following are some algorithm implementations of the Eight Digital problems:

The following code uses a deep iterative search policy. As this algorithm improves the DFS search policy, you still need to restore the status when returning the DFS recursive call:

/* <Br/> * iterative Deep Search <br/> */</P> <p> # include <cstdio> <br/> # include <queue> <br/> # include <stack> <br/> # include <ctime> <br/> # include <cstring> <br/> using namespace STD; </P> <p> # define hashtablesize 362880 <br/> # define not! </P> <p> typedef struct maps {<br/> int detail [3] [3]; <br/> int X, Y; // record space (0) <br/>} map, * pmap; </P> <p> map org; // initial state <br/> map end; // target status <br/> bool hashtable [hashtablesize] = {false }; // hash table <br/> const static int Direction [4] [2] ={{-}, {}, {0,-1 }, {100 }}; // four movable directions </P> <p> int path []; <br/> int step; <br/> int maxdeep; <br/> bool finish; </P> <p>/* <br/> * enter octal digital (the validity check is not performed here) <br/> */<br/> void input () {</P> <P> int I, j; <br/> int sum = 0; </P> <p> printf ("input octal sequence:/N "); <br/> for (I = 0; I <9; I ++) {</P> <p> scanf ("% d", * Org. detail + I );//". "The operator priority is higher than" * "</P> <p> If (0 = * (* Org. detail + I) {</P> <p> Org. X = I/3; <br/> Org. y = I % 3; <br/>}</P> <p> for (I = 0; I <9; I ++) {// calculate the sequence number </P> <p> If (0 = * (* Org. detail + I) <br/> continue; </P> <p> for (j = 0; j <I; j ++) <br/> If (0! = * (* Org. detail + J) & * (* Org. detail + J) <* (* Org. detail + I) <br/>{< br/> sum ++; <br/>}</P> <p> If (sum % 2 = 0) // coordinates of the numbers in the target status <br/>{< br/> end. detail [0] [0] = 1, end. detail [0] [1] = 2, end. detail [0] [2] = 3; <br/> end. detail [1] [0] = 4, end. detail [1] [1] = 5, end. detail [1] [2] = 6; <br/> end. detail [2] [0] = 7, end. detail [2] [1] = 8, end. detail [2] [2] = 0; <br/>}< br/> else <br />{< Br/> end. detail [0] [0] = 1, end. detail [0] [1] = 2, end. detail [0] [2] = 3; <br/> end. detail [1] [0] = 8, end. detail [1] [1] = 0, end. detail [1] [2] = 4; <br/> end. detail [2] [0] = 7, end. detail [2] [1] = 6, end. detail [2] [2] = 5; <br/>}< br/> return; <br/>}</P> <p>/* <br/> * check whether the two statuses are the same <br/> */<br/> inline bool isequal (Map, map B) {</P> <p> return 0 = memcmp (const void *) (*. detail), (const void *) (* B. Detail), 36 ); <br/>}</P> <p>/* <br/> * hash value calculation <br/> */<br/> int hashvalue (Map) {</P> <p> int count; // records the number of reverse orders of an element <br/> int I, j; </P> <p> int value = 0; <br/> static int PV [9] = {24,120,720,504,}; <br/> for (I = 0; I <9; I ++) {<br/> for (j = 0, Count = 0; j <I; j ++) {<br/> If (*. detail + I) <* (*. detail + J) // calculate the number of reverse orders of an element <br/> count ++; <br/>}< br/> value + = PV [I] * count; <br/>}< br/> return value; <br/>}</P> <p>/* <br/> * depth Improvement of Priority Search-iterative deepening search <br/> */<br/> void DFS (MAP & node, int deep) {</P> <p> If (deep> maxdeep) <br/> return; </P> <p> If (true = isequal (node, end )) {</P> <p> finish = true; <br/> step = deep; <br/> return; <br/>}</P> <p> for (int K = 0; k <4 & not finish; k ++) {</P> <p> map TMP = node; <br/> TMP. X = node. X + Direction [k] [0]; <br/> TMP. y = node. Y + Direction [k] [1]; </P> <p> If (TMP. x <0 | TMP. x> 2 | TMP. Y <0 | TMP. y> 2) <br/> continue; </P> <P> TMP. detail [node. x] [node. y] = TMP. detail [TMP. x] [TMP. y]; <br/> TMP. detail [TMP. x] [TMP. y] = 0; </P> <p> int tmpindex = hashvalue (TMP); <br/> If (hashtable [tmpindex] = true) <br/> continue; </P> <p> hashtable [tmpindex] = true; <br/> path [Deep] = K; // here, path information is recorded by the K value indicating the direction <br/> DFS (TMP, deep + 1 ); // use recursive methods <br/> hashtable [tmpindex] = false; <br/>}< br/> return; <br/>}</P> <p>/* <br/> * output result <br/> */</P> <p> void output () {</P> <p> map now = Org; <br/> int oldx, Oldy, I, j; <br/> int COUNT = 0; <br/> printf ("% d required in total. /n ", step); <br/> for (I = 0; I <3; I ++) {<br/> for (j = 0; j <3; j ++) <br/> printf ("% 3d", org. detail [I] [J]); <br/> printf ("/N"); <br/>}</P> <p> for (int K = 0; k <step; k ++) {</P> <p> oldx = now. x; <br/> Oldy = now. y; <br/> now. X + = Direction [path [k] [0]; <br/> now. Y + = Direction [path [k] [1]; </P> <p> now. detail [oldx] [Oldy] = now. detail [now. x] [now. y]; // move the space <br/> now. d Etail [now. x] [now. y] = 0; </P> <p> printf ("/n step % d/N", ++ count); <br/> getchar (); </P> <p> for (I = 0; I <3; I ++) {<br/> for (j = 0; j <3; j ++) <br/> printf ("% 3d", now. detail [I] [J]); <br/> printf ("/N "); <br/>}< br/> printf ("/n the end! /N "); <br/> return; </P> <p >}</P> <p> int main () {</P> <p> input (); <br/> clock_t time = clock (); <br/> hashtable [hashvalue (org)] = true; </P> <p> for (maxdeep = 1; 0 = step; maxdeep ++) {// differences between iterative search and DFS <br/> DFS (ORG, 0); <br/>}</P> <p> printf ("computing time: % DMS/N ", clock ()-time); <br/> output (); <br/> return 0; <br/>}

The disadvantage of the above solution is that the hash function conflict is not considered.

Related Keywords:

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.