评价:有思想的搜索。
由于指令来自一个方向,机器人可以往三个方向移动,情况太多对我们不利。考虑怎么利用地图让机器人只能往一个方向移动。
当旁边有两个以内的 . 格点就可以控制它移动。当然若旁边有 +,且旁边有一个以内 .,可以直接移动到那里,然后按以前方法移到实验室,有 L 同理。
不枚举起点可以从实验室反向搜索。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, sx, sy;
const int dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1};
bool in(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= m;
}
void solve(){
cin >> n >> m;
vector< vector<char> > c(n + 2, vector<char>(m + 2, '#'));
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j){
cin >> c[i][j];
if(c[i][j] == 'L')
sx = i, sy = j;
}
queue< pair<int, int> > q;
q.push(make_pair(sx, sy));
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
for(int i = 0; i < 4; ++ i){
int nx = x + dx[i], ny = y + dy[i];
if(!in(nx, ny) || c[nx][ny] == '#' || c[nx][ny] == 'L' || c[nx][ny] == '+')
continue;
int s = 0, f = 0;
for(int j = 0; j < 4; ++ j){
int tx = nx + dx[j], ty = ny + dy[j];
if(in(tx, ty) && (c[tx][ty] == 'L' || c[tx][ty] == '+'))
f = 1;
if(in(tx, ty) && c[tx][ty] == '.')
++ s;
}
if((s <= 2 && !f) || (s <= 1 && f))
c[nx][ny] = '+', q.push(make_pair(nx, ny));
}
}
for(int i = 1; i <= n; ++ i){
for(int j = 1; j <= m; ++ j)
cout << c[i][j];
cout << "\n";
}
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;
cin >> t;
while(t --)
solve();
return 0;
}