1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <bits/stdc++.h> using namespace std;
void moveChange(int after_row, int after_col, int before_row, int before_col, array<array<char, 5>, 5> &puzzle) { char temp = puzzle[after_row][after_col]; puzzle[after_row][after_col] = puzzle[before_row][before_col]; puzzle[before_row][before_col] = temp; }
bool puzzleMove(char &c, pair<int, int> &empty_pos, array<array<char, 5>, 5> &puzzle) { int *row = &empty_pos.first, *col = &empty_pos.second;
switch (c) { case 'A': if(*row > 0) { moveChange(*row, *col, *row - 1, *col, puzzle); *row -= 1; } else { return false; } break;
case 'B': if(*row < 4) { moveChange(*row, *col, *row + 1, *col, puzzle); *row += 1; } else { return false; } break;
case 'L': if(*col > 0) { moveChange(*row, *col, *row, *col - 1, puzzle); *col -= 1; } else { return false; } break;
case 'R': if(*col < 4) { moveChange(*row, *col, *row, *col + 1, puzzle); *col += 1; } else { return false; } break;
default: return false; }
return true; }
int main(){ ios::sync_with_stdio(0);cin.tie(0);
bool exit_end = false; int n = 1; int num = 3; while(!exit_end){ array<array<char, 5>, 5> puzzle; vector<char> moves; pair<int, int> empty_pos;
for(int i = 0; i < 5; i++){ string line; getline(cin, line);
if(line[0] == 'Z') { exit_end = true; break; }
for(int j = 0; j < 5; j++) { puzzle[i][j] = line[j];
if(puzzle[i][j] == ' ' || puzzle[i][j] == '\0') { empty_pos = make_pair(i, j); } } }
if(exit_end) break;
char move; while (cin >> move && move != '0') { moves.push_back(move); } cin.ignore();
bool illegal = true; for(char c: moves){ illegal = puzzleMove(c, empty_pos, puzzle); if(illegal == false) break; }
if(n > 1) cout << endl; cout << "Puzzle #" << n << ":" << endl; if(!illegal) { cout << "This puzzle has no final configuration." << endl; } else { for(int i = 0; i < 5; i++){ for(int j = 0; j < 5; j++) { if (j > 0) cout << " ";
puzzle[i][j] ? cout << puzzle[i][j] : cout << " "; } cout << endl; } } n++; } }
|