题目
给定一个10x10的地图,B是仓库,L是湖泊,’.'是路,R是石头(石头不能走),现在仓库着火了,母牛哥要去救火了,求仓库到湖泊的最小步数(仓库和湖泊不算入步数)
解法
bfs模板题。。。。。
代码
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int _map[15][15];
bool vis[15][15];
int dir[4][2] = {1,0, 0,1, -1,0, 0,-1};
int sx, sy;
int gx, gy;
int main() {
queue <int> xx, yy, ans;
char ch;
for(int i=1; i<=10; ++i) {
for(int j=1; j<=10; ++j) {
cin >> ch;
if(ch=='B') {
sx = i;
sy = j;
xx.push(i);
yy.push(j);
ans.push(-1);
vis[i][j] = 1;
}
if(ch=='L') {
gx = i;
gy = j;
_map[i][j] = 1;
}
if(ch=='.') {
_map[i][j] = 1;
}
}
}
while(xx.size()) {
int x = xx.front();
int y = yy.front();
int t = ans.front();
if(x==gx && y==gy) {
cout << t;
break;
}
xx.pop();
yy.pop();
ans.pop();
for(int k=0; k<4; ++k) {
int tx = x + dir[k][0];
int ty = y + dir[k][1];
if(tx >0 && ty>0 && tx<=10 && ty<=10 && !vis[tx][ty] && _map[tx][ty]) {
xx.push(tx);
yy.push(ty);
vis[tx][ty] = 1;
ans.push(t+1);
}
}
}
return 0;
}