【模拟】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 ...
随机推荐
- Gdb 常用命令
命令名称 含义 示例 b fun_name 设置断点 b main b 行号 if 条件 设置带条件断点 如:b 11 if i==10 n 下一行 n s 跳入函数内部 s sum fin ...
- Newtonsoft.Json.dll使用
1:Newtonsoft.Json.dll 下载 http://json.codeplex.com/ 2:解析JSON字符窜 方法1: using Newtonsoft.Json; using Sy ...
- CentOS上安装MySQL
1.准备RPM安装包 MySQL-server-5.6.33-1.linux_glibc2.5.x86_64 MySQL-client-5.6.33-1.linux_glibc2.5.x86_64 2 ...
- 中断——中断描述符表的定义和初始化(一) (基于3.16-rc4)
1.中断描述符表的定义(arch/x86/kernel/traps.c) gate_desc debug_idt_table[NR_VECTORS] __page_aligned_bss; 定义的描述 ...
- Zend studio注册码
Zend studio 7.1 注册码 username:lisijie_orgLicense Key:3F4F495657BF3F4A95657BF3 Zend studio 8 注册码(适用于7. ...
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
- 搭建Discuz! (mysql+apache+Discuz! )
0. 配置环境 0.0 安装apache 0.1 安装php 1.数据库准备 1.1 创建数据库用户 1.2 创建discuz使用的数据库(编码:utf8-general-ci) 1.3 把1.2创建 ...
- tomcat 7 用户设置
在tomcat/conf/tomcat-users.xml加入如下脚本就可以了 <role rolename="admin-gui"/> <role rolena ...
- [Objective-c 基础 - 2.1] 封装
A.封装内部细节,根据需求暴露方法 #import <Foundation/Foundation.h> @interface Student : NSObject { int age; } ...
- hdu 4612 (双联通+树形DP)
加一条边后最少还有多少个桥,先Tarjan双联通缩点, 然后建树,求出树的直径,在直径起点终点加一条边去的桥最多, #pragma comment(linker, "/STACK:10240 ...