UVa 1602 网格动物(回溯)
2017-01-25 15:21
316 查看
https://vjudge.net/problem/UVA-1602
题意:计算n连通块不同形态的个数。
思路:
实在是不知道该怎么做好,感觉判重实在是太麻烦了。
判重就是判断所有格子位置是否都相同,这样我们可以定义一个结构体来保存每个格子的坐标点,用set容器poly来保存这些格子,然后再用一个set容器poly_set来保存指定数量i个连通块的各个图形的坐标点,也就是说该容器是用来保存poly的。(不太好解释,具体可以看代码。)因为图形必须是连通的,所以在添加第i个格子的时候必定是在i-1个格子的基础上添加的。这样我们在添加第i个格子的时候,只需要遍历保存第(i-1)个连通块的poly_set,然后遍历它当中的每个格子,每个格子可以往4个方向发展。然后进行判断。
因为一个连通块可以在不同的坐标点,即使他们形状相同,也许坐标是不同的。所以首先需要将它们标准化,也就是平移。平移就是选取连通块中最小的x和y值,并将(x,y)定为(0,0)原点,这样每个图形就可以比较坐标了。当然,判重时还需要进行旋转,180度翻转操作,旋转的话就是每个格子都顺时针旋转90度即可。相应的几何变换为(x,y)->(y,-x)。180度翻转得几何变化就是(x,y)->(x,-y)。旋转和翻转后需要重新平移来标准化。
由于这道题数据不大,可以直接打表,这样数据多的时候比较快。
这道题目确实很不错,不过真的是挺麻烦的,借鉴了大神们的代码之后,看了很久终于理解了做法。
题意:计算n连通块不同形态的个数。
思路:
实在是不知道该怎么做好,感觉判重实在是太麻烦了。
判重就是判断所有格子位置是否都相同,这样我们可以定义一个结构体来保存每个格子的坐标点,用set容器poly来保存这些格子,然后再用一个set容器poly_set来保存指定数量i个连通块的各个图形的坐标点,也就是说该容器是用来保存poly的。(不太好解释,具体可以看代码。)因为图形必须是连通的,所以在添加第i个格子的时候必定是在i-1个格子的基础上添加的。这样我们在添加第i个格子的时候,只需要遍历保存第(i-1)个连通块的poly_set,然后遍历它当中的每个格子,每个格子可以往4个方向发展。然后进行判断。
因为一个连通块可以在不同的坐标点,即使他们形状相同,也许坐标是不同的。所以首先需要将它们标准化,也就是平移。平移就是选取连通块中最小的x和y值,并将(x,y)定为(0,0)原点,这样每个图形就可以比较坐标了。当然,判重时还需要进行旋转,180度翻转操作,旋转的话就是每个格子都顺时针旋转90度即可。相应的几何变换为(x,y)->(y,-x)。180度翻转得几何变化就是(x,y)->(x,-y)。旋转和翻转后需要重新平移来标准化。
由于这道题数据不大,可以直接打表,这样数据多的时候比较快。
这道题目确实很不错,不过真的是挺麻烦的,借鉴了大神们的代码之后,看了很久终于理解了做法。
#include<iostream> #include<cstring> #include<set> #include<algorithm> using namespace std; const int dx[] = { -1, 1, 0, 0 }; const int dy[] = { 0, 0, -1, 1 }; const int N = 10; int n, w, h; int ans[15][15][15]; //打表之后的答案 struct Cell //定义单元格 { int x, y; Cell(int a, int b) { x = a; y = b; } Cell() {} bool operator < (const Cell&rhs) const //重载小于号,在set容器中排序 { return x < rhs.x || (x == rhs.x && y < rhs.y); } }; typedef set<Cell>poly; //坐标的集合,也就是一个连通块(1,2,3....) set<poly> poly_set[15]; //有i个Cell的poly集合 //规范化到最小点(0,0) poly normalize(poly& p) { poly this_p; int min_x = p.begin()->x, min_y = p.begin()->y; //找到最小的x和y坐标 for (poly::iterator q = p.begin(); q != p.end(); q++) { if (q->x < min_x) min_x = q->x; if (q->y < min_y) min_y = q->y; } //平移 for (poly::iterator q = p.begin(); q != p.end(); q++) { this_p.insert(Cell(q->x - min_x, q->y - min_y)); } return this_p; } //向右翻转90度 poly rotate(poly& p) { poly this_p; for (poly::iterator q = p.begin(); q != p.end(); q++) { this_p.insert(Cell(q->y, -q->x)); //x值为原来的y值,y值为原来的-x值 } //旋转之后需要将它规范化 return normalize(this_p); } //向下翻转180度 poly flip(poly& p) { poly this_p; for (poly::iterator q = p.begin(); q != p.end(); q++) { this_p.insert(Cell(q->x, -q->y)); //x坐标不变,y变号 } return normalize(this_p); } void check(const poly& this_p, Cell& this_c) { poly p = this_p; p.insert(this_c); //标准化 p = normalize(p); int n = p.size(); for (int i = 0; i < 4; i++) { //if (poly_set .find(p) != 0 ) if (poly_set .find(p) != poly_set .end()) //已存在 return; //对该连通块向右旋转90度 p = rotate(p); } //将该连通块翻转180度 p = flip(p); for (int i = 0; i < 4; i++) { if (poly_set .find(p) != poly_set .end()) return; p = rotate(p); } //如果未重复则加入该连通块的集合 poly_set .insert(p); } void Generate() //打表 { //先生成一个连通块 poly s; s.insert(Cell(0, 0)); poly_set[1].insert(s); //插入到一个Cell的poly集合,只有一个格子的连通块只有这么一个 //根据有i-1个Cell(格子)的poly(连通块)集合来生成有i个Cell的poly集合 for (int i = 2; i <= N; i++) //从生成第2个格子开始,一直到第10个 { //遍历有i-1个Cell的连通块集合 for (set<poly>::iterator p = poly_set[i - 1].begin(); p != poly_set[i - 1].end(); p++) { //在每个连通块中遍历每个Cell即格子 for (poly::const_iterator q = p->begin(); q != p->end(); q++) { for (int j = 0; j < 4; j++) { Cell new_c(q->x + dx[j], q->y + dy[j]); //生成新格子 if (p->find(new_c) == p->end()) //如果在该坐标上还没有Cell(格子),则继续往下判断 { check(*p, new_c); //判断当前连通块加上这个Cell坐标后是否存在 } } } } } //生成答案 for(int i = 1; i <= N;i++) { for (int w = 1; w <= i; w++) { for (int h = 1; h <= i; h++) { int cnt = 0; for (set<poly>::iterator p = poly_set[i].begin(); p != poly_set[i].end(); p++) { int max_x = p->begin()->x, max_y = p->begin()->y; for (poly::iterator q = p->begin(); q != p->end(); q++) { if (max_x < q->x) max_x = q->x; if (max_y < q->y) max_y = q->y; } if (min(max_x, max_y) < min(w, h) && max(max_x,max_y) < max(w, h)) //判断能够放入网格的条件 cnt++; } ans[i][w][h] = cnt; } } } } int main() { //freopen("D:\\txt.txt", "r", stdin); Generate(); //打表 while (cin >> n >> w >> h) { cout << ans [w][h] << endl; } return 0; }
相关文章推荐
- UVA1602 Lattice Animals 网格动物 (暴力,STL)
- Uva 1602 Lattice Animals (网格动物)
- UVa 1602 Lattice Animals 网格动物
- 搜索 【uva1602】Lattice Animals (练习题7-14 网格动物)
- 例题7-14 网格动物 UVa1602
- 例题7-14 网格动物(Lattice Animals, ACM/ICPC NEERC 2004, UVa1602)
- uva 10001(回溯)
- UVA - 387 A Puzzling Problem(回溯)
- uva519 - Puzzle (II)(回溯)
- UVA - 185 Roman Numerals(回溯+剪枝)
- Uva 110 - Meta-Loopless Sorts(!循环,回溯!)
- UVA12113(暴力+回溯)
- uva 1602 Lattice Animals
- Graph Coloring +uva+回溯
- uva--331+dfs+回溯
- uva 11210 Chinese Mahjong 中国麻将 dfs回溯
- UVA 208 Firetruck 消防车(回溯 + 剪枝)
- 习题 7-2 uva225(回溯)
- uva539 卡坦岛 简单回溯!
- UVa 129 - Krypton Factor(DFS+回溯)