UVA1602

实现的细节很多,学到了如何翻转、旋转、平移,get很多技巧,值得一做。
AC代码:

#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=10+5;
int ans[maxn][maxn][maxn];

struct animal{
    int x,y;
    animal(int x=0,int y=0):x(x),y(y){};
    bool operator < (const animal &p) const {
        return x<p.x||(x==p.x&&y<p.y);
    }
};
typedef set<animal> node;
#define For_animal(c,p) for(node::iterator c=(p).begin();c!=(p).end();++c)

inline node normalize(const node &p){  //normalize
    int minx=p.begin()->x,miny=p.begin()->y;
    For_animal(c,p){
        minx=min(minx,c->x);
        miny=min(miny,c->y);
    }
    node p2;
    For_animal(c,p){
        p2.insert(animal(c->x-minx,c->y-miny));
    }
    return p2;
}

inline node rotate(const node &p){  //rotate
    node p2;
    For_animal(c,p){
        p2.insert(animal(c->y,-c->x));
    }
    return normalize(p2);
}

inline node flip(const node &p){ //flip
    node p2;
    For_animal(c,p){
        p2.insert(animal(c->x,-c->y));
    }
    return normalize(p2);
}

const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
set<node>poly[maxn];

void add(const node &p0,const animal &newc){ //add
    node p=p0;
    p.insert(newc);
    p=normalize(p);
    int n=p.size();
    for(int i=0;i<4;++i){
        if(poly[n].count(p)!=0) return;
        p=rotate(p);
    }
    p=flip(p);
    for(int i=0;i<4;++i){
        if(poly[n].count(p)!=0) return;
        p=rotate(p);
    }
    poly[n].insert(p);
}

void solve(){
    node s;
    s.insert(animal(0,0));
    poly[1].insert(s);

    for(int n=2;n<=10;++n){
        for(set<node>::iterator p=poly[n-1].begin();p!=poly[n-1].end();++p){
           For_animal(c,*p)     //从当前联通块开始延伸
                for(int dir=0;dir<4;++dir){
                    int newx=c->x+dx[dir],newy=c->y+dy[dir];
                    animal newc(newx,newy);
                    if(p->count(newc)==0) add(*p,newc);
                }
        }
    }

    //make answer into array
    const int len=10;
    for(int n=1;n<=len;++n)
        for(int w=1;w<=len;++w)
            for(int h=1;h<=len;++h){
                int cnt=0;
                for(set<node>::iterator p=poly[n].begin();p!=poly[n].end();++p){
                    int maxx=0,maxy=0;
                    For_animal(c,*p){
                        maxx=max(maxx,c->x);
                        maxy=max(maxy,c->y);
                    }
                    if(min(maxx,maxy)<min(w,h)&&max(maxx,maxy)<max(w,h))  //这个地方是<而不是<=的原因是坐标是从0开始
                        ++cnt;
                }
                ans[n][w][h]=cnt;
            }

}


int main(){
    solve();
    int n,w,h;
    while(scanf("%d%d%d",&n,&w,&h)==3){
        printf("%d\n",ans[n][w][h]);
    }
    return 0;
}

如有不当之处欢迎指出!

转载于:https://www.cnblogs.com/flyawayl/p/8305569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值