CodeForces 200A (暴力+剪枝)

这题的剪枝不错,看了别人的代码才发现有这种剪枝的。留个纪念。或许以后有用。

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值