Image Is Everything

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 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)
  我个人的空间思维能力并不是很强,所以这道题让我很苦恼,第一遍连题目都没有看懂 后来反复阅读加上看了书上的解题思路之后,才搞懂了这道题。思路理清之后,我一遍就通过了测试。再回头看看这道题,发现它的代码虽长但是敲起来并不麻烦,所以我发现,只要是理清了思路,不管代码长还是短,敲起来都不困难。下面我大体说一下这道我题的思路。
  题目中有提示说“.表示该位置没有任何立方体”。同时我们也不难发现,如果我从不同的方向看同一单位立方体,发现它的颜色是不一样的,那说明这个单位立方体是不存在的。因此,我们可以设置两个三维数组——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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值