Advertisement
farhansadaf

p227

Apr 13th, 2021 (edited)
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. // Source: https: //www.life2coding.com/uva-online-solution-227-puzzle-problem/
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. char board[5][5];
  8. pair<int, int> blank; // blank cell -> row, col
  9.  
  10. void moveBlank(int i, int j)
  11. {
  12.     /*
  13.     Swaps blank with i'th row, j'th column of board.
  14.     e.g.
  15.     board = [
  16.         ['A', 'B', 'C', 'D', 'E'],
  17.         ['F', 'G', 'H', 'I', 'J'],
  18.         ['K', 'L', ' ', 'M', 'N'],      // blank = (2, 2)
  19.         ['O', 'P', 'Q', 'R', 'S'],
  20.         ['T', 'U', 'V', 'W', 'X']
  21.     ]
  22.  
  23.     Say, moveBlank(i=2, j=3) is called; that will result in
  24.  
  25.     board = [
  26.         ['A', 'B', 'C', 'D', 'E'],
  27.         ['F', 'G', 'H', 'I', 'J'],
  28.         ['K', 'L', 'M', ' ', 'N'],      // blank = (2, 3)
  29.         ['O', 'P', 'Q', 'R', 'S'],
  30.         ['T', 'U', 'V', 'W', 'X']
  31.     ]
  32.     */
  33.     board[blank.first][blank.second] = board[i][j];
  34.     board[i][j] = ' ';
  35.     blank.first = i, blank.second = j;
  36. }
  37.  
  38. int main()
  39. {
  40.     char c;
  41.     string input;
  42.     int T = 0; // Test case | board no.
  43.  
  44.     while (getline(cin, input))
  45.     {
  46.         /* Taking input of 'board' and 'blank' */
  47.         if (input == "Z")
  48.             break;
  49.  
  50.         if (T > 0)
  51.             cout << endl; // Print \n after printing each output board
  52.         T++;
  53.  
  54.         // Input for 1st line of the board
  55.         for (int j = 0; j < 5; j++)
  56.             board[0][j] = input[j];
  57.  
  58.         // Input for remaining 4 lines of the board
  59.         for (int i = 0; i < 4; i++)
  60.         {
  61.             getline(cin, input);
  62.             for (int j = 0; j < 5; j++)
  63.                 board[i + 1][j] = input[j];
  64.         }
  65.  
  66.         // Find out where blank (' ') is (row, col)
  67.         for (int i = 0; i < 5; i++)
  68.         {
  69.             for (int j = 0; j < 5; j++)
  70.             {
  71.                 if (board[i][j] == ' ')
  72.                     blank.first = i, blank.second = j;
  73.             }
  74.         }
  75.  
  76.         /* Taking input sequence of moves (A, B, R, L) */
  77.         bool illegal = false;
  78.         while (cin >> c)
  79.         {
  80.             if (c == '0') // Take character input till '0' arrives
  81.                 break;
  82.  
  83.             if (illegal)
  84.                 continue;
  85.  
  86.             switch (c)
  87.             {
  88.             case 'A': // Swap Above                          
  89.                 if (blank.first > 0) // When moveBlank is called for Above, blank.first will decrease by 1. So if blank.first= = 0 currently, that will cause in am illegal move.
  90.                     moveBlank(blank.first - 1, blank.second); // Swap blank (i, j) with (i-1, j); Same column, row change; Similar operations for Below, Left, Right
  91.                 else
  92.                     illegal = true;
  93.                 break;
  94.  
  95.             case 'B': // Swap below
  96.                 if (blank.first < 4)
  97.                     moveBlank(blank.first + 1, blank.second);
  98.                 else
  99.                     illegal = true;
  100.                 break;
  101.  
  102.             case 'L': // Swap Left
  103.                 if (blank.second > 0)
  104.                     moveBlank(blank.first, blank.second - 1);
  105.                 else
  106.                     illegal = true;
  107.                 break;
  108.  
  109.             case 'R': // Swap Right
  110.                 if (blank.second < 4)
  111.                     moveBlank(blank.first, blank.second + 1);
  112.                 else
  113.                     illegal = true;
  114.                 break;
  115.             }
  116.         }
  117.  
  118.         // Ignore or clear input buffer for next input board
  119.         cin.ignore();
  120.  
  121.         /* Print the result */
  122.         cout << "Puzzle #" << T << ":" << endl;
  123.  
  124.         if (illegal)
  125.         {
  126.             cout << "This puzzle has no final configuration." << endl;
  127.             continue;
  128.         }
  129.  
  130.         for (int i = 0; i < 5; i++)
  131.         {
  132.             for (int j = 0; j < 5; j++)
  133.             {
  134.                 if (j > 0) cout << " ";     // Don't print ' ' after last one.
  135.                 cout << board[i][j];
  136.             }
  137.             cout << endl;
  138.         }
  139.     }
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement