米哈游9.4 B卷
// 第一题 放英雄 100 void one() { int n = 0, m = 0, q = 0; cin >> n >> m >> q; int i = 0; vector<vector<string>> maps(n, vector<string>(m, "NULL")); //unordered_set<string> heros_set; int t = 0, x = 0, y = 0; string hero = "NULL"; while (i < q) { i++; cin >> t >> x >> y; if (t == 1) { cin >> hero; } if (x<0 || x>n || y<0 || y>m) { continue; } if (t == 1) { if (maps[x - 1][y - 1] == "NULL") { maps[x - 1][y - 1] = hero; } } else { if (maps[x - 1][y - 1] != "NULL") { maps[x - 1][y - 1] = "NULL"; } } } for (auto& row : maps) { bool flag = true; for (auto& name : row) { if (flag) { cout << name; flag = false; } else { cout << " "<<name; } } cout << endl; } }
# 第二题 消消乐 100
void two() { string input; cin >> input; if (input.size() <= 1) { cout << 0; return; } int sz = input.size(); stack<char> stk; for (int i = 0; i < sz; i++) { if (stk.empty()) { stk.push(input[i]); } else { if (stk.top() == 'f' && input[i] == 'w' || stk.top() == 'w' && input[i] == 'f') { stk.pop(); } else { stk.push(input[i]); } } } cout << sz - stk.size(); }
void dfs(vector<string>& maps, vector<int>& directions, unordered_map<string, int> &memo, long slide_nums,int i, int j,long &ans,int &n,int &m) { string key = to_string(i) + "," + to_string(j); if (memo[key]==1) { return; } memo[key] = 1; //cout << i + 1 << "," << j + 1 << "," << slide_nums << endl; int next_i = 0, next_j = 0; bool flag = false; for (int& direction : directions) { if (flag == true) { break; } flag = false; switch (direction) { case 1: next_i = 0; next_j = j; for (int ii = i - 1; ii >= 0; ii--) { if (maps[ii][j] == '#') { next_i = ii + 1; break; } if (maps[ii][j] == '@') { ans = min(ans, slide_nums + 1); flag = true; break; } } //cout << "向上:"; if(!flag) dfs(maps, directions, memo, slide_nums + 1, next_i, next_j, ans,n,m); break; case 2: next_i = i; next_j = m-1; for (int jj = j+1; jj < m; jj++) { if (maps[i][jj] == '#') { next_j = jj - 1; break; } if (maps[i][jj] == '@') { ans = min(ans, slide_nums + 1); flag = true; break; } } //cout << "向右:"; if (!flag) dfs(maps, directions, memo, slide_nums + 1, next_i, next_j, ans, n, m); break; case 3: next_i = n-1; next_j = j; for (int ii = i + 1; ii < n; ii++) { if (maps[ii][j] == '#') { next_i = ii - 1; break; } if (maps[ii][j] == '@') { ans = min(ans, slide_nums + 1); flag = true; break; } } //cout << "向下:"; if (!flag) dfs(maps, directions, memo, slide_nums + 1, next_i, next_j, ans, n, m); break; case 4: next_i = i; next_j = 0; for (int jj = j - 1; jj >=0; jj--) { if (maps[i][jj] == '#' ) { next_j = jj + 1; break; } if (maps[i][jj] == '@') { ans = min(ans, slide_nums + 1); flag = true; break; } } //cout << "向左:"; if (!flag) dfs(maps, directions, memo, slide_nums + 1, next_i, next_j, ans, n, m); break; default: break; } } memo[key] = 0; } void three() { int n = 0, m = 0; cin >> n >> m; vector<string> maps(n); int i = 0; unordered_map<string, int> memo; //set<string> path; while (i < n) { cin >> maps[i++]; } long ans = INT32_MAX; vector<int> directions = { 1,2,3,4 }; dfs(maps, directions, memo, 0, 0, 0, ans, n, m); cout << (ans == INT32_MAX ? -1 : ans); }