UVA 1062 Lattice Animals
2016-11-11 21:24
555 查看
P3870网格动物
问题描述
网格动物是一种在网格中的联通块,由n个方块构成的联通块称为n连块。平移、旋转、翻转之后相同的算作同一种。例如,2*4里面的5连块有5种(第一行),3*3里的8连块有3种(第二行)。
输入n,w,h,请问w*h的网格里有多少种n连块?
输入格式
输入包含多组数据,每组数据仅一行,三个整数n,w,h。
输出格式
每行输出一个整数,表示N连块的个数
样例输入
5 1 4
5 2 4
5 3 4
5 5 5
8 3 3
样例输出
0
5
11
12
3
提示
1<=n<=10
1<=w,h<=n
数据组数<=300
看数据范围就知道是搜索题了……,虽然组数比较多我们可以先算出数组ans
[w][h]然后直接输出就完了
我们就从(0,0)号点开始往上下左右四个方向搜索,如果对于一个搜到的连通块,在此之前已经搜到了一个连通块可以通过翻转,旋转,翻转后旋转等等操作得到这个连通块,就说明这个连通块已经出现过了,不计入答案
那么我们判断一个连通块是否出现过呢?我们可以使用如下方法:
定义一个结构体Single,表示每一个单位格子的位置坐标
再定义一个集合set<Single>,即为一个连通块所选的格子集合
再定义一个集合set<set<Single> >G[i] 即为大小为i的连通块的集合
所以判断一个连通块s是否已经出现过,只需要判断G[s.size()].count(s)即可
在搜完之后要进行一个标准化操作,即算出一个连通块集合的最小格子的坐标,然后所有其他格子的坐标都减去这个坐标,易于判断和查找
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
const int maxn=15,inf=0x3f3f3f3f;
inline void _read(int &x){
char t=getchar();bool sign=true;
while(t<'0'||t>'9')
{if(t=='-')sign=false;t=getchar();}
for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0';
if(!sign)x=-x;
}
struct Single{
int x,y;
Single(int x,int y):x(x),y(y){}
bool operator<(const Single& h)const{
return x==h.x?y<h.y:x<h.x;
}
};
typedef set<Single> Block;
set<Block> G[maxn];
int ans[15][15][15],n,w,h,t;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
inline Block Standarize(Block p){//标准化操作
int minx=inf,miny=inf;
Block temp;
set<Single> :: iterator it;
for(it=p.begin();it!=p.end();it++)
minx=min(minx,it->x),miny=min(miny,it->y);
for(it=p.begin();it!=p.end();it++)
temp.insert(Single(it->x-minx,it->y-miny));
return temp;
}
inline Block Spin(Block p){//旋转操作
Block temp;
set<Single>::iterator it;
for(it=p.begin();it!=p.end();it++)
temp.insert(Single(it->y,-(it->x)));
return Standarize(temp);
}
inline Block Reverse(Block p){//翻转操作
Block temp;
set<Single>::iterator it;
for(it=p.begin();it!=p.end();it++)
temp.insert(Single(it->x,-(it->y)));
return Standarize(temp);
}
bool check(Block p){//判断p连通块是否已经出现过
int i,B=p.size();
if(G[B].count(p))return 0;
for(i=0;i<4;i++){
p=Spin(p);
if(G[B].count(p))return 0;
}
p=Reverse(p);
if(G[B].count(p))return 0;
for(i=0;i<4;i++){
p=Spin(p);
if(G[B].count(p))return 0;
}
G[B].insert(p);
return 1;
}
void dfs(Block p){
if(p.size()==n){
check(p);
return ;
}
set<Single> :: iterator it;
for(it=p.begin();it!=p.end();it++){
for(int i=0;i<4;i++){
Single cur=Single(dx[i]+it->x,dy[i]+it->y);
if(!p.count(cur)){
Block temp=p;
temp.insert(cur);
dfs(temp);
}
}
}
}
void Get_table(){
Block s;
s.insert(Single(0,0));
G[1].insert(s);
for(n=2;n<=10;n++){
set<Block> :: iterator it;
for(it=G[n-1].begin();it!=G[n-1].end();it++)dfs(*it);
}
for(n=1;n<=10;n++){
set<Block>::iterator it;
set<Single>::iterator i;
for(it=G
.begin();it!=G
.end();it++){
int maxx=0,maxy=0;
for(i=it->begin();i!=it->end();i++){
maxx=max(maxx,i->x);
maxy=max(maxy,i->y);
}
if(maxx<maxy)swap(maxx,maxy);
for(w=1;w<=10;w++)
for(h=1;h<=10;h++)
if(maxx<max(w,h)&&maxy<min(w,h))ans
[w][h]++;
}
}
}
int main(){
Get_table();
while(cin>>n>>w>>h)printf("%d\n",ans
[w][h]);
}
来源 spark uva1602
|
网格动物是一种在网格中的联通块,由n个方块构成的联通块称为n连块。平移、旋转、翻转之后相同的算作同一种。例如,2*4里面的5连块有5种(第一行),3*3里的8连块有3种(第二行)。
输入n,w,h,请问w*h的网格里有多少种n连块?
输入格式
输入包含多组数据,每组数据仅一行,三个整数n,w,h。
输出格式
每行输出一个整数,表示N连块的个数
样例输入
5 1 4
5 2 4
5 3 4
5 5 5
8 3 3
样例输出
0
5
11
12
3
提示
1<=n<=10
1<=w,h<=n
数据组数<=300
看数据范围就知道是搜索题了……,虽然组数比较多我们可以先算出数组ans
[w][h]然后直接输出就完了
我们就从(0,0)号点开始往上下左右四个方向搜索,如果对于一个搜到的连通块,在此之前已经搜到了一个连通块可以通过翻转,旋转,翻转后旋转等等操作得到这个连通块,就说明这个连通块已经出现过了,不计入答案
那么我们判断一个连通块是否出现过呢?我们可以使用如下方法:
定义一个结构体Single,表示每一个单位格子的位置坐标
再定义一个集合set<Single>,即为一个连通块所选的格子集合
再定义一个集合set<set<Single> >G[i] 即为大小为i的连通块的集合
所以判断一个连通块s是否已经出现过,只需要判断G[s.size()].count(s)即可
在搜完之后要进行一个标准化操作,即算出一个连通块集合的最小格子的坐标,然后所有其他格子的坐标都减去这个坐标,易于判断和查找
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
const int maxn=15,inf=0x3f3f3f3f;
inline void _read(int &x){
char t=getchar();bool sign=true;
while(t<'0'||t>'9')
{if(t=='-')sign=false;t=getchar();}
for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0';
if(!sign)x=-x;
}
struct Single{
int x,y;
Single(int x,int y):x(x),y(y){}
bool operator<(const Single& h)const{
return x==h.x?y<h.y:x<h.x;
}
};
typedef set<Single> Block;
set<Block> G[maxn];
int ans[15][15][15],n,w,h,t;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
inline Block Standarize(Block p){//标准化操作
int minx=inf,miny=inf;
Block temp;
set<Single> :: iterator it;
for(it=p.begin();it!=p.end();it++)
minx=min(minx,it->x),miny=min(miny,it->y);
for(it=p.begin();it!=p.end();it++)
temp.insert(Single(it->x-minx,it->y-miny));
return temp;
}
inline Block Spin(Block p){//旋转操作
Block temp;
set<Single>::iterator it;
for(it=p.begin();it!=p.end();it++)
temp.insert(Single(it->y,-(it->x)));
return Standarize(temp);
}
inline Block Reverse(Block p){//翻转操作
Block temp;
set<Single>::iterator it;
for(it=p.begin();it!=p.end();it++)
temp.insert(Single(it->x,-(it->y)));
return Standarize(temp);
}
bool check(Block p){//判断p连通块是否已经出现过
int i,B=p.size();
if(G[B].count(p))return 0;
for(i=0;i<4;i++){
p=Spin(p);
if(G[B].count(p))return 0;
}
p=Reverse(p);
if(G[B].count(p))return 0;
for(i=0;i<4;i++){
p=Spin(p);
if(G[B].count(p))return 0;
}
G[B].insert(p);
return 1;
}
void dfs(Block p){
if(p.size()==n){
check(p);
return ;
}
set<Single> :: iterator it;
for(it=p.begin();it!=p.end();it++){
for(int i=0;i<4;i++){
Single cur=Single(dx[i]+it->x,dy[i]+it->y);
if(!p.count(cur)){
Block temp=p;
temp.insert(cur);
dfs(temp);
}
}
}
}
void Get_table(){
Block s;
s.insert(Single(0,0));
G[1].insert(s);
for(n=2;n<=10;n++){
set<Block> :: iterator it;
for(it=G[n-1].begin();it!=G[n-1].end();it++)dfs(*it);
}
for(n=1;n<=10;n++){
set<Block>::iterator it;
set<Single>::iterator i;
for(it=G
.begin();it!=G
.end();it++){
int maxx=0,maxy=0;
for(i=it->begin();i!=it->end();i++){
maxx=max(maxx,i->x);
maxy=max(maxy,i->y);
}
if(maxx<maxy)swap(maxx,maxy);
for(w=1;w<=10;w++)
for(h=1;h<=10;h++)
if(maxx<max(w,h)&&maxy<min(w,h))ans
[w][h]++;
}
}
}
int main(){
Get_table();
while(cin>>n>>w>>h)printf("%d\n",ans
[w][h]);
}
来源 spark uva1602
相关文章推荐
- UVa1602 - Lattice Animals
- Lattice Animals UVA - 1602
- UVA1602 Lattice Animals 网格动物 (暴力,STL)
- UVa1062 - Containers贪心算法
- UVA 1602 Lattice Animals
- 搜索 【uva1602】Lattice Animals (练习题7-14 网格动物)
- Uva 1602 Lattice Animals (网格动物)
- UVA-1602 Lattice Animals 搜索问题(打表+set)
- [Uva1602][Poj2170][Zoj2669][Northeastern Europe 2004] Lattice Animals 【set+傻瓜搜索】
- 【例题 7-14 UVA-1602】Lattice Animals
- UVa 1602 Lattice Animals 网格动物
- UVa 1602 Lattice Animals
- Uva1602 Lattice Animals 【枚举打表+形状判重】【例题7-14】
- UVA - 1602 Lattice Animals : 完备信息 set
- UVa 1602:Lattice Animals(BFS)
- uva 1602 Lattice Animals
- UVA 10382
- uva 409
- UVA562 Dvide Coins
- uva 10534 Wavio Sequence