【Jason's_ACM_解题报告】Image Is Everything

Image Is Everything

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.

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

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

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)


此题乃是WorldFinal试题,也算是为我做不出来找了一个借口,直接看的Liu的代码。。。

对我来说这道题的信息量很大,我学到了这些东西:

1.在适当的场合(大量的嵌套for循环)宏定义REP来简化for循环的代码量;

2.编写合适的read_char()函数方便的读入字符串并过滤掉无关的字符;

3.通过传递实参实现多返回值函数;

4.for(;;)的巧妙应用;

5.使用数学归纳法证明算法的正确性(此处我理解的有点模糊,请Doris看看能不能帮忙解释一下~)

需要注意的一点是,这里pos[x][y][z]的三个坐标的坐标系为如下坐标系(空间直角坐标系的x轴的负方向为此题建模的正方向,z轴的负方向为此题建模的正方向,y轴不变):

(图片可能无法显示)



附代码如下:


#include<cstdio> 
#include<cstring>

using namespace std;

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

int n;
char pos[MAXN][MAXN][MAXN];
char view[6][MAXN][MAXN];

char read_char(){
	char ch;
	for(;;){
		ch=getchar();
		if(('A'<=ch&&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=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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值