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

E. Crazy Robot(CF1613E)(搜索 + 思维),第1张

E. Crazy Robot(CF1613E)(搜索 + 思维) E. Crazy Robot(CF1613E)(搜索 + 思维)
题目来源:E. Crazy Robot(cf1613E)
题意:

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

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


思路:

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


AC代码
#include 
#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 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 v;
vector 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 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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5635182.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存