[ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]
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
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
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) 题目大意:nxnxn立方体,其中一些单位立方体已经缺失(剩下的部分不一定联通)。每个立方体重量为1克,且被图上单一的颜色(即6个面颜色相同)。给出前左后右顶底6个视图,判断这个物体的最大质量。输入包含多组数据,每组数据的第一行为一个整数n;以下n行每行从左往右依次为前左后右顶底6个视图,每个视图占n列。在每一列大写字母表示颜色(不同字母表示不同颜色),句号(.)表示可以看穿(即没有任何立方体)。输入n=0结束
解题思路:看了刘汝佳算法书上的讲解有点晕,于是在网上找了一个人家的讲解才真正明白!本题用的是迭代更新法[这个方法是个很好的解题思路:先弄一个死循环,每次根据上一次状态更新下一次状态,这样可以从一点线索找到全部线索[比如扫雷],然后如果连续2次状态没有改变,那么就说明不会产生新的线索啦,也就结束运算。]对于这一题用下列方法更新状态:
1. 进行循环,每次循环对立方体的六个视图进行遍历
2. 遍历一个视图时,对视图上的每个位置,计算出对应立方体上那一排的每个坐标.
3. 对每个坐标,判断:
a. 如果这个坐标的单位立方体为空,跳过,否则b
b. 如果这个坐标的单位立方体没有上色,将它涂上这个视图对应位置的颜色,否则c
c. 如果单位立方体上色了,判断这个颜色和该视图对应位置颜色是否相同,如果不同说明出现矛盾,这个单位立方体不存在,标为空;如果相同暂定为存在.
4. 如果六个视图遍历完后没有删除一个单位立方体,循环结束.否则重新遍历六个视图.
5. 对立方体的每个单位立方体进行遍历,除掉不存在的单位立方体,计算出重量.
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++) const int maxn=;
int n;
char pos[maxn][maxn][maxn];
char view[][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){
switch(k){
case :x=len;y=j;z=i;break;
case :x=n--j;y=len;z=i;break;
case :x=n--len;y=n--j;z=i;break;
case :x=j;y=n--len;z=i;break;
case :x=n--i;y=j;z=len;break;
case :x=i;y=j;z=n--len;break;
}
}//表示第k个视图中,第i行第j列、深度为len单位立方体在原立方体中的坐标(x,y,z)
int main(){
while(cin>>n && n){
REP(i,n)REP(k,)REP(j,n)view[k][i][j]=read_char();
REP(i,n)REP(j,n)REP(k,n)pos[i][j][k]='#';
// 如果视图这个位置为空,可以看穿,说明这排是没有单位立方体的.全部标为空.
REP(k,)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,)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;// 两个视图看到的颜色一样.暂定为存在
else{// 两个视图看到的颜色不一样,说明不存在,删掉这个单位立方体.
pos[x][y][z]='.';
done=false;
}
}
}
if(done)break;// 如果遍历六个视图后没有删掉一个单位立方体,可以结束循环.
} int ans=;// 计算重量
REP(i,n)REP(j,n)REP(k,n)
if(pos[i][j][k]!='.')ans++; printf("Maximum weight: %d gram(s)\n",ans);
}return ;
}
[ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]的更多相关文章
- [ACM_模拟][ACM_暴力] Lazier Salesgirl [暴力 懒销售睡觉]
Description Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making ...
- [ACM_数学] LA 3708 Graveyard [墓地雕塑 圈上新加点 找规律]
Description Programming contests became so popular in the year 2397 that the governor of New Earck ...
- [ACM_模拟] POJ1068 Parencodings (两种括号编码转化 规律 模拟)
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...
- [ACM_模拟] ACM - Draw Something Cheat [n个长12的大写字母串,找出交集,按字母序输出]
Description Have you played Draw Something? It's currently one of the hottest social drawing games o ...
- [ACM_模拟] ZOJ 3713 [In 7-bit 特殊输出规则 7bits 16进制]
Very often, especially in programming contests, we treat a sequence of non-whitespace characters as ...
- [ACM_模拟] ZJUT OJ 1139 七龙珠 (追及类问题,s-t图像,模拟)
Description 话说孙悟饭与小林正在与刚造访地球的赛亚人贝吉塔交战,因为连贝吉塔的手下纳巴的实力也远在他俩之上,由于差距悬殊,小林不得不设脱离战场,去寻找正在修炼中的悟空求救,而赛亚人一伙 ...
- [ACM_模拟] ZJUT 1155 爱乐大街的门牌号 (规律 长为n的含k个逆序数的最小字典序)
Description ycc 喜欢古典音乐是一个 ZJUTACM 集训队中大家都知道的事情.为了更方便地聆听音乐,最近 ycc 特意把他的家搬到了爱乐大街(德语Philharmoniker-Stra ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
随机推荐
- JAXB 2.0 API is being loaded from the bootstrap classloader
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个 ...
- 极客DIY:使用树莓派制作一套“NAS+私有云盘+下载机”
原创作者:HackLiu 0×00 前言 如果你家里有多台设备需要联网需要娱乐,你一定会或多或少遇到设备碎片化带来的烦恼.当然,已经有很多厂商包括新晋的小米.360在内的互联网公司做了这个事情 ...
- JS事件冒泡
JavaSciprt事件中有两个很重要的特性:事件冒泡以及目标元素. 事件冒泡: 当一个元素上的事件被触发的时候,比如说鼠标点击了一个按钮,同样的事件将会在那个元素的所有祖先元素中被触发.这 一过程被 ...
- 利用iframe无刷新上传文件的坑
页面里经常要用到文件上传的功能,而且要求页面不刷新,先说一下原理:页面里放一个file控件和submit按钮,外面用form表单包住,给form表单加上对应的属性值,action.method.ent ...
- 剑指offer题目31-40
面试题31:连续字数组的最大和 public class Solution { public int FindGreatestSumOfSubArray(int[] array) { int len ...
- 20145225《Java程序设计》 实验四 Android开发基础
20145225<Java程序设计> 实验四 Android开发基础 实验报告 实验内容 安装Android Studio 运行安卓AVD模拟器 使用安卓运行出虚拟手机并显示HelloWo ...
- angualrjs
$rootScope是angularJS中最接近全局作用域的对象.在$rootScope上附加太多业务逻辑并不是好主意,这与污染JavaScript的全局作用域是一样的. $scope对象就是一个普通 ...
- (必看)ping值不代表网速
在下售卖美国.香港VPN服务器多年,在于客户的交流中,最多关心的就是ping值速度,认为ping速度越低速度越快,以此来评判一台VPN服务器的速度快慢,这其实是一个误区!现在来详细说明下. 1.pin ...
- Eclipse的安装和java环境变量的设置
首先准备工作是要下载好Eclipse和java JDK. 必须要注意的是,Eclipse和java JDK必须下载同一位数的版本,即64位同为64位,32位同为32位.否则在安装完成运行Eclipse ...
- 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
public class Three_03 { public static void main(String[] args) { for(int i=100;i<1000;i++){ int a ...