cout_jerry @ 2022-08-20 22:10:20
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int mmap[1001][1001],n,zh[1000001][4],head=1,tail=1,x,y,xx,yy;
string a;
int bfs(int x,int y,int xx,int yy)
{
for(;;)
{
if(x==xx&&y==yy)
{
// cout<<"c:"<<zh[head][0]<<" "<<zh[head][1]<<" "
return zh[head][2];
}
if(mmap[x+1][y]==1)
{
zh[tail][0]=x+1;
zh[tail][1]=y;
zh[tail][2]=zh[head-1][2]+1;
tail++;
// cout<<"a:"<<x+1<<" "<<y<<" "<<zh[head-1][2]+1<<endl;
}
if(mmap[x][y+1]==1)
{
zh[tail][0]=x;
zh[tail][1]=y+1;
zh[tail][2]=zh[head-1][2]+1;
tail++;
// cout<<"a:"<<x<<" "<<y+1<<" "<<zh[head-1][2]+1<<endl;
}
if(mmap[x-1][y]==1)
{
zh[tail][0]=x-1;
zh[tail][1]=y;
zh[tail][2]=zh[head-1][2]+1;
tail++;
// cout<<"a:"<<x-1<<" "<<y<<" "<<zh[head-1][2]+1<<endl;
}
if(mmap[x][y-1]==1)
{
zh[tail][0]=x;
zh[tail][1]=y-1;
zh[tail][2]=zh[head-1][2]+1;
tail++;
// cout<<"a:"<<x<<" "<<y-1<<" "<<zh[head-1][2]+1<<endl;
}
x=zh[head][0];
y=zh[head][1];
head++;
// cout<<"b:"<<x<<" "<<y<<endl;
}
}
int main(){
std::ios::sync_with_stdio(false);//去同步优化
cin.tie(0);//缓存优化
cout.tie(0);//缓存优化
cin>>n;
for(register int i(1);i<=n;i++)
{
cin>>a;
for(int j(1);j<=n;j++)
{
mmap[i][j]=a[j-1]-47;
}
}
cin>>x>>y>>xx>>yy;
cout<<bfs(x,y,xx,yy);
return 0;
}
//for (register int i(1); i <= n; ++ i)循环加速
//register int 变量定义优化
//inline 函数优化
//cout<<111<<"\n";
本人已自闭,望大佬来调
by cout_jerry @ 2022-08-20 22:35:08
@Hoshino_kaede 我这个有判断,再录入是路录入成了1,店铺变2,边界为0,而只有为1时才会搜索,要不然样例都过不了