I - 贪吃蛇
裸的BFS
首先把目前的每个点加入队列中
然后每个点四周可以走的步数 + 1 ,并且入队即可
#include<iostream>
#include<queue>
using namespace std;
struct G{
int x, y ,s;
};
queue<G> q;
char a[110][110] ;
int dx[] = {0,0,1,-1} , dy[] = {1,-1,0,0};
int main(){
int n , m ; cin >> n >> m;
int x1 ,y1 ,x2 ,y2; cin >> x1 >> y1 >> x2 >> y2 ;
getchar();
for(int i = 0 ; i < n ; i ++ ) cin >> a[i];
q.push({x1 - 1,y1 - 1,0});
a[x2 - 1][y2 - 1] = 'E';
while(q.size()){
G t = q.front();
if(a[t.x][t.y] == 'E') break;
q.pop();
a[t.x][t.y] = '#';
for(int i = 0 ; i < 4 ; i ++){
int x = t.x + dx[i] , y = t.y + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '#'){
q.push({x,y,t.s + 1});
}
}
}
if(q.empty()) printf("-1\n");
else printf("%d",q.front().s * 100);
return 0;
} 
京公网安备 11010502036488号