E. Crazy Robot(CF1613E)(搜索 + 思维)

1 篇文章 0 订阅

E. Crazy Robot(CF1613E)(搜索 + 思维)


题目来源:E. Crazy Robot(cf1613E)


题意:

给出一个地图, 有墙( # ), 有房间( . ), 还有一个实验室( L ), 机器人可能在任意一个房间, 每次让其移动时, 他总会向其他可移动的方向移动, 若其他方向移动不了, 他将停在原地不动.

判断当机器人在什么位置时, 无论机器人怎么移动都能移动到实验室, 将所有可能的位置从 ‘.’ 变成 ‘+’, 最后输出地图


思路:

每次从实验室往外搜索, 判断搜到的当前的点所能到的 ‘.’ ( ‘+’ 直接修改后的 )是否小于一个即可, 因为 ‘+’ 已经搜过了, 让机器人走到 ‘+’ 之后相当于一定能到实验室, 同时将每次搜到的合法点用vis标记


AC代码

#include <bits/stdc++.h>
#define endl "\n"
#define rep(i, m, n) for (int i = (m); i <= (n); ++i)
#define rrep(i, m, n) for (int i = (m); i >= (n); --i)
#define IOS ios::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10, mod = 1e9 + 7;
int n, m, x[N];
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
string s;
vector<string> v;
vector<int> vis[N];
bool fact(int x, int y) {
	int cou = 0;
	rep(i, 0, 3) {
		int tx = x + dx[i], ty = y + dy[i];
		if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
		if (v[tx][ty] == '.') cou++;
	}
	return cou < 2;
}
void bfs(int x, int y) {
	queue<PII> q; q.push({ x, y }); vis[x][y] = 1;
	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; ++i) {
			int tx = x + dx[i], ty = y + dy[i];
			if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
			if (vis[tx][ty]) continue;
			if (v[tx][ty] == '.') 
				if (fact(tx, ty)) {
					v[tx][ty] = '+';
					vis[tx][ty] = 1;
					q.push({ tx, ty });
				}
		}
	}
}
void solve() {
	scanf("%d %d", &n, &m);
	int x = -1, y = -1;
	rep(i, 0, n - 1) {
		vis[i].assign(m, 0); // 给vector开m个空间, 并赋值为0
		cin >> s; v.push_back(s);
		rep(j, 0, m - 1) 
			if (s[j] == 'L')
				x = i, y = j;
	}
 	if (x != -1) bfs(x, y);
	for (auto& op : v) cout << op << endl;
	rep(i, 0, n - 1) vis[i].clear();
	v.clear();
}
int main() {
	int t; cin >> t;
	while (t--) solve();
	return 0;
}

END

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值