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. Each1×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$](2995img1.gif)
![$ \le$](2995img1.gif)
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
训练指南的例题,感觉好难,不看他给的代码就不会做。
有N*N的方块,是由1*1的小方块组成,每个小方块的6个面颜色一样。现在可能有一些小方块不存在,给你6个方向的颜色的视图,.表示这个位置颜色不存在,问最多有多少个小方块。
关键是如果同一个小方块从两个方向看颜色不一样,那么这个小方块一定不存在。如果视图上一个点没颜色,说明从这个点水平看过去的一条都没有小方块。
先假设有个新的N*N的立方体,没有涂颜色,并且每一个小方块都存在,然后再根据题目给的视图删掉矛盾的小方块,直到最后没有矛盾了,答案就出来了。
找矛盾的方法:用view[k][i][j]表示k视图第i行第j列的颜色,pos[x][y][z]表示自己那个新的立方体坐标为x,y,z的小立方体的颜色。循环找出所有view[k][i][j]!='.'的地方,找到当前view[k][i][j]对应看到的新立方体的x,y,z的小立方体,如果小立方体没有染颜色,就染上,退出深度这一层循环(不矛盾)。如果小立方体颜色和view的颜色一样,退出深度的循环(不矛盾),不矛盾说明目前这个小立方体存在,深度就不能再增加了,view看到的就是它。如果小立方体的颜色和view的颜色不一样,矛盾,就把这个小立方体从新立方体中删掉。
这道题不知道为什么用memset初始化pos为‘#’ WA了,循环赋值就过了。。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define INF 0x3f3f3f3f
#define MAXN 15
#define MAXM 20010
#define eps 1e-9
#define pii pair<int,int>
using namespace std;
int N;
char pos[MAXN][MAXN][MAXN],view[6][MAXN][MAXN];
char read_char(){
char c;
while(1){
c=getchar();
if(isupper(c)||c=='.') return c;
}
}
void get(int &x,int &y,int &z,int k,int i,int j,int p){ //对应转化
if(k==1){
x=N-p+1;
y=j;
z=N-i+1;
}
if(k==2){
x=j;
y=p;
z=N-i+1;
}
if(k==3){
x=p;
y=N-j+1;
z=N-i+1;
}
if(k==4){
x=N-j+1;
y=N-p+1;
z=N-i+1;
}
if(k==5){
x=i;
y=j;
z=N-p+1;
}
if(k==6){
x=N-i+1;
y=j;
z=p;
}
}
int main(){
freopen("in.txt","r",stdin);
while(scanf("%d",&N),N){
int i,j,k,x,y,z,p;
for(i=1;i<=N;i++)
for(k=1;k<=6;k++)
for(j=1;j<=N;j++) view[k][i][j]=read_char();
for(x=1;x<=N;x++) //给pos初始化,#代表没颜色,不知道为什么memset不行
for(y=1;y<=N;y++)
for(z=1;z<=N;z++) pos[x][y][z]='#';
for(k=1;k<=6;k++) //删掉一开始就确定不存在的点
for(i=1;i<=N;i++)
for(j=1;j<=N;j++) if(view[k][i][j]=='.'){
for(p=1;p<=N;p++){
get(x,y,z,k,i,j,p);
pos[x][y][z]='.';
}
}
while(1){ //循环找矛盾
int flag=1;
for(k=1;k<=6;k++)
for(i=1;i<=N;i++)
for(j=1;j<=N;j++) if(view[k][i][j]!='.'){
for(p=1;p<=N;p++){
get(x,y,z,k,i,j,p);
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]='.';
flag=0;
}
}
if(flag) break; //当前view一整个循环都找不到矛盾
}
int ans=0;
for(x=1;x<=N;x++)
for(y=1;y<=N;y++)
for(z=1;z<=N;z++) if(pos[x][y][z]!='.') ans++;
printf("Maximum weight: %d gram(s)\n",ans);
}
return 0;
}