您的位置:首页 > 其它

UVA1602

2016-09-28 18:52 295 查看
实现的细节很多,学到了如何翻转、旋转、平移,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
.count(p)!=0) return;
p=rotate(p);
}
p=flip(p);
for(int i=0;i<4;++i){
if(poly
.count(p)!=0) return;
p=rotate(p);
}
poly
.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
.begin();p!=poly
.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
[w][h]=cnt;
}

}

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


如有不当之处欢迎指出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: