Server guard marathon Board

Source: Internet
Author: User

In fact, some people have raised the question of marathon board very early. As early as 1823, J. C. warnsdorff proposed a famous algorithm. When each node selects its subnodes, it first selects the smallest 'egresses 'for search. The 'egresses' indicates the number of feasible subnodes in these subnodes, that is to say, the smaller the 'suntzu 'node, the higher the jump priority. Why is this option selected? This is an optimal method for local adjustment. If multiple child nodes are selected first, there will be more and more sub-nodes with fewer outlets, and it is likely that the 'dead 'node will appear (as the name suggests, there is no exit and no skip node). In this way, the following search is purely futile, this will waste a lot of useless time. In turn, if you choose to jump to a few nodes each time, then fewer and fewer nodes will be exported, and the chances of success will be even greater. This kind of algorithm is called greedy algorithm, also called greedy algorithm or heuristic algorithm. It makes optimal adjustments to the local part of the entire solution process. It is only suitable for finding optimal solutions or partial decomposition, but cannot find the optimal solution. This adjustment method is called greedy strategy. It is uncertain about what greedy strategy is needed, and the specific problem is analyzed. Experiments show that the speed of solving the horse traversal problem is significantly improved after the above greedy strategy is applied. If you only need to find a solution, you can do it without backtracking, because there was no computer in the world when this algorithm was proposed, this method can be obtained by hand, and its efficiency can be imagined.

# Include <stdio. h> <br/> # include <string. h> <br/> # include <vector> <br/> # include <algorithm> <br/> using namespace STD; <br/> # define N 8 // modify the size of the Board </P> <p> struct point {<br/> int X, Y; // X is horizontal, Y <br/> point () {}< br/> point (int x, int y): x (x), y (y) {}< br/>}; </P> <p> struct pridir {<br/> int Dir, outport; <br/> pridir (INT Dir, int outport ): dir (DIR), outport (outport) {}< br/> bool operator <(const pridir RHs) Co NST {<br/> return outport <RHS. outport; <br/>}< br/>}; </P> <p> int dir [8] [2] = {1,-2}, {2, -1}, {-2,-1}, {-1,-2 }}; <br/> int step [N] [N]; <br/> point start; </P> <p> void show (int A [] [N], int N) {<br/> for (INT I = 0; I <n; ++ I) {<br/> for (Int J = 0; j <N; + + J) {<br/> if (a [I] [J]! =-1) printf ("% 3d", a [I] [J]); <br/> else printf ("*"); <br/>}< br/> printf ("/N "); <br/>}</P> <p> inline int inchess (int x, int y) {<br/> return 0 <= x & x <n & 0 <= Y & Y <n; <br/>}</P> <p> int countoutport (point S) {<br/> int ret = 0; <br/> for (INT I = 0; I <8; ++ I) {<br/> int ny = S. Y + dir [I] [0]; <br/> int Nx = S. X + dir [I] [1]; <br/> If (inchess (NX, NY) & step [NX] [NY] =-1) {<br/> + ret; <br/>}< br/ >}< Br/> return ret; <br/>}</P> <p> int endflag; <br/> void travel (point S, int D) {<br/> step [S. x] [S. y] = D; <br/> vector <pridir> nextponits; <br/> // show (step, n); System ("pause "); </P> <p> If (D> = N * N-1) {<br/> show (step, n); <br/> system ("pause "); <br/> endflag = 1; <br/>}< br/> If (endflag) {<br/> return; <br/>}< br/> // count the number of "exits" of each extension node <br/> for (INT I = 0; I <8; ++ I) {<br/> int ny = S. Y + dir [I] [0]; <br/> in T Nx = S. x + dir [I] [1]; <br/> If (! Endflag & inchess (NX, NY) & step [NX] [NY] =-1) {<br/> int outport = countoutport (point (NX, NY); <br/> If (outport> 0 | D + 1 = N * N-1) {// when "exit" or the last element exists <br/> nextponits. push_back (pridir (I, outport )); <br/>}< br/> // sort by "exit" from small to large <br/> sort (nextponits. begin (), nextponits. end (); <br/> // brute force recursion <br/> for (INT I = 0; I <nextponits. size (); ++ I) {<br/> int nextdir = nextponits [I]. dir; <br /> Int ny = S. Y + dir [nextdir] [0]; <br/> int Nx = S. X + dir [nextdir] [1]; <br/> If (! Endflag & inchess (NX, NY) & step [NX] [NY] =-1) {<br/> travel (point (NX, NY ), d + 1); <br/>}</P> <p> step [S. x] [S. y] =-1; <br/>}</P> <p> int main () {<br/> scanf ("% d", & start. x, & start. y); <br/> memset (step,-1, sizeof (STEP); </P> <p> endflag = 0; <br/> travel (start, 0); </P> <p> return 0; <br/>}

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.