zoukankan      html  css  js  c++  java
  • 【模拟】UVa 1030

    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$ le$N$ le$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不同,那么对应的单位立方体一定不存在;删除该立方体之后,也可能会造成新的矛盾。此处则存在删除次数的问题。

    代码如下:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 using namespace std;
     6 const int maxn = 11;
     7 int n;
     8 char read_char()
     9 {
    10     char ch;
    11     while(1)
    12     {
    13         ch = getchar();
    14         if((ch >= 'A' && ch <= 'Z') || ch == '.') return ch;
    15     }
    16 }
    17 char view[maxn][maxn][maxn];
    18 char pos[maxn][maxn][maxn];
    19 void get(int k, int i, int j, int len, int& x, int& y, int&z)
    20 {//第k个视图中,第i行j列深度为len对应立方体中的坐标(x, y, z);
    21     if(k == 0)//
    22         x = len, y = j, z = i;
    23     if(k == 1)//
    24         x = n-j-1, y = len, z = i;
    25     if(k == 2)//
    26         x = n-len-1, y = n-j-1, z = i;
    27     if(k == 3)//
    28         x = j, y = n-len-1, z = i;
    29     if(k == 4)//
    30         x = n-i-1, y = j, z = len;
    31     if(k == 5)//
    32         x = i, y = j, z = n-len-1;
    33 }
    34 int main()
    35 {
    36     while(~scanf("%d", &n) && n)
    37     {
    38         char ch;
    39         for(int i = 0; i < n; i ++) //第i行
    40             for(int k = 0; k < 6; k++) //第j面
    41                 for(int j = 0; j < n; j++) //第k列
    42                     view[k][i][j] = read_char();
    43         for(int i = 0; i < n; i ++) //
    44             for(int j = 0; j < n; j++)
    45                 for(int k = 0; k < n; k++)
    46                     pos[i][j][k] = '#';
    47         for(int k = 0; k < 6; k++) //第j面
    48             for(int i = 0; i < n; i++)
    49                 for(int j = 0; j < n; j++)
    50                     if(view[k][i][j] == '.')
    51                         for(int l = 0; l < n; l++) //深度len
    52                         {
    53                             int x, y, z;
    54                             get(k, i, j, l, x, y, z);
    55                             pos[x][y][z] = '.'; //无单位立方体的地方标志为'.'
    56                         }
    57 
    58         while(1)
    59         {
    60             bool done = true;
    61             for(int k = 0; k < 6; k++) //第j面
    62                 for(int i = 0; i < n; i++) //第i行
    63                     for(int j = 0; j < n; j++) //第j列
    64                         if(view[k][i][j] != '.')
    65                         {
    66                             for(int l = 0; l < n; l++) //深度len — 扫描
    67                             {
    68                                 int x, y, z;
    69                                 get(k, i, j, l, x, y, z);
    70                                 if(pos[x][y][z] == '.') //若该单位立方体不存在,深度加1
    71                                     continue;
    72                                 if(pos[x][y][z] == '#') //若该单位立方体存在但为初始状态,则更改为即给颜色(此主要
    73                                 {                       //是为了判断不同面颜色是否相等,若相等则存在立方体,否则不存在
    74                                     pos[x][y][z] = view[k][i][j];
    75                                     break;
    76                                 }
    77                                 if(pos[x][y][z] == view[k][i][j]) //存在
    78                                     break;
    79                                 pos[x][y][z] = '.'; //不存在
    80                                 done = false;
    81                             }
    82                         }
    83             if(done) break;
    84         }
    85         int ans = 0;
    86         for(int i = 0; i < n; i ++) //
    87             for(int j = 0; j < n; j++)
    88                 for(int k = 0; k < n; k++)
    89                 {
    90                     if(pos[i][j][k] != '.') ans++;
    91                 }
    92         printf("Maximum weight: %d gram(s)
    ", ans);
    93     }
    94     return 0;
    95 }
    View Code

    其中:

    char view[6][maxn][maxn]; //存储各面各位置的颜色  
    char pos[maxn][maxn][maxn]; //N*N*N,模拟单位立方体的存在与否

    初始化

    1 for(int i = 0; i < n; i ++) //第i行
    2     for(int k = 0; k < 6; k++) //第j面
    3         for(int j = 0; j < n; j++) //第k列
    4             view[k][i][j] = read_char(); //读入颜色
    5 for(int i = 0; i < n; i ++)6     for(int j = 0; j < n; j++)
    7         for(int k = 0; k < n; k++)
    8             pos[i][j][k] = '#'; //第i行j列k深的单位立方体初始化为不存在

    read_char()函数:

    1 char read_char()
    2 {
    3     char ch;
    4     while(1)
    5     {
    6         ch = getchar();
    7         if((ch >= 'A' && ch <= 'Z') || ch == '.') return ch;
    8     }
    9 }

    主函数:

    ①程序用了一个get函数来表示第k个视图中,第i行j列深度为len的单位正方体在原立方体中的坐标(x,y,z);

     1 void get(int k, int i, int j, int len, int& x, int& y, int&z)
     2 {//第k个视图中,第i行j列深度为len对应立方体中的坐标(x, y, z);
     3     if(k == 0)//
     4         x = len, y = j, z = i;
     5     if(k == 1)//
     6         x = n-j-1, y = len, z = i;
     7     if(k == 2)//
     8         x = n-len-1, y = n-j-1, z = i;
     9     if(k == 3)//
    10         x = j, y = n-len-1, z = i;
    11     if(k == 4)//
    12         x = n-i-1, y = j, z = len;
    13     if(k == 5)//
    14         x = i, y = j, z = n-len-1;
    15 }

    ②删除次数问题.

    不难证明,第一次删除是必要的,再利用数学归纳法,假设前k次删除是必要的,且删除立方体之后不能解除矛盾,则第k+1次删除是必要的。

    while(1)
    {
        bool done = true;
        for(int k = 0; k < 6; k++) //第j面
            for(int i = 0; i < n; i++) //第i行
                for(int j = 0; j < n; j++) //第j列
                    if(view[k][i][j] != '.')
                    {
                        for(int l = 0; 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数组中不是”."的个数。

  • 相关阅读:
    hdu 2527:Safe Or Unsafe(数据结构,哈夫曼树,求WPL)
    hdu 2019:数列有序!(数据结构,直接插入排序+折半插入排序)
    hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
    hdu 3336:Count the string(数据结构,串,KMP算法)
    hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
    hdu 2141:Can you find it?(数据结构,二分查找)
    hdu 1232:畅通工程(数据结构,树,并查集)
    hdu 2025:查找最大元素(水题,顺序查找)
    hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)
    hdu 1174:爆头(计算几何,三维叉积求点到线的距离)
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4299963.html
Copyright © 2011-2022 走看看