bfs
虽然可以重复到某个点
但是同一时间维度上不能到达同一个点(因为重复时间访问同一个点不可能得到最优解)
用vis[maxn][maxn][maxt] 来记录
#include<bits/stdc++.h>
using namespace std;
#define maxn 110
struct node
{
int tp;
int t1;
int t2;
node(int _tp=0,int _t1=0,int _t2=0):tp(_tp),t1(_t1),t2(_t2) {};
};
struct point
{
int x;
int y;
int h;
};
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
node mp[maxn][maxn];
bool vis[maxn][maxn][10010];
int n,m,t;
int bfs()
{
memset(vis,false,sizeof vis);
point st;
st.x=1;
st.y=1;
st.h=0;
vis[1][1][0]=true;
queue<point> q;
q.push(st);
point t;
while(!q.empty())
{
t=q.front();
q.pop();
int sx=t.x;
int sy=t.y;
int h=t.h;
for(int i=0;i<4;i++)
{
int ex=sx+dx[i];
int ey=sy+dy[i];
int eh=h+1;
if(!vis[ex][ey][eh]&&ex>=1&&ex<=n&&ey>=1&&ey<=m)
{
if(mp[ex][ey].tp==1)
{
int t1=mp[ex][ey].t1;
int t2=mp[ex][ey].t2;
if(eh>=t1&&eh<=t2)
continue;
}
if(ex==n&&ey==m)
{
return eh;
}
point tmp;
tmp.x=ex;
tmp.y=ey;
tmp.h=eh;
q.push(tmp);
vis[ex][ey][eh]=true;
}
}
}
}
int main()
{
cin>>n>>m>>t;
for(int i=1;i<=t;i++)
{
int r,c,a,b;
cin>>r>>c>>a>>b;
mp[r][c].tp=1;
mp[r][c].t1=a;
mp[r][c].t2=b;
}
cout<<bfs()<<endl;
return 0;
}