Stepping board Algorithm

Source: Internet
Author: User

# Include
 
  
# Include
  
   
Using namespace std; struct point {int x; int y ;}; stack
   
    
Sta; int map [8] [8]; int moved [8] [2] = {-,-, 2,-,-2, -1,-2,-2,-1}; void dfs (int x, int y) {int visited [8] [8]; memset (visited, 0, sizeof (visited); // 0 indicates that no point * p = new point has been accessed; p-> x = x; p-> y = y; point * temp; point * q; int count = 0; sta. push (p); visited [x] [y] = 1; int a, B; count ++; while (! Sta. empty () {q = sta. top (); if (count = 16) // break cannot be found in 64; int flag = 0; for (int I = 0; I <8; I ++) {a = q-> x + moved [I] [0]; B = q-> y + moved [I] [1]; if (a> = 0 & a <4 & B> 0 & B <4 &&(! Visited [a] [B]) {temp = new point; temp-> x = a; temp-> y = B; visited [a] [B] = 1; sta. push (temp); count ++; flag = 1; // indicates that the current process can continue without backtracking. // It is not bfs, so you need to jump out of break ;}} if (flag = 0) {// you cannot continue. You need to backtrack count --; // sta. pop (); // The memory may leak. temp = sta. top (); a = temp-> x; B = temp-> y; visited [a] [B] = 0; sta. pop (); delete temp ;}} int n = sta. size (); cout <
    
     
X; B = temp-> y; cout <> x> y; dfs (x, y); return 0 ;}
    
   
  
 

The matted chessboard algorithm is not tested because the time complexity is too high. It should be correct.

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.