米哈游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();

}

#第三题 滑动获取宝箱最小次数 只通过了4.74% 估计还是返回-1的时候过的  有大佬帮忙看看吗 测试用例是过了的 不知道啥问题
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);
}



#米哈游笔试#
全部评论
第三题我bfs A的,话说没几个人笔试吗。。。
点赞
送花
回复
分享
发布于 2022-09-04 22:18 广东
第二题不用栈,在每次遇到s的时候,结果加上前面f和w数量最小值,更新f和w的数量为0就可以了
点赞
送花
回复
分享
发布于 2022-09-04 23:21 江苏
秋招专场
校招火热招聘中
官网直投
hi~同学,秋招遇“寒气”,牛客送温暖啦!23届秋招笔面经有奖征集中,参与就得牛客会员7天免费体验,最高赢300元京东卡!戳我去看>>>https://www.nowcoder.com/link/zhengjipinglun
点赞
送花
回复
分享
发布于 2022-09-05 12:36 北京
楼主收到感谢信了吗
点赞
送花
回复
分享
发布于 2022-09-05 21:01 广东
3A 了 但是前面选择做的有点麻
点赞
送花
回复
分享
发布于 2022-09-08 22:16 四川
米子是ACM模式还是核心模式
点赞
送花
回复
分享
发布于 2022-09-09 15:19 四川

相关推荐

2 2 评论
分享
牛客网
牛客企业服务