#include <iostream> #include <vector> #include <string> using namespace std; vector<vector<char>> solve_puzzle(int n, int m, vector<string> initial_state, int k, string moves) { vector<vector<char>> puzzle(n, vector<char>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { puzzle[i][j] = initial_state[i][j]; } } pair<int, int> b_pos; pair<int, int> c_pos; int move = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (puzzle[i][j] == 'B') { b_pos = { i, j }; } else if (puzzle[i][j] == 'C') { c_pos = { i, j }; } } } for (char move : moves) { if (move == 'G') { b_pos.first != 0 ? b_pos.first-- : b_pos.first; c_pos.first != 0 ? c_pos.first-- : c_pos.first; } else if (move == 'D') { b_pos.first != puzzle.size() - 1 ? b_pos.first++ : b_pos.first; c_pos.first != puzzle.size() - 1 ? c_pos.first++ : c_pos.first; } else if (move == 'L') { b_pos.second != puzzle[0].size() - 1 ? b_pos.second-- : b_pos.second; c_pos.second != puzzle[0].size() - 1 ? c_pos.second-- : c_pos.second; } else if (move == 'P') { b_pos.second != puzzle[0].size() - 1 ? b_pos.second++ : b_pos.second; c_pos.second != puzzle[0].size() - 1 ? c_pos.second++ : c_pos.second; } } puzzle[b_pos.first][b_pos.second] = 'B'; puzzle[c_pos.first][c_pos.second] = 'C'; return puzzle; } int main() { int n, m; cin >> n >> m; vector<string> initial_state(n); for (int i = 0; i < n; i++) { cin>>initial_state[i]; } int k; cin >> k; string moves; cin >> moves; auto final_state = solve_puzzle(n, m, initial_state, k, moves); for (const auto& row : final_state) { for (char c : row) { cout << c; } cout << endl; } return 0; }
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 | #include <iostream> #include <vector> #include <string> using namespace std; vector<vector<char>> solve_puzzle(int n, int m, vector<string> initial_state, int k, string moves) { vector<vector<char>> puzzle(n, vector<char>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { puzzle[i][j] = initial_state[i][j]; } } pair<int, int> b_pos; pair<int, int> c_pos; int move = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (puzzle[i][j] == 'B') { b_pos = { i, j }; } else if (puzzle[i][j] == 'C') { c_pos = { i, j }; } } } for (char move : moves) { if (move == 'G') { b_pos.first != 0 ? b_pos.first-- : b_pos.first; c_pos.first != 0 ? c_pos.first-- : c_pos.first; } else if (move == 'D') { b_pos.first != puzzle.size() - 1 ? b_pos.first++ : b_pos.first; c_pos.first != puzzle.size() - 1 ? c_pos.first++ : c_pos.first; } else if (move == 'L') { b_pos.second != puzzle[0].size() - 1 ? b_pos.second-- : b_pos.second; c_pos.second != puzzle[0].size() - 1 ? c_pos.second-- : c_pos.second; } else if (move == 'P') { b_pos.second != puzzle[0].size() - 1 ? b_pos.second++ : b_pos.second; c_pos.second != puzzle[0].size() - 1 ? c_pos.second++ : c_pos.second; } } puzzle[b_pos.first][b_pos.second] = 'B'; puzzle[c_pos.first][c_pos.second] = 'C'; return puzzle; } int main() { int n, m; cin >> n >> m; vector<string> initial_state(n); for (int i = 0; i < n; i++) { cin>>initial_state[i]; } int k; cin >> k; string moves; cin >> moves; auto final_state = solve_puzzle(n, m, initial_state, k, moves); for (const auto& row : final_state) { for (char c : row) { cout << c; } cout << endl; } return 0; } |