任一时间t的可能位置=t-1所有可能位置移动一步的有效位置
#include<iostream>
#include<set>
using namespace std;
class tim{
public:
int a,b;
tim(){a=b=-1;}
};
class dex{
public:
int r,c;
dex(int x,int y) {r=x;c=y;}
dex() {r=0;c=0;}
bool operator<(const dex v)const{
return r<v.r || (r==v.r && c<v.c);}
};
tim all[105][105];
int n,m,t;
int _min=9999+m*n;
set<dex> ww[2];
int main(){
cin>>n>>m>>t;
int r,c,a,b;
while(t--){
cin>>r>>c>>a>>b;
all[r][c].a=a;
all[r][c].b=b;
}
dex x;
x.c=x.r=1;
ww[0].insert(x);
x.r=n;x.c=m;
int t=0;
int f=0;
while(ww[f].find(x)==ww[f].end()){
++t;
int g=(f+1)&1;
for(set<dex>::iterator it=ww[f].begin();it!=ww[f].end();it++){
int xx=it->r+1,yy=it->c;
if(xx>0 && xx<=n && yy>0 && yy<=m && (t< all[xx][yy].a || t>all[xx][yy].b)) ww[g].insert(dex(xx,yy));
xx=it->r-1;
if(xx>0 && xx<=n && yy>0 && yy<=m && (t< all[xx][yy].a || t>all[xx][yy].b)) ww[g].insert(dex(xx,yy));
xx=it->r,yy=it->c+1;
if(xx>0 && xx<=n && yy>0 && yy<=m && (t< all[xx][yy].a || t>all[xx][yy].b)) ww[g].insert(dex(xx,yy));
xx=it->r,yy=it->c-1;
if(xx>0 && xx<=n && yy>0 && yy<=m && (t< all[xx][yy].a || t>all[xx][yy].b)) ww[g].insert(dex(xx,yy));
}
ww[f].clear();
f=g;
}
cout<<t<<endl;
}