Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <Windows.h>
  3. #include <stdio.h>
  4.  
  5. static int N = 5;
  6. static int** Board = NULL;
  7.  
  8. #define Abs(a) (a < 0 ? -a : a)
  9.  
  10. bool BeenThere()
  11. {
  12.     for (int i = 0; i < N; i++)
  13.     {
  14.         for (int j = 0; j < N; j++)
  15.         {
  16.             if (Board[i][j] == 0)
  17.                 return false;
  18.         }
  19.     }
  20.  
  21.     return true;
  22. }
  23.  
  24. bool CanDo_Knight(int Row, int Column, int Count)
  25. {
  26.     if ((Row >= 0 && Row < N) && (Column >= 0 && Column < N))
  27.     {
  28.         if (Board[Row][Column] == 1) //Been there, done that...
  29.         {
  30.             return false;
  31.         }
  32.         else
  33.         {
  34.             Board[Row][Column] = 1;
  35.            
  36.             if (Count == N * N)
  37.                 return true;
  38.  
  39.             for (int r = -2; r <= 2; r++)
  40.             {
  41.                 if (r != 0)
  42.                 {
  43.                     for (int c = -2; c <= 2; c++)
  44.                     {
  45.                         if (c != 0)
  46.                         {
  47.                             if (Abs(r) != Abs(c))
  48.                             {
  49.                                 if (CanDo_Knight(Row + r, Column + c, Count + 1))
  50.                                 {
  51.                                     printf("{ %d, %d", Row + r + 1, Column + c + 1);
  52.                                     if (Count > 1)
  53.                                         printf(" }, ");
  54.                                     else
  55.                                         printf(" }\n");
  56.  
  57.                                     return true;
  58.                                 }
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             Board[Row][Column] = 0;
  66.             return false;
  67.         }
  68.     }
  69.     else
  70.     {
  71.         return false;
  72.     }
  73. }
  74.  
  75. void PrintStartingPoint_Knight()
  76. {
  77.     int TotalTime = GetTickCount();
  78.  
  79.     int i = 0;
  80.     int j = 0;
  81.     for (int i = 0; i < N; i++)
  82.     {
  83.         for (int j = 0; j < N; j++)
  84.         {
  85.             for (int k = 0; k < N; k++)
  86.                 ZeroMemory(Board[k], N * sizeof(Board[i]));
  87.  
  88.             int StartingTime = GetTickCount();
  89.  
  90.             printf("%d, %d:\n", i, j);
  91.  
  92.             if (CanDo_Knight(i, j, 1))
  93.                 printf("Success!!!");
  94.             else
  95.                 printf("Failure\t");
  96.             printf("\t(%f seconds)\n\n\n", (GetTickCount() - StartingTime) / 1000.0f);
  97.         }
  98.     }
  99.  
  100.     printf("Done!\nTotal Time: %f seconds.\n", (GetTickCount() - TotalTime) / 1000.0f);
  101. }
  102.  
  103. void PrintBoard()
  104. {  
  105.     const char Outputs[2][4][6] = { { "%d", "%c", "  " }, { "%02d", "% 2c", "   " } };
  106.  
  107.     for (int row = -1; row < N; row++)
  108.     {
  109.         if (row != -1)
  110.         {
  111.             printf(Outputs[N >= 10][0], row + 1);
  112.  
  113.             putchar('|');
  114.         }
  115.         else
  116.         {
  117.             printf(Outputs[N >= 10][2]);
  118.         }
  119.  
  120.         for (int col = 0; col < N - 1; col++)
  121.         {
  122.             if (row == -1)
  123.                 printf(Outputs[N >= 10][0], col + 1);
  124.             else
  125.                 printf(Outputs[N >= 10][1], Board[row][col]);
  126.  
  127.             putchar('|');
  128.         }
  129.  
  130.         if (row == -1)
  131.             printf(Outputs[N >= 10][0], N);
  132.         else
  133.             printf(Outputs[N >= 10][1], Board[row][N - 1]);
  134.  
  135.         putchar('\n');
  136.     }
  137. }
  138.  
  139. void SetQueen(int Row, int Column, int bSet)
  140. {
  141.     for (int distance = 1; distance < N; distance++)
  142.     {
  143.         for (int row = -1; row <= 1; row++)
  144.         {
  145.             for (int col = -1; col <= 1; col++)
  146.             {
  147.                 int r = Row + row * distance;
  148.                 int c = Column + col * distance;
  149.  
  150.                 if ((r >= 0 && r < N) && (c >= 0 && c < N))
  151.                     Board[r][c] += (bSet ? 1 : -1);
  152.             }
  153.         }
  154.     }
  155.  
  156.     if (bSet)
  157.         Board[Row][Column]++;
  158.     else
  159.         Board[Row][Column] = 0;
  160. }
  161.  
  162. bool CanDo_Queen(int Row = 0)
  163. {
  164.     if (Row >= N)
  165.         return (Row == N);
  166.  
  167.     for (int Column = 0; Column < N; Column++)
  168.     {
  169.         if (Board[Row][Column] == 0)
  170.         {
  171.             SetQueen(Row, Column, 1);
  172.             bool bRet = CanDo_Queen(Row + 1);
  173.             SetQueen(Row, Column, 0);
  174.  
  175.             if (bRet)
  176.             {
  177.                 Board[Row][Column] = 'Q';
  178.  
  179.                 printf("{ %d, %d }", Row + 1, Column + 1);
  180.                
  181.                 if (Row)
  182.                     printf(", ");
  183.                 else
  184.                     printf("\n");
  185.  
  186.                 return true;
  187.             }
  188.         }
  189.     }
  190.  
  191.     return false;
  192. }
  193.  
  194. void PrintStartingPoint_Queen()
  195. {
  196.     int StartingTime = GetTickCount();
  197.  
  198.     if (CanDo_Queen())
  199.     {
  200.         printf("Success!!!!!!!!!!!\n--------------------\n");
  201.         PrintBoard();
  202.     }
  203.     else
  204.     {
  205.         printf("Failure");
  206.     }
  207.  
  208.     printf("\nDone!\nTime: %f seconds.\n", (GetTickCount() - StartingTime) / 1000.0f);
  209. }
  210.  
  211. int main()
  212. {
  213.     int bLoop = 1;
  214.     int Choice;
  215.  
  216.     do
  217.     {
  218.         printf( "1. Knight's Tour\n"
  219.                 "2. Queens Puzzle\n"
  220.                 "3. Exit\nEnter your choice: ");
  221.         do
  222.         {
  223.             bLoop = scanf_s("%d", &Choice);
  224.  
  225.             if (Choice == 3)
  226.                 return 1;
  227.  
  228.             if (!bLoop)
  229.                 printf("Invalid option!\n");
  230.         }
  231.         while (!bLoop);
  232.  
  233.         if (Board)
  234.         {
  235.             for (int i = 0; i < N; i++)
  236.                 delete Board[i];
  237.  
  238.             delete[] Board;
  239.         }
  240.  
  241.         do
  242.         {
  243.             printf("Enter size: ");
  244.             bLoop = scanf_s("%d", &N);
  245.  
  246.             if (!bLoop)
  247.                 printf("Enter a valid integer!\n");
  248.             else if (N < 4 || N >= 100)
  249.                 printf("Size must be at least 4 and less than 100!\n");
  250.  
  251.         } while (N < 4 || N >= 100);
  252.  
  253.         Board = (int**)new int[N];
  254.         for (int i = 0; i < N; i++)
  255.         {
  256.             Board[i] = new int[N];
  257.             ZeroMemory(Board[i], N * sizeof(Board[i]));
  258.         }
  259.  
  260.         if (Choice == 1)
  261.         {
  262.             printf( "\n---------------------------------------------\n"
  263.                     "\n\t\tKnight's Tour\n"
  264.                     "\n---------------------------------------------\n");
  265.  
  266.             PrintStartingPoint_Knight();
  267.         }
  268.         else if (Choice == 2)
  269.         {
  270.             printf( "\n---------------------------------------------\n"
  271.                     "\n\t\tQueens Puzzle\n"
  272.                     "\n---------------------------------------------\n");
  273.  
  274.             PrintStartingPoint_Queen();
  275.         }
  276.     } while (bLoop);
  277.  
  278.     return 0;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement