题意&思路:
这题花里胡哨的,简化题意一句话:B到L最少的步数,R处不能走。
那么就只需要记录起点、终点位置,套BFS模板就行了。
Code
#include <bits/stdc++.h>
using namespace std;
char a[150][150];
long long v[150][150];
struct point{
long long x1,y1,step;
}start;
long long dx[4] = {1,0,-1,0};
long long dy[4] = {0,1,0,-1};
queue<point> dl;
int main(){
long long n,m;
for(long long i=1;i<=10;i++){
for(long long j=1;j<=10;j++){
cin >> a[i][j];
if(a[i][j]=='B'){
start.x1 = i;
start.y1 = j;
}
if(a[i][j]=='L'){
n=i;
m=j;
}
}
}
// for(int i=1; i<=10; i++)cout<<a[i]<<endl;
// cout<<n<<' '<<m<<endl;
start.step = 1;
v[start.x1][start.y1] = 1;
dl.push(start);
while(!dl.empty()){
point head = dl.front();
dl.pop();
if(head.x1 == n && head.y1 == m){
cout << head.step-2 << endl;
break;
}
// cout << head.x1 << ' ' << head.y1 << ' ' << head.step<<endl;
for(long long i=0;i<4;i++){
long long tx = head.x1 + dx[i];
long long ty = head.y1 + dy[i];
if(tx <= 10 && tx > 0 && ty <= 10 && ty > 0 && v[tx][ty]==0 && a[tx][ty]=='.' || a[tx][ty]=='L'){
v[tx][ty] = 1;
point temp;
temp.x1 = tx;
temp.y1 = ty;
temp.step = head.step + 1;
dl.push(temp);
}
}
}
return 0;
}