Image is Everything

Problem

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 ≤ N ≤ 10). 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

.R. YYR .Y. RYY .Y. .R. 

GRB YGR BYG RBY GYB GRB 

.R. YRR .Y. RRY .R. .Y. 

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)

题目分析

题目大意  一个n*n*n的立方体,每一个1*1*1的小立方体都涂有单一的不同的颜色,给出前、左、后、右、上和下的视图,"."表示该位置能被看穿(即立方体有缺失),不同的字母表示不同的颜色,求该物体可能的最大的体积。

分析

.R. YYR .Y. RYY .Y. .R. 

GRB YGR BYG RBY GYB GRB 

.R. YRR .Y. RRY .R. .Y. 

n个字符为一组,样例中所给的每n组对应一个面所观察到的情况。联系实际可以分析,能看穿(即有"."标志的地方所对应的单位立方体一定都不存在。)顶视图的下边界与前视图的上边界对应,底视图的上边界与前视图的下边界对应,存在这类的对应方式但是字母不同(即颜色不同)的正方体不存在。按此,迭代删除所有不可能存在的小正方体,最后剩下的即最大的物体。

按序遍历,每一个单位立方体都会有一个与之对应的x,y,z,用View表示不同的视图看到的颜色的情况,用pos表示当前遍历到的立方体的位置,用”#“标记整个正方体(pos表示的整个立方体),根据上述原则,pos数组对应view数组, 不可能存在的具体x,y,z标记为".",带遍历结束后,没有被标记过"."的位置都是"#",是存在小立方体的。

取人之长

  • 利用REP(i,n) 宏定义简化代码
  • 定义了read_char函数返回被筛选过的字符,比起直接scanf,代码的鲁棒性更好,也节省了空间。

练习地址:点击打开链接

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)

const int maxn =10;
int n;
char pos[maxn][maxn][maxn];
char view[6][maxn][maxn];

char read_char()  //选择性读取各个视图的颜色
{
	char ch;
	for(;;)
	{
		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) //6个面对应的x,y,z的值
{
	if(k==0){x=len;y=j;z=i;}
	if(k==1){x=n-1-j;y=len;z=i;}
	if(k==2){x=n-1-len;y=n-1-j;z=i;}
	if(k==3){x=j;y=n-1-len;z=i;}
	if(k==4){x=n-1-i;y=j;z=len;}
	if(k==5){x=i;y=j;z=n-1-len;}
}
int main()
{
	while(scanf("%d",&n)==1&&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]='.';
			}
		for(;;)
		{
			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;
} 



 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值