题目描述
给定一个 n 行 m 列的地牢,其中 ‘.’ 表示可以通行的位置,‘X’ 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。
输入描述:
每个输入包含 1 个测试用例。每个测试用例的第一行包含两个整数 n 和 m(1 <= n, m <= 50),表示地牢的长和宽。接下来的 n 行,每行 m 个字符,描述地牢,地牢将至少包含两个 ‘.’。接下来的一行,包含两个整数 x0, y0,表示牛牛的出发位置(0 <= x0 < n, 0 <= y0 < m,左上角的坐标为 (0, 0),出发位置一定是 ‘.’)。之后的一行包含一个整数 k(0 < k <= 50)表示牛牛合法的步长数,接下来的 k 行,每行两个整数 dx, dy 表示每次可选择移动的行和列步长(-50 <= dx, dy <= 50)
输出描述:
输出一行一个数字表示最坏情况下需要多少次移动可以离开地牢,如果永远无法离开,输出 -1。以下测试用例中,牛牛可以上下左右移动,在所有可通行的位置.上,地牢出口如果被设置在右下角,牛牛想离开需要移动的次数最多,为3次。
示例1
输入
复制
3 3
…
…
…
0 1
4
1 0
0 1
-1 0
0 -1
输出
复制
3
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N=100;
struct node{
int x,y,step;
};
char g[N][N];
bool st[N][N];
int main() {
int n,m;
while (cin>>n>>m) {
memset(st,false,sizeof st);
//.表示 可以通过, X 不可以通过
for(int i=0;i<n;++i) for(int j=0;j<m;++j) cin>>g[i][j];
int x0,y0;cin>>x0>>y0;
int k;cin>>k;
vector<pair<int,int> >steps(k);
for(int i=0;i<k;++i) {
int x,y;cin>>x>>y;
steps[i]={x,y};
}
queue<node>q;
q.push({x0,y0,0});
st[x0][y0]=true;
int max_step=-1;
while (q.size()) {
auto pos = q.front();q.pop();
for (int i=0;i<k;++i) {
int nextx = pos.x + steps[i].first,nexty=pos.y+steps[i].second;
if(nextx>=0 && nextx<n && nexty>=0 && nexty<m && g[nextx][nexty]=='.' &&st[nextx][nexty]==false) {
//可以通过
q.push({nextx,nexty,pos.step+1});
st[nextx][nexty]=true;//判重
max_step = max(max_step, pos.step+1);
}
}
}
bool all_reach=true;
for (int i=0;i<n;++i) {
for (int j=0;j<m;++j) {
if (st[i][j]==false && g[i][j]=='.') {
all_reach=false;break;
}
}
}
if (all_reach) {
cout <<max_step<<endl;
}else puts("-1");
}
}