uva1030 - Image Is Everything 六视图方块 模拟

本文介绍了一款用于推断物体最大可能重量的程序,该程序通过分析物体六个方向的颜色视图来判断哪些立方体可能存在,进而计算出物体的最大重量。
部署运行你感兴趣的模型镜像

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. Each1×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 containingN, which is the size of the object (1$ \le$N$ \le$10). The nextN 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

  训练指南的例题,感觉好难,不看他给的代码就不会做。

  有N*N的方块,是由1*1的小方块组成,每个小方块的6个面颜色一样。现在可能有一些小方块不存在,给你6个方向的颜色的视图,.表示这个位置颜色不存在,问最多有多少个小方块。

  关键是如果同一个小方块从两个方向看颜色不一样,那么这个小方块一定不存在。如果视图上一个点没颜色,说明从这个点水平看过去的一条都没有小方块。

  先假设有个新的N*N的立方体,没有涂颜色,并且每一个小方块都存在,然后再根据题目给的视图删掉矛盾的小方块,直到最后没有矛盾了,答案就出来了。

  找矛盾的方法:用view[k][i][j]表示k视图第i行第j列的颜色,pos[x][y][z]表示自己那个新的立方体坐标为x,y,z的小立方体的颜色。循环找出所有view[k][i][j]!='.'的地方,找到当前view[k][i][j]对应看到的新立方体的x,y,z的小立方体,如果小立方体没有染颜色,就染上,退出深度这一层循环(不矛盾)。如果小立方体颜色和view的颜色一样,退出深度的循环(不矛盾),不矛盾说明目前这个小立方体存在,深度就不能再增加了,view看到的就是它。如果小立方体的颜色和view的颜色不一样,矛盾,就把这个小立方体从新立方体中删掉。

  这道题不知道为什么用memset初始化pos为‘#’  WA了,循环赋值就过了。。


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define INF 0x3f3f3f3f
#define MAXN 15
#define MAXM 20010
#define eps 1e-9
#define pii pair<int,int>
using namespace std;
int N;
char pos[MAXN][MAXN][MAXN],view[6][MAXN][MAXN];
char read_char(){
    char c;
    while(1){
        c=getchar();
        if(isupper(c)||c=='.') return c;
    }
}
void get(int &x,int &y,int &z,int k,int i,int j,int p){  //对应转化
    if(k==1){
        x=N-p+1;
        y=j;
        z=N-i+1;
    }
    if(k==2){
        x=j;
        y=p;
        z=N-i+1;
    }
    if(k==3){
        x=p;
        y=N-j+1;
        z=N-i+1;
    }
    if(k==4){
        x=N-j+1;
        y=N-p+1;
        z=N-i+1;
    }
    if(k==5){
        x=i;
        y=j;
        z=N-p+1;
    }
    if(k==6){
        x=N-i+1;
        y=j;
        z=p;
    }
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N),N){
        int i,j,k,x,y,z,p;
        for(i=1;i<=N;i++)
            for(k=1;k<=6;k++)
                for(j=1;j<=N;j++) view[k][i][j]=read_char();
        for(x=1;x<=N;x++)       //给pos初始化,#代表没颜色,不知道为什么memset不行
            for(y=1;y<=N;y++)
                for(z=1;z<=N;z++) pos[x][y][z]='#';
        for(k=1;k<=6;k++)                  //删掉一开始就确定不存在的点
            for(i=1;i<=N;i++)
                for(j=1;j<=N;j++) if(view[k][i][j]=='.'){
                    for(p=1;p<=N;p++){
                        get(x,y,z,k,i,j,p);
                        pos[x][y][z]='.';
                    }
                }
        while(1){                //循环找矛盾
            int flag=1;
            for(k=1;k<=6;k++)
                for(i=1;i<=N;i++)
                    for(j=1;j<=N;j++) if(view[k][i][j]!='.'){
                        for(p=1;p<=N;p++){
                            get(x,y,z,k,i,j,p);
                            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;
                            pos[x][y][z]='.';
                            flag=0;
                        }
                    }
            if(flag) break;        //当前view一整个循环都找不到矛盾
        }
        int ans=0;
        for(x=1;x<=N;x++)
            for(y=1;y<=N;y++)
                for(z=1;z<=N;z++) if(pos[x][y][z]!='.') ans++;
        printf("Maximum weight: %d gram(s)\n",ans);
    }
    return 0;
}






您可能感兴趣的与本文相关的镜像

Langchain-Chatchat

Langchain-Chatchat

AI应用
Langchain

Langchain-Chatchat 是一个基于 ChatGLM 等大语言模型和 Langchain 应用框架实现的开源项目,旨在构建一个可以离线部署的本地知识库问答系统。它通过检索增强生成 (RAG) 的方法,让用户能够以自然语言与本地文件、数据库或搜索引擎进行交互,并支持多种大模型和向量数据库的集成,以及提供 WebUI 和 API 服务

【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值