Image Is Everything

来源:互联网 发布:java开发pc客户端 编辑:程序博客网 时间:2024/06/05 08:26

Description

Your new company is building a robot that can hold small lightweight objects. The robot will have the intelligence to determine if an object is light enough to hold. It does this by taking pictures of the object from the 6 cardinal directions, and then inferring an upper limit on the object's weight based on those images. You must write a program to do that for the robot.

You can assume that each object is formed from an N×N×N lattice of cubes, some of which may be missing. Each 1×1×1 cube weighs 1 gram, and each cube is painted a single solid color. The object is not necessarily connected.

Input

The input for this problem consists of several test cases representing different objects. Every case begins with a line containing N, which is the size of the object ( 1$ \le$N$ \le$10). The next N lines are the differentN×N views of the object, in the order front, left, back, right, top, bottom. Each view will be separated by a single space from the view that follows it. The bottom edge of the top view corresponds to the top edge of the front view. Similarly, the top edge of the bottom view corresponds to the bottom edge of the front view. In each view, colors are represented by single, unique capital letters, while a period (.) indicates that the object can be seen through at that location.

Input for the last test case is followed by a line consisting of the number 0.

Output

For each test case, print a line containing the maximum possible weight of the object, using the format shown below.

Sample Input

3.R. YYR .Y. RYY .Y. .R.GRB YGR BYG RBY GYB GRB.R. YRR .Y. RRY .R. .Y.2ZZ ZZ ZZ ZZ ZZ ZZZZ ZZ ZZ ZZ ZZ ZZ0

Sample Output

Maximum weight: 11 gram(s)Maximum weight: 8 gram(s)
  我个人的空间思维能力并不是很强,所以这道题让我很苦恼,第一遍连题目都没有看懂后来反复阅读加上看了书上的解题思路之后,才搞懂了这道题。思路理清之后,我一遍就通过了测试。再回头看看这道题,发现它的代码虽长但是敲起来并不麻烦,所以我发现,只要是理清了思路,不管代码长还是短,敲起来都不困难。下面我大体说一下这道我题的思路。
  题目中有提示说“.表示该位置没有任何立方体”。同时我们也不难发现,如果我从不同的方向看同一单位立方体,发现它的颜色是不一样的,那说明这个单位立方体是不存在的。因此,我们可以设置两个三维数组——view,pos。分别存放不同视图看到的颜色以及在三维坐标系下单位立方体的位置。我们首先将pos所有位置标记上有颜色,然后根据view把“.”所对应的位置删除。接着按照view在pos中把对应的位置涂上颜色,若出现两种颜色都要涂在同一个单位立方体的情况,说明该位置处无单位立方体,把该位置删除。最终数一下pos中剩余的单位立方体个数即可。代码如下:
#include<iostream>#include<cstdio>#define FOR(i,n) for(int i=0;i<(n);i++)  //宏定义简化代码int n;char view[6][10][10];char pos[10][10][10];using namespace std;char read_char(){for(;;){char a;a=getchar();if((a>='A'&&a<='Z')||a=='.') return a;}}void get(int k,int i,int j,int len,int &x,int &y,int &z){if(k==0){x=len;y=j;z=i;}if(k==1){x=n-j-1;y=len;z=i;}if(k==2){x=n-len-1;y=n-j-1;z=i;}if(k==3){x=j;y=n-len-1;z=i;}if(k==4){x=n-i-1;y=j;z=len;}if(k==5){x=i;y=j;z=n-1-len;}}int main(){while(scanf("%d",&n)==1&&n){FOR(i,n) FOR(k,6) FOR(j,n) view[k][i][j]=read_char();FOR(i,n) FOR(j,n) FOR(k,n) pos[i][j][k]='#';FOR(k,6) FOR(i,n) FOR(j,n)if(view[k][i][j]=='.'){FOR(len,n){int x,y,z;get(k,i,j,len,x,y,z);pos[x][y][z]='.';}}for(;;){bool done=true;FOR(k,6) FOR(i,n) FOR(j,n){if(view[k][i][j]!='.'){int x,y,z;FOR(len,n){get(k,i,j,len,x,y,z);if(pos[x][y][z]=='#'){pos[x][y][z]=view[k][i][j];break;}if(pos[x][y][z]=='.') continue;if(pos[x][y][z]==view[k][i][j]) break;pos[x][y][z]='.';done=false;}}}if(done) break;}int ans=0;FOR(i,n) FOR(j,n) FOR(k,n) if(pos[i][j][k]!='.') ans++;printf("Maximum weight: %d gram(s)\n",ans);}return 0;} 
通过这道题,我学会了:
1.可以用宏定义来简化代码。这道题有大量的for循环,如果不用宏定义,就显得冗长复杂,可读性不强。
2.可以用函数实现字符输入。利用函数可以在数组中之存放我所需要的字符或数据。

0 0
原创粉丝点击