fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 8
  6.  
  7. int main() {
  8. int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
  9. int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
  10. int tourLengths[65] = {0};
  11.  
  12. srand(time(NULL));
  13.  
  14. for (int tour = 0; tour < 1000; tour++) {
  15. int board[SIZE][SIZE] = {0};
  16. int currentRow = rand() % 8;
  17. int currentCol = rand() % 8;
  18. board[currentRow][currentCol] = 1;
  19. int moveNumber = 1;
  20. int canMove = 1;
  21.  
  22. while (canMove) {
  23. int validMoves[8] = {0};
  24. int numValidMoves = 0;
  25.  
  26. for (int moveType = 0; moveType < 8; moveType++) {
  27. int testRow = currentRow + vertical[moveType];
  28. int testCol = currentCol + horizontal[moveType];
  29.  
  30. if (testRow >= 0 && testRow < SIZE && testCol >= 0 && testCol < SIZE) {
  31. if (board[testRow][testCol] == 0) {
  32. validMoves[numValidMoves] = moveType;
  33. numValidMoves++;
  34. }
  35. }
  36. }
  37.  
  38. if (numValidMoves > 0) {
  39. int moveType = validMoves[rand() % numValidMoves];
  40. currentRow += vertical[moveType];
  41. currentCol += horizontal[moveType];
  42. moveNumber++;
  43. board[currentRow][currentCol] = moveNumber;
  44. } else {
  45. canMove = 0;
  46. }
  47. }
  48. tourLengths[moveNumber]++;
  49. }
  50.  
  51. printf("Tour Length\tFrequency\n");
  52. for (int i = 1; i <= 64; i++) {
  53. if (tourLengths[i] > 0) {
  54. printf("%d\t\t%d\n", i, tourLengths[i]);
  55. }
  56. }
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Tour Length	Frequency
4		1
5		1
6		2
8		4
9		1
10		4
11		3
12		6
13		5
14		6
15		12
16		8
17		7
18		13
19		12
20		9
21		8
22		25
23		20
24		22
25		13
26		36
27		23
28		23
29		44
30		40
31		26
32		30
33		37
34		41
35		41
36		44
37		35
38		40
39		40
40		34
41		33
42		32
43		26
44		35
45		34
46		23
47		23
48		14
49		16
50		11
51		11
52		9
53		7
54		7
55		1
56		1
57		1