#include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,j,n) for(int i=j;i lt; }cells[1001][1001]; bool paths[1001]; //map > > mp; int x,y,stx,sty; int exits; int main() { cin>>x>>y; cin>>stx>>sty; stx--; sty--; REP(i,0,x) REP(j,0,y) { scanf("%d",&cells[i][j].trav); cells[i][j].exit=false; cells[i][j].prevx=cells[i][j].prevy=-1; cells[i][j].trav=0; } int path; cin>>path; int x1,y1,x2,y2; REP(i,0,path) { scanf("%d %d %d %d",&x1,&y1,&x2,&y2); cells[x1-1][y1-1].path=true; cells[x1-1][y1-1].lt.PB(point(x2-1,y2-1)); } cin>>exits; REP(i,0,exits){ scanf("%d %d",&x1,&y1); cells[x1-1][y1-1].exit=true; } queue q; q.push(point(stx,sty)); bool flag=false; int markx,marky; point res; while(!q.empty()){ point a=q.front(); q.pop(); x1=a.x; y1=a.y; if(cells[x1][y1].exit==true) { res.x=x1; res.y=y1; //cout<-1&&cells[x1-1][y1].trav==0){ q.push(point(x1-1,y1)); cells[x1-1][y1].trav=1; cells[x1-1][y1].prevx=x1; cells[x1-1][y1].prevy=y1; } if(y1-1>-1&&cells[x1][y1-1].trav==0){ q.push(point(x1,y1-1)); cells[x1][y1-1].trav=1; cells[x1][y1-1].prevx=x1; cells[x1][y1-1].prevy=y1; } if(y1+1 stk; stk.push(res); while(res.x!=-1){ cout<>x; return 0; }