【模拟】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 ( 1
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
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 ...
随机推荐
- 第一个App“今日材料报价”上架,记录一下【原】
App Store地址:https://itunes.apple.com/us/app/jin-ri-cai-liao-bao-jia/id967274552?l=zh&ls=1&mt ...
- Weblogic常见故障一:JDBC Connection Pools
最近系统老是出现数据库连接池不够用的问题,由于weblogic数据源里没有配置JDBC连接数,取的是默认值15,太小导致connection耗尽,是不是就报一堆错.后来通过修改WebLogic数据源配 ...
- Mac窗口管理管理软件SizeUp
一.SizeUp 是一款 Mac窗口管理管理软件.借助SizeUp,可以快速变化窗口大小(最大化.最小化),可以快速切换窗口的不同位置. 尤其在双显示器,更是扮演者方便.高效.好用的角色,提供了快速切 ...
- iOS相机权限、相册权限、定位权限判断
1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author = [ALAsse ...
- ios 说一说UINavigationController 的堆栈
iOS的界面堆栈管理比android的要好用很多. 这里写两点:一点是 如何重回前面的vc,而不是push一个alloc的新界面.第二点就是判断当前堆栈显示的vc是何vc. vc堆栈: vc1-> ...
- .@RequestMapping 使用方法
1.@RequestMapping 使用方法 SpringMVC中,@RequestMapping用来处理请求,比方XXX.do @RequestMapping("/aaa") ...
- JS 添加千分位,测试可以使用
JS 添加千分位,测试可以使用 <script language="javascript" type="text/javascript">funct ...
- LVS DR模型
1,环境 VMWare10, CentOS6.3 2,LVS DR网络规划 所有机器都只需要一张网卡,给Director的eth0网卡起个别名eth0:1即VIP的值:给RealServer的lo网卡 ...
- [AngularJS - app] AngularJS Location-picker app
From: http://rangle.io/blog/two-ways-to-build-a-location-picker-for-a-mobile-angularjs-application/ ...
- iOS开发——面试笔试精华(四)
面试笔试精华(四) 1. Object-C有多继承吗?没有的话用什么代替? 1> OC是单继承,没有多继承 2> 有时可以用分类和协议来代替多继承 2. ...