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 ( 1N10 ). The next N lines are the different N×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. 2 ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ 0
Sample Output
Maximum weight: 11 gram(s)
Maximum weight: 8 gram(s)
我个人的空间思维能力并不是很强,所以这道题让我很苦恼,第一遍连题目都没有看懂
。
后来反复阅读加上看了书上的解题思路之后,才搞懂了这道题。思路理清之后,我一遍就通过了测试。再回头看看这道题,发现它的代码虽长但是敲起来并不麻烦,所以我发现,只要是理清了思路,不管代码长还是短,敲起来都不困难。下面我大体说一下这道我题的思路。
#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;
}
通过这道题,我学会了: