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的更多相关文章

  1. UVA 1030 - Image Is Everything【模拟+思维+迭代更新】

    题目链接:uva 1030 - Image Is Everything 题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给 ...

  2. uva 1030 - Image Is Everything(迭代更新)

    题目链接:uva 1030 - Image Is Everything 题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给 ...

  3. [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 ...

  4. [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]

      Updating a Dictionary  In this problem, a dictionary is collection of key-value pairs, where keys ...

  5. [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]

      Robot Instructions  You have a robot standing on the origin of x axis. The robot will be given som ...

  6. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...

  7. uva 133 The Dole Queue 双向约瑟夫环 模拟实现

    双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...

  8. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  9. UVA 246 - 10-20-30 (模拟+STL)

    UVA 246 - 10-20-30 题目链接 题意:给52张的扑克堆,先从左往右发7张牌,之后连续不断从左往右发7张牌,假设有牌堆形成了下面3种情况(按顺序推断): 1.头两张+尾一张和为10或20 ...

随机推荐

  1. 如何理解Stay hungry,stay foolish?

    People know about this words because of Steve Jobs.Me too. Hungry,对知识我们一般不会用hungry,我们会用curious,什么时候我 ...

  2. 数组(Array)

    1. 数组(Array):相同类型数据的集合就叫做数组. 2. 数组的定义与赋值(系统会默认初始化) 普通数组: package com.li; public class Array{ public ...

  3. [坑]c#中double转字符串精度丢失问题记录

    在项目遇到了一个比较大的double值,然后出现了一些意想不到的状况: double b=1141.161994934082; b.ToString();//'1141.16199493408' 然后 ...

  4. 配置OpenStack以使用LDAP实现身份管理

    本文展示了如何配置 Keystone,以便使用轻量级目录http://www.aliyun.com/zixun/aggregation/34570.html">访问协议( LDAP)服 ...

  5. VS2010 error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“const char [3]”转换为“LPCTSTR”

    VS2010 (VC2010)建立工程时默认的字符集是Unicode,所以在代码中使用MessageBox时需要输入Unicode,用TEXT()这个宏输入参数,否则会报错:   代码:Message ...

  6. IAR stm8带库的工程模板

    下载:http://pan.baidu.com/share/link?shareid=2243555626&uk=2483252218

  7. Struts2中DMI(动态方法调用)的错误问题(There is no Action mapped for namespace [/xxx] and action name [xxx!yyy] a)

    默认的Struts.xml中是这样的 <constant name="struts.enable.DynamicMethodInvocation" value="f ...

  8. Android最新锁屏病毒分析及解锁

    一.情况简介   从去年开始PC端的敲诈者类病毒在不断的爆发,今年年初的时候手机上也开始出现了敲诈者之类的病毒,对这类病毒很无语也是趋势,因为很多时候病毒的产生是和金钱利益相关的.前天去吾爱破解论坛病 ...

  9. 正确导入svn拉取的工程

    为什么要写这篇博文?主要是记录摸着石头过黄河的过程.之前在eclipse装了svn插件,拉取远程工程,在eclipse显示的工程,并不会分开显示模块工程,反而 是以总工程的姿态呈现.或许你觉得不管分模 ...

  10. Lync边缘服务器配置

    以下步骤均使用Lync管理员权限即可完成 1.在前端下载并编辑拓扑,新建边缘池 如果边缘池中只有一台服务器,则池名称与服务器名称相同,如下: 如果需要删除边缘池,则需要先取消关联,如下: 2.发布拓扑 ...