这题的剪枝不错,看了别人的代码才发现有这种剪枝的。留个纪念。或许以后有用。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <string>
#define LL long long
#define DB double
#define SD(a) scanf("%lf",&a)
#define SS(a) scanf("%s",a)
#define SF scanf
#define PF printf
#define MM(a,v) memset(a,v,sizeof(a))
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define REPD(i,a,b) for(int (i)=(a);(i)>(b);(i)--)
#define N 2009
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define bug puts("bug")
using namespace std;
int v[N][N];
int n,m,k;
char cc;
void inline SI(int &c)
{
c = 0;
while(1)
{
cc = getchar();
if(cc>='0'&&cc<='9')
{
c = c*10+cc-'0';
}else
{
if(c>0) return ;
}
}
}
int oor(int x,int y)
{
if(x<1||x>n) return 0;
if(y<1||y>m) return 0;
return 1;
}
struct nod{
int x,y;
void get(){SI(x);SI(y);}
};
nod get(nod &c)
{
if(v[c.x][c.y]==0)
{
v[c.x][c.y] = 1;
return c;
}
nod t;
REP(i,v[c.x][c.y],INF)
{
int f = max(c.x-i,1),to = min(n+1,c.x+i+1);
int y1=c.y , y2= c.y;
if(c.x-i<1)
{
y1-=(1-c.x+i);
y2+=(1-c.x+i);
}
REP(x,f,to)
{
if(oor(x,y1)&&v[x][y1]==0)
{
t.x= x;
t.y= y1;
v[x][y1] = 1;
return t;
}
if(oor(x,y2)&&v[x][y2]==0)
{
t.x= x;
t.y= y2;
v[x][y2] = 1;
return t;
}
if(x<c.x) y1--,y2++;
else y1++,y2--;
}
v[c.x][c.y]++;
}
return t;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w+",stdout);
#endif
SI(n);SI(m);SI(k);
nod t;
REP(i,0,k)
{
t.get();
REP(j,-2,3)//剪枝
REP(k,-2,3)
if(oor(t.x+j,t.y+k))
{
v[t.x][t.y] = max(v[t.x][t.y],v[t.x+j][t.y+k]-abs(j)-abs(k));
}
t = get(t);
PF("%d %d\n",t.x,t.y);
}
return 0;
}