Description
Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby asks you to help him figure out the minimum time it takes to reach the teaching building.
The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.
Input
The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the column
Next there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching building
Next is a map of n rows and m columns, 0 indicate a open space and 1 indicate a obstacles.**
Output
For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.
Sample Input
5 4
4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
Sample Output
7
数组模拟队列
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 1000000007
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false);
int next[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
int s[110][110];
int vis[110][110];
struct node{
int x, y, step;
}q[100010];
int main()
{
int n, m;
cin >> n >> m;//读入地图大小
int ex, ey;
cin >> ex >> ey;//读入终点
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) cin >> s[i][j];//读入地图
}
int head = 1;
int tail = 1;
q[tail].x = 1;//起点的横坐标进入队列
q[tail].y = 1;//起点的纵坐标进入队列
q[tail].step = 0;//初始化起点步长为0
vis[1][1] = 1;//将起点标记为走过
tail++;
int flag = 0;
while(head<tail){
for(int i = 0; i < 4; i++){//依次搜索四个方向
int nx = q[head].x+next[i][0];
int ny = q[head].y+next[i][1];
if(nx<1||nx>n||ny<1||ny>m) continue;//判断该点位置是否合法
if(s[nx][ny]==0&&vis[nx][ny]==0){//判断该点是否走过以及是否是障碍
vis[nx][ny] = 1;//将该点标记为走过
q[tail].x = nx;//该点的横坐标进入队列
q[tail].y = ny;//该点的纵坐标进入队列
q[tail].step = q[head].step+1;//记录步长
tail++;
}
if(nx==ex&&ny==ey){//判断是否到达终点
flag = 1;
break;
}
}
if(flag) break;
head++;//将队首元素移除队列
}
cout << q[tail-1].step << endl;
return 0;
}
队列做法
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define N 1000000007
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
int g[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int s[110][110];
int jud[110][110];
int n, m;
int sx, sy;
int ex, ey;
struct node{
int x, y;
int step;
};
int bfs()
{
queue<node>q;
node p;
p.x = 1;
p.y = 1;
p.step = 0;
q.push(p);
jud[1][1] = 1;
while(!q.empty()){
node t = q.front();
q.pop();
if(t.x==ex&&t.y==ey) return t.step;
for(int i = 0; i < 4; i++){
int xx = t.x+g[i][0];
int yy = t.y+g[i][1];
if(xx>0&&xx<=n&&yy>0&&yy<=m&&s[xx][yy]==0&&jud[xx][yy]==0){
jud[xx][yy] = 1;
node t1;
t1.x = xx;
t1.y = yy;
t1.step = t.step+1;
q.push(t1);
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> m;
cin >> ex >> ey;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) cin >> s[i][j];
}
int cnt = bfs();
cout << cnt << endl;
return 0;
}