同样的 code
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,a[1010][1010],used[1010][1010],st1,st2,ed1,ed2;
int dir[5][2]={{0},{0,1},{1,0},{-1,0},{0,-1}};
char c;
struct node{
int x,y;
int bs;
};
int bfs(int x,int y)
{
memset(used,0,sizeof(used));
node st,h,tmp;
st.x=x;
st.y=y;
queue<node> q;
q.push(st);
while(!q.empty())
{
h=q.front();
q.pop();
if(h.x==ed1&&h.y==ed2)
return h.bs;
for(int i=1;i<=4;i++)
{
tmp.x=h.x+dir[i][0],tmp.y=h.y+dir[i][1];
if(tmp.x>=1&&tmp.x<=n&&tmp.y>=1&&tmp.y<=n&&!used[tmp.x][tmp.y]&&!a[tmp.x][tmp.y])
{
tmp.bs = h.bs+1;
q.push(tmp);
used[tmp.x][tmp.y] = 1;
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>c;
a[i][j]=c-'0';
}
scanf("%d%d%d%d",&st1,&st2,&ed1,&ed2);
cout<<bfs(st1,st2);
return 0;
}
同一份数据
32
01100100100011010000001100000000
01000000110010001000000110000010
00101000000110000000000001001010
00000010010100000011101000101000
00100001000000101010100100010111
10000110000001010010110101111010
00001010010100000100000001101000
01001100011000000001000000000011
01000101010110001011001000011011
11000001001000000101100000100110
00111100110100001000111000010001
00010000001100001100001010000111
00110001110000000010001001000100
10100010000100111100011000000001
00001011001001100110001001101010
11010101101001011010010000111110
00100010101010100001000010010000
00011000001010110000100110010000
00000011001110001110100000001110
00001001000100100010011000000000
10000000101010011100000000000010
00001000001101000000000010100100
10111100001001001000011000000000
10100110011010010000010010010100
10001111001000001011101001000010
00100010000000100001000100001100
01000100100000100100100000000011
01000010000010001001000101000110
11101100001010110010111010101100
10111000000000010001001000000001
01000000010001000000101011011101
11000001100000001100100010001000
1 2 23 2
为什么本地输出的是 50,而你谷 IDE 输出的是乱码?
或者说,怎么让它输出50?
这是用于测试的题面
现在有一个n*n的由0和1组成的矩阵,1表示不可以走的地方,0表示可以走的地方,给定你起始点(x1,y1)和终点(x2,y2),要求你只能垂直或水平前进,需要你求出最短到达终点的距离。
输入描述
第1行:一个数 n
第2行~第n+1行:整个矩阵描述(0表示通路,1表示不通的路,注意两个数之间没有空格)
第n+2行:四个数 x1,y1,x2,y2
输出描述
只有1行:最短到达目的地距离