样例4答案是3 我的代码输出2
#include<iostream>
#include<queue>
using namespace std;
int n,m,k,mynext[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int a[3010][3010],ans[3010][3010];
bool v[3010][3010],y[3010][3010];
struct type{
int x,y;
}t[9000010];
queue<type> q;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
void bfs()
{
v[1][1]=1;
q.push((type){1,1});
while(!q.empty())
{
type now=q.front();
q.pop();
if(now.x==n&&now.y==m) return ;
for(int i=0;i<4;i++)
{
int newx=now.x+mynext[i][0],newy=now.y+mynext[i][1];
if(newx>n||newx<1||newy>m||newy<1||a[newx][newy]==0||v[newx][newy]==1) continue;
v[newx][newy]=1;
ans[newx][newy]=ans[now.x][now.y]+1;
q.push(type{newx,newy});
}
if(y[now.x][now.y]==1)
{
for(int i=1;i<=k;i++)
{
if(t[i].x!=now.x&&t[i].x!=now.y)
{
if(a[t[i].x][t[i].y]!=0)
{
v[t[i].x][t[i].y]=1;
if(v[t[i].x][t[i].y]!=0) ans[t[i].x][t[i].y]=min(ans[now.x][now.y]+1,ans[t[i].x][t[i].y]);
else ans[t[i].x][t[i].y]=ans[now.x][now.y]+1;
q.push((type){t[i].x,t[i].y});
}
}
}
}
}
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]=read();
}
}
for(int i=1;i<=k;i++)
{
int x1,y1;
x1=read(),y1=read();
y[x1][y1]=1;
t[i]=(type){x1,y1};
}
bfs();
if(ans[n][m]!=0) cout<<ans[n][m];
else cout<<-1;
return 0;
}
悬赏1关注,lz可能只有晚上能看