【模拟】UVa 1030 - Image Is Everything
1030 - Image Is Everything
Time limit: 3.000 seconds
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 ( 1N10). 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) 分析:
① “看穿”的位置所对应的所有单位立方体一定都不存在;
② 根据题意,每个单位立方体各面被涂单一的颜色,则根据颜色可辨别单位立方体的存在与否,若前视图的右上角颜色A和顶视图的右下角颜色B不同,那么对应的单位立方体一定不存在;删除该立方体之后,也可能会造成新的矛盾。此处则存在删除次数的问题。 代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn = ;
int n;
char read_char()
{
char ch;
while()
{
ch = getchar();
if((ch >= 'A' && ch <= 'Z') || ch == '.') return ch;
}
}
char view[maxn][maxn][maxn];
char pos[maxn][maxn][maxn];
void get(int k, int i, int j, int len, int& x, int& y, int&z)
{//第k个视图中,第i行j列深度为len对应立方体中的坐标(x, y, z);
if(k == )//前
x = len, y = j, z = i;
if(k == )//左
x = n-j-, y = len, z = i;
if(k == )//后
x = n-len-, y = n-j-, z = i;
if(k == )//右
x = j, y = n-len-, z = i;
if(k == )//顶
x = n-i-, y = j, z = len;
if(k == )//底
x = i, y = j, z = n-len-;
}
int main()
{
while(~scanf("%d", &n) && n)
{
char ch;
for(int i = ; i < n; i ++) //第i行
for(int k = ; k < ; k++) //第j面
for(int j = ; j < n; j++) //第k列
view[k][i][j] = read_char();
for(int i = ; i < n; i ++) //
for(int j = ; j < n; j++)
for(int k = ; k < n; k++)
pos[i][j][k] = '#';
for(int k = ; k < ; k++) //第j面
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
if(view[k][i][j] == '.')
for(int l = ; l < n; l++) //深度len
{
int x, y, z;
get(k, i, j, l, x, y, z);
pos[x][y][z] = '.'; //无单位立方体的地方标志为'.'
} while()
{
bool done = true;
for(int k = ; k < ; k++) //第j面
for(int i = ; i < n; i++) //第i行
for(int j = ; j < n; j++) //第j列
if(view[k][i][j] != '.')
{
for(int l = ; l < n; l++) //深度len — 扫描
{
int x, y, z;
get(k, i, j, l, x, y, z);
if(pos[x][y][z] == '.') //若该单位立方体不存在,深度加1
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 = ;
for(int i = ; i < n; i ++) //
for(int j = ; j < n; j++)
for(int k = ; k < n; k++)
{
if(pos[i][j][k] != '.') ans++;
}
printf("Maximum weight: %d gram(s)\n", ans);
}
return ;
}
其中:
char view[6][maxn][maxn]; //存储各面各位置的颜色
char pos[maxn][maxn][maxn]; //N*N*N,模拟单位立方体的存在与否
初始化
for(int i = ; i < n; i ++) //第i行
for(int k = ; k < ; k++) //第j面
for(int j = ; j < n; j++) //第k列
view[k][i][j] = read_char(); //读入颜色
for(int i = ; i < n; i ++) for(int j = ; j < n; j++)
for(int k = ; k < n; k++)
pos[i][j][k] = '#'; //第i行j列k深的单位立方体初始化为不存在
read_char()函数:
char read_char()
{
char ch;
while()
{
ch = getchar();
if((ch >= 'A' && ch <= 'Z') || ch == '.') return ch;
}
}
主函数:
①程序用了一个get函数来表示第k个视图中,第i行j列深度为len的单位正方体在原立方体中的坐标(x,y,z);
void get(int k, int i, int j, int len, int& x, int& y, int&z)
{//第k个视图中,第i行j列深度为len对应立方体中的坐标(x, y, z);
if(k == )//前
x = len, y = j, z = i;
if(k == )//左
x = n-j-, y = len, z = i;
if(k == )//后
x = n-len-, y = n-j-, z = i;
if(k == )//右
x = j, y = n-len-, z = i;
if(k == )//顶
x = n-i-, y = j, z = len;
if(k == )//底
x = i, y = j, z = n-len-;
}
②删除次数问题.
不难证明,第一次删除是必要的,再利用数学归纳法,假设前k次删除是必要的,且删除立方体之后不能解除矛盾,则第k+1次删除是必要的。
while()
{
bool done = true;
for(int k = ; k < ; k++) //第j面
for(int i = ; i < n; i++) //第i行
for(int j = ; j < n; j++) //第j列
if(view[k][i][j] != '.')
{
for(int l = ; l < n; l++) //深度len — 扫描
{
int x, y, z;
get(k, i, j, l, x, y, z);
if(pos[x][y][z] == '.') //若该单位立方体不存在,深度加1
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;
}
最后输出,即输出pos数组中不是”."的个数。
【模拟】UVa 1030 - Image Is Everything的更多相关文章
- UVA 1030 - Image Is Everything【模拟+思维+迭代更新】
题目链接:uva 1030 - Image Is Everything 题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给 ...
- uva 1030 - Image Is Everything(迭代更新)
题目链接:uva 1030 - Image Is Everything 题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给 ...
- [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_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, where keys ...
- [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- UVA 246 - 10-20-30 (模拟+STL)
UVA 246 - 10-20-30 题目链接 题意:给52张的扑克堆,先从左往右发7张牌,之后连续不断从左往右发7张牌,假设有牌堆形成了下面3种情况(按顺序推断): 1.头两张+尾一张和为10或20 ...
随机推荐
- 面向对象基础3(class0523)
怎么实现多态2-接口 接口是定义一种能力,规定子类能干什么和抽象类有些相似,解决类的单根继承.接口可以实现多继承 案例 鸟-麻雀sparrow,鸵鸟ostrich,企鹅penguin,鹦鹉parrot ...
- [原]我的CentOS设置
[Date]2013-10-06 [Keywords]Linux.CentOS.Fedora [Environment]CentOS 6.3 Final . Wiz [Content]: 1.安装Fl ...
- jquery 资料收集
1.jquery 特效:http://www.yeshou-jquery.com/yeshou/jquery.html 2.jquery 无缝文字滚动效果(跑马灯效果)插件 Marquee(MSCla ...
- 【Hadoop学习】Apache HBase项目简介
正在撰写,稍后来访……
- dataStructure@ Binary Search Tree
#include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...
- Ubuntu 固态硬盘 4K对齐及启用 Trim,及其验证方法
因为之前一个移动硬盘因为坏道蔓延导致没办法继续使用,我略冲动地跑去买了一块 120GB 的三星840 固态硬盘回来.为了使用起来更方便,还去弄了个光驱位硬盘托架,把固态硬盘接在了光驱位与原本的笔记本硬 ...
- 【转】hive简介安装 配置常见问题和例子
原文来自: http://blog.csdn.net/zhumin726/article/details/8027802 1 HIVE概述 Hive是基于Hadoop的一个数据仓库工具,可以将结构化 ...
- 现代程序设计——homework-01
1.我的GitHub用户 首先,接触到现代程序设计这门课之后我才正式开始使用GitHub和它的客户端,以前都是去网站看代码.扒样例.我注册的账户名为:hennande.目前该账户中有我的第一份关于ho ...
- Fragment回调顺序及getActivity()为NullPointerException解决方法
Fragment回调顺序 onAttach->onCreate->onCreateView->onActivityCreated ps:最后发现经常在Fragment里面getAct ...
- jdbc连接的工具类
在不实用框架的情况下,有一个jdbc的工具类来进行数据库的连接就再好不过了,下面提供这个工具类DBUtil.java package org.jdbc.test; import java.io.Inp ...