Calculates the total number of loops from the bottom corner of the Board to the top right corner.

Source: Internet
Author: User
Tags stack pop


# Include "stdio. h"
# Define N 8 // because the calculated data is too large, it takes a long time to calculate it. You can change the value of N for testing.
# Include "iostream. h" // This algorithm adopts the Backtracking Method.

Enum bin {fal, tr}; // if there is a better algorithm, please send me an email. Thank you.
Int top = 0;
Long int num = 0;
Int row [] = {-1,-2,-2,-, 1 };
Int col [] = {-2,-, 1,-1,-2 };
Bin mark [N] [N];

Struct stack
{
Int x, y;
Int dir;} board [N * N];

Void push (stack it)
{
Board [top]. x = it. x;
Board [top]. y = it. y;
Board [top]. dir = it. dir;
Mark [board [top]. x] [board [top]. y] = tr;
Top ++;
}

Stack pop ()
{
-- Top;
Mark [board [top]. x] [board [top]. y] = fal;
Board [top]. dir ++;
Return board [top];
}

Bin empty ()
{
If (top = 0) return tr;
Else return fal;
}

Void main ()
{
Stack temp = {N-1, N-1,-1 };
Push (temp );
While (! Empty ())
{
Int g, h;
Temp = pop ();
Int I = temp. x;
Int j = temp. y;
Int dir = temp. dir;
While (dir <8)
{
G = I + row [dir];
H = j + col [dir];
If (g <0) | (h <0) | (g> = N) | (h> = N) | mark [g] [h]) dir ++;
Else {
If (g = 0 & h = 0) {num ++; dir ++ ;}
Else {
Temp. x = I;
Temp. y = j;
Temp. dir = dir;
Push (temp );
& N

Related Article

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.