Share a fun algorithmic game

Source: Internet
Author: User

Introduction of the Author:

Childhood is not a diligent child, the general thing is to maintain the middle level. The spirit of the matter is not poor, so I prefer to listen to others to share things, so as to discover the merits of others.

If the three minute heat to describe me is more appropriate, I will do what should be done as for the other? Look at the mood!

I envy those who have thought and act, in my opinion they are very ' business ', of course, I will slowly develop in that direction, although it takes time to precipitate (haha)

As a lot of programs ape in the sister paper, I decided to play a stage name for myself, no, pen name! In order to show the different personalities of our team, I also suggest that everyone write their own stage name when writing!

Then the problem came, browse Iteye when you see the problem, by the way to the side of the study caught dead ...

0x000

00000

00000

00000

00000

But X point, put all the rings together, can not be repeated, can not be inclined to connect, can not jump circle

The first step: I turn this diagram into a language that the program can read

One A from

21 22 23) 24 25

31 32 33) 34 35

41 42 43) 44 45

51 52 53) 54 55

Step two: Set up four events

First, each point has four directions to go, I set four directional events

are: Go up, go down, go left, go right

Private function Left ($x, $y, $OLDARR)

{

if ($y > 1)

{

if (false = = In_array ($x. $y-1, $OLDARR)//Determine if this point has passed

{

$OLDARR [] = $x. $y -1;//Record of the points passed

return $x. " -". $y -1;//return to the next point

}

}

return false;

}

Private function Right ($x, $y, $OLDARR)

{

if ($y < 5)

{

if (false = = In_array ($x. $y +1, $OLDARR)

{

$OLDARR [] = $x. $y +1;

return $x. " -". $y +1;

}

}

return false;

}

Private function Up ($x, $y, $OLDARR)

{

if ($x > 1)

{

if (false = = In_array ($x -1. $y, $OLDARR)

{

$OLDARR [] = $x -1. $y;

Return $x-1. " -". $y;

}

}

return false;

}

Private function Down ($x, $y, $OLDARR)

{

if ($x < 5)

{

if (false = = In_array ($x +1. $y, $OLDARR)

{

$OLDARR [] = $x +1. $y;//record past the point

return $x +1. " -". $y;

}

}

return false;

}

Step three: Start writing a test run method

When the starting point is fixed, start to select the direction, when One Direction can walk, go to the next point, and then continue to select the direction, cycle, until all the points go through.

Public Function Gotry ($x, $y, $OLDARR, $methods)

{

$OLDARR [] = $x. $y;

foreach ($methods as $method)

{

$next = $this $method ($x, $y, $OLDARR);//The possibility of looping four directions

if (false = = Empty ($next))//If this direction can go then continue, otherwise, go another direction

{

$xy = Explode ("-", $next);

if ($xy [0]! = 1 && $xy [1]! = 2)//12

{

Gotry ($xy [0], $xy [1]); Recursive loops

}

}

}

}

Fourth Step write Main method: Loop fetch call

Public Function agame ($request, $response)

{

$methods = Array (' Left ', ' right ', ' up ', ' down ');

for ($i =1; $i <=5; $i + +)

{

for ($j =1; $j <=5; $j + +)

{

Change the starting point for different

if ($i! = 1 && $j! = 2)

{

$OLDARR = Array ();

Gotry ($i, $j, $OLDARR, $methods);

if (count ($oldArr) = = 24)

{

Var_dump ($OLDARR); exit;

}

}

}

}

}


The above is my share of the process, the code may have a lot of bad places, but the writing algorithm is quite interesting thing. Like recursive method only heard is also the first real to use, some knowledge is useless, when you really need to estimate accidentally thought of

Because I know very little knowledge, I saw a little game in the morning, a little interest in the process of writing a blog to achieve the next ...

Last note my pen name---little dream.



Sync Blog: http://www.cnblogs.com/newbalanceteam/p/4835894.html

This article is from the "Hanyubin-Boulevard from Life" blog, please be sure to keep this source http://byteh.blog.51cto.com/141786/1725399

Share a fun algorithmic game

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.