【简单题】【综合能力】UVA-1030-Image Is Everything
来源:互联网 发布:资金流向分析软件 编辑:程序博客网 时间:2024/05/16 20:31
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=3471
题目描述:给你一个最大n*n*n的立方块构成几何体的六个方向上的视图,(各方块是有颜色的,一个单位方块的六面颜色相同),问最多用多少个方块可以满足这些视图
解题思路:因为没用到什么算法,所以可以称之为“水题”,但显然,非常不好做!相当的考验代码能力。
我自己的思路是,最多10*10*10 = 1000 个方块,如果用o(1000)的算法每次找到一个出错的方块并把它删除,那么最多执行1000次删除操作,时间复杂度上可以接受,但是准备自己写的时候发现,真的有点无从下手,怎么判断一个方块的颜色是否出错真是太麻烦了。
于是决定直接看大白书,“学习优秀的代码风格”。
照着大白书写的代码,真的受益匪浅,这道题的结构设计的太棒了
先是考虑到有大量的 for 循环,使用了宏定义REP简洁了代码。
pos 数组和 view 数组思路很清晰
读入方法用的这个read_char函数我也是第一回看到,非常好,让我自己写可能光是怎么输入我还要纠结一会。
最让我敬佩的是那个 get 函数,它把视图和实际几何体联系了起来,输入第 k 视图上的横纵坐标和深度就能获得在实际几何立体中的位置( x , y ,z ),将它们这样通过一个函数方便的联系在一起,并引入 “视图深度” 正是我没想到的!
嗯,,可以说收获很多吧,要消化好自己用上!
AC代码:
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define REP(i,n) for(int i = 0 ; i < (n); i++) ///对for循环的宏定义,这样清爽多了,请背诵const int maxn = 10;int n;char pos[maxn][maxn][maxn]; ///立方空间内的实际方块颜色char view[6][maxn][maxn]; ///存储六个视图char read_char() ///只能读入大写字母或者 ‘.’{ char ch; while(1) { ch = getchar(); if((ch >= 'A' && ch <= 'Z') || ch == '.') return ch; }}void get(int k, int i, int j, int len, int &x, int &y, int &z){ if (k==0) { x = n - 1 - len; y = j; z = n - 1 - i;} if (k==1) { x = j; y = len; z = n - 1 - i;} if (k==2) { x = len; y = n - 1 - j; z = n - 1 - i;} if (k==3) { x = n - 1 - j; y = n - 1 - len; z = n - 1 - i;} if (k==4) { x = i; y = j; z = n - 1 - len;} if (k==5) { x = n - 1 - i; y = j; z = len;}}int main(){ freopen("input.txt","r",stdin); while(scanf("%d", &n),n) { REP(i,n) REP(k,6) REP(j,n) view[k][i][j] = read_char(); ///六个视图的输入 REP(i,n) REP(j,n) REP(k,n) pos[i][j][k] = '#'; ///对立方体的初始化 REP(k,6) REP(i,n) REP(j,n) if (view[k][i][j] == '.') ///如果视图中就存在 ‘.’ ,那么抠去一排 { REP(p,n) { int x,y,z; get(k,i,j,p,x,y,z); pos[x][y][z] = '.'; } } while(1) ///不断地抠去错误的方块 { bool done = true; REP(k,6) REP(i,n) REP(j,n) if (view[k][i][j] != '.') { REP(p,n) { int x,y,z; get(k, i, j, p, x, y, z); if(pos[x][y][z] == '.') continue; if(pos[x][y][z] == '#') ///给没见过的方块染色 { pos[x][y][z] = view[k][i][j]; break; } if (pos[x][y][z] == view[k][i][j]) break; ///找到最靠近表面的颜色正确的了 pos[x][y][z] = '.'; ///以上情况都没发生,就抠去这个方块 done = false; } } if(done) break; } int ans = 0; REP(i,n) REP(j,n) REP(k,n) if (pos[i][j][k] != '.') ans++; printf("Maximum weight: %d gram(s)\n",ans); } return 0;}
AC截图:
0 0
- 【简单题】【综合能力】UVA-1030-Image Is Everything
- Uva-1030-Image Is Everything
- Uva 1030 - Image Is Everything
- UVA 1030 Image Is Everything
- UVA-1030-Image Is Everything
- UVA 1030 Image Is Everything
- Uva 1030 Image Is Everything
- uva 1030 - Image Is Everything(乱搞)
- UVA 1030 - Image Is Everything(贪心枚举)
- 1030 - Image Is Everything
- uva 1030 - Image Is Everything(思维,4级)
- uva 1030 - Image Is Everything(迭代更新)
- uva 1030 Image Is Everything(迭代更新)
- World Finals 2004 / UVa 1030 Image Is Everything (想法题&迭代更新)
- 1030 - Image Is Everything (贪心)
- uvaoj 1030 - Image Is Everything
- Image Is Everything
- Image Is Everything
- linux 内核编译与重启新内核
- Android Studio Unknown host 'downloads.gradle.org'
- C++单元测试--打桩测试
- 按键驱动深化-异步通知机制
- 又一道关于STL中的vector容器的面试题
- 【简单题】【综合能力】UVA-1030-Image Is Everything
- 彻底搞定C指针---指向指针的指针
- 电脑死机后,word未保存
- 软件体系结构的复习提纲
- POJ——水刷 I Think I Need a Houseboat
- HDU 2063过山车(二分最大匹配之最大匹配)
- 写给自己的话
- VS2010编译与安装ICE指南
- 测试驱动风格学习笔记