求助
63分
剩下的点TLE
我加上了墨迹注释
下面是代码
谢谢大家
#include<bits/stdc++.h>
using namespace std;
int m,mp[310][310],ans=2e9;
bool flag=false,vis[310][310];
void search(int x,int y,int step)
{
if(x<0||y<0||x>305||y>305)
return;
if(vis[x][y]==true)
return;
if(mp[x][y]!=-1&&mp[x][y]<=step)
return;
if(mp[x][y]==-1)
{
ans=min(ans,step);
flag=true;
return;
}
vis[x][y]=true;
if(x+1>=0&&y>=0)
search(x+1,y,step+1);
if(x>=0&&y+1>=0)
search(x,y+1,step+1);
if(x-1>=0&&y>=0)
search(x-1,y,step+1);
if(x>=0&&y-1>=0)
search(x,y-1,step+1);
vis[x][y]=false;
}
int main(){
cin>>m;
memset(mp,-1,sizeof(mp));
for(int i=1;i<=m;i++)
{
int x,y,t;
cin>>x>>y>>t;
if(mp[x][y]!=-1)
mp[x][y]=min(mp[x][y],t);
else
mp[x][y]=t;
if(mp[x+1][y]!=-1)
mp[x+1][y]=min(mp[x+1][y],t);
else
mp[x+1][y]=t;
if(mp[x-1][y]!=-1)
mp[x-1][y]=min(mp[x-1][y],t);
else
mp[x-1][y]=t;
if(mp[x][y+1]!=-1)
mp[x][y+1]=min(mp[x][y+1],t);
else
mp[x][y+1]=t;
if(mp[x][y-1]!=-1)
mp[x][y-1]=min(mp[x][y-1],t);
else
mp[x][y-1]=t;
}
search(0,0,0);
if(flag==true)
{
cout<<ans<<endl;
}
else
{
cout<<-1<<endl;
}
return 0;
}