| | |
Knight's Tour Speedup.
![]() |
Hiya fellas,
I just made this Knight's Tour program, and wish to speed it up without making it non-brute force. So it shouldn't have any intelligence.
I did most I could, and was wondering if you guys know more ways to speed this program up. First thing I thought about is changing the recursive "solveit" to an iterative function, but will this really speed the program up, since it's maximum "call depth" is 64?
The code is attached as well as below. Any other comments are welcome as well.
Thanks in Advance,
Nick
PS:
Code is attached and below:
I just made this Knight's Tour program, and wish to speed it up without making it non-brute force. So it shouldn't have any intelligence.
I did most I could, and was wondering if you guys know more ways to speed this program up. First thing I thought about is changing the recursive "solveit" to an iterative function, but will this really speed the program up, since it's maximum "call depth" is 64?
The code is attached as well as below. Any other comments are welcome as well.
Thanks in Advance,
Nick
PS:
Code is attached and below:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #define NODEBUG #define TIMEELAPSED {printf("\tTime elapsed: %lu\n", GetTickCount() - startTime);} const int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2}; const int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; unsigned int boardsSolved = 0; unsigned char board[8*8] = {1}; unsigned int startTime; void printBoard(const unsigned char *board){ unsigned int i, j; for(i = 0; i < 8; i++){ for(j = 0; j < 8; j++){ printf("%02u ", board[i*8+j]); } putchar('\n'); } putchar('\n'); return; } int solveit(const unsigned int *knightPos, const unsigned int steps){ if(steps == 64){ printf("Board (%u) solved:\n", ++boardsSolved); printBoard(board); TIMEELAPSED return 0; } /*Algo: calc xpos if xpos is valid: calc ypos if ypos is valid: if board[x][y] is not stepped on set board[x][y] to steps call self with updated position and board */ unsigned int i; unsigned int curPos[2]; for(i = 0; i < 8; i++){ curPos[0] = knightPos[0] + horizontal[i]; if(curPos[0] > 7){ continue; } else{ curPos[1] = knightPos[1] + vertical[i]; if(curPos[1] > 7){ continue; } else{ if(board[curPos[0] + curPos[1]*8] == 0){ board[curPos[0] + curPos[1]*8] = steps+1; #ifdef DEBUG printf("Board after %u steps: \n", steps); printBoard(board); #endif solveit(curPos, steps+1); board[curPos[0] + curPos[1]*8] = 0; } else{ continue; } } } } #ifdef DEBUG else{ printf("All possibilities tried, returning.\n"); } #endif return -1; } int main(){ printf("Knights Tour Finder v0.1\n"); startTime = GetTickCount(); unsigned int startPos[2] = {0}; printf("Starting with this board: \n"); printBoard(board); solveit(startPos, 1); printf("Done"); return 0; }
That's not entirely correct I'm afraid.
I only display the board when it is solved, that's also when I calculate the difference in time.
Notice the #ifdef DEBUG in front of the "print the board after every move" line. That line only gets compiled when DEBUG is defined, which it isn't. It was there for, naturally, debugging purposes.
I only display the board when it is solved, that's also when I calculate the difference in time.
Notice the #ifdef DEBUG in front of the "print the board after every move" line. That line only gets compiled when DEBUG is defined, which it isn't. It was there for, naturally, debugging purposes.
Last edited by Clockowl; Jun 11th, 2008 at 5:35 pm.
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 7
•
•
•
•
That's not entirely correct I'm afraid.
I only display the board when it is solved, that's also when I calculate the difference in time.
Notice the #ifdef DEBUG in front of the "print the board after every move" line. That line only gets compiled when DEBUG is defined, which it isn't. It was there for, naturally, debugging purposes.
Have you profiled it yet, and what were the results?
What compiler are you using, and with what flags?
I'm sure you could google around and find other versions of this program. How does your program compare to them, when run on the same hardware and compiler?
That will give you real insight into the performance of your program.
Why does it matter what speeds I'm getting? I want it to be faster. You can't meassure this speed very well since it will "hang" on the same solution for quite a long time and then sometimes spawn 500 solutions in 2 seconds. It gets to 3498th solution in about 24 minutes on my system.
Open solutions are acceptable.
There's a good benchmark at solution nr. 823. It'll get there in about 70 seconds here and then hang for some time.
Open solutions are acceptable.
There's a good benchmark at solution nr. 823. It'll get there in about 70 seconds here and then hang for some time.
Last edited by Clockowl; Jun 12th, 2008 at 8:23 am.
•
•
•
•
Why does it matter what speeds I'm getting?
i want to know if its worth my time to post my solution. if you're getting speeds as fast as mine, i wont bother.
anyhow, the Warnsdorff algorithm, as ive coded it, solves each of the 64 positions on the board in 15 milliseconds or less. This is a 1.8 GHz machine.
mind you, this is just "solving" any given position. I could keep running it at that rate indefinitely, but if i had to keep track of unique solutions, it would add some overhead.
Last edited by jephthah; Jun 12th, 2008 at 11:51 am.
Sorry if that sounded hostile, I was honestly wondering why it mattered.
Are you saying that for any given position you find *all* the solutions in 15ms? Warnsdorff only finds a set, no? If so, please post your implementation so I can study it. Thank you.
Are you saying that for any given position you find *all* the solutions in 15ms? Warnsdorff only finds a set, no? If so, please post your implementation so I can study it. Thank you.
Last edited by Clockowl; Jun 12th, 2008 at 11:57 am.
i dont have my code available to me here at work, its on my laptop at home.
all i did was write code to implement the Warnsdorff Algorithm. its a couple hundred years old, and it's no secret.
you could implement it yourself and get the same results i did.
http://web.telia.com/~u85905224/knight/eWarnsd.htm
the pros of this method is that it's accurate (~98%) and its FAST. when i said 15 milliseconds or less, that was the maximum total time to find one solution per square, for ALL of the 64 squares in succession. so, approximately 250 microseconds per solution, or less. (and actually that was on my laptop, an AMD Athelon X2 (2.something GHz per core)
the real flaw with it is that it won't find all possible solutions, which may be unacceptable depending on your requirements. it can find multiple solutions per square, some closed and some open, but it wont find them all.
.
all i did was write code to implement the Warnsdorff Algorithm. its a couple hundred years old, and it's no secret.
you could implement it yourself and get the same results i did.
http://web.telia.com/~u85905224/knight/eWarnsd.htm
the pros of this method is that it's accurate (~98%) and its FAST. when i said 15 milliseconds or less, that was the maximum total time to find one solution per square, for ALL of the 64 squares in succession. so, approximately 250 microseconds per solution, or less. (and actually that was on my laptop, an AMD Athelon X2 (2.something GHz per core)
the real flaw with it is that it won't find all possible solutions, which may be unacceptable depending on your requirements. it can find multiple solutions per square, some closed and some open, but it wont find them all.
.
Last edited by jephthah; Jun 12th, 2008 at 1:50 pm.
![]() |
Other Threads in the C Forum
- Previous Thread: How to call the function including arrays of structure in main.c?
- Next Thread: Macro for dynamically allocate multidimensional arrays
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






