#include using namespace std; typedef long long int ll; #define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout< ostream& operator <<(ostream &o,const pair p){o<<"("<=qmax ? 0 : _quf; } inline void _push(int a,int b,int c){ _qu[_quf][0]=a; _qu[_quf][1]=b; _qu[_quf][2]=c; _quf=(++_quf)>=qmax ? 0 : _quf; } inline void _pop(int &a,int &b,int &c){ a=_qu[_qub][0]; b=_qu[_qub][1]; c=_qu[_qub][2]; _qub=(++_qub)>=qmax ? 0 : _qub; } int w,h; int dp[1001][1001]; int vroad[1001][1001]; int hroad[1001][1001]; int vimos[1001][1001]; int himos[1001][1001]; int main(){ int i,j,k,l; int x,y,d; int m,n; int u1,u2; cin >>w>>h>>n; for (i=0;i=u2){ // down(flip->up) vimos[u2%w][u2/w]=max(u1/w-u2/w,vimos[u2%w][u2/w]); }else if (u1>u2){ // left himos[u2%w][u2/w]=max(u1-u2,himos[u2%w][u2/w]); }else if(u1+w>u2){ // right himos[u1%w][u1/w]=max(u2-u1,himos[u1%w][u1/w]); }else{ // up(flip->down) vimos[u1%w][u1/w]=max(u2/w-u1/w,vimos[u1%w][u1/w]); } u1=u2; } } for (x=0;x> q; _push(0,0,0); while (_qub!=_quf){ _pop(x,y,d); if (dp[x][y]) continue; if (x==w-1 && y==h-1){ cout<