zoukankan      html  css  js  c++  java
  • USACO Section 2.1 The Castle 解题报告

    题目

    题目描述

    有一个城堡,城堡中有若干个房间,房间与房间之间用墙来进行分隔。现在我们需要统计这个城堡有多少个房间,并且还要找出最大的房间的面积是多少(一个单元格就代表一个单元面积)。城堡的主人现在想要在这些房间中通过打通一面墙来使两个房间合并,并且要求合并之后的房间是所有可能情况中最大的房间。我们需要计算的是,合并之后最大的房间是多大,需要打通的墙壁是哪一面。我们进行房间改造有一定的规则:从城堡的左下角开始往右上角遍历单元格,每个单元格只要考虑两面墙(如果有相应的墙壁),优先考虑朝向为北的墙壁(N),之后再考虑朝向为东的墙壁(E)。

    问题输入

    输入与平常有些不同,我们是每个单元格中有一个数字x。现在我们有如下规定:

    • 1:代表朝向为西的墙壁
    • 2:代表朝向为北的墙壁
    • 4:代表朝向为东的墙壁
    • 8:代表朝向为南的墙壁
      如果x可以由1,2,4,8这几个数中的某些数字相加得到,那就代表这个位置有相应的墙壁。城堡最大为50*50的单元格组成。

    问题输出

    输出房间改造之前的房间数、最大的房间面积。然后输出房间改造之后的最大的房间面积、需要改造的墙壁。

    样例输入

    7 4
    11 6 11 6 3 10 6
    7 9 6 13 5 15 5
    1 10 12 7 13 7 5
    13 11 10 8 10 12 13
    

    样例输出

    5
    9
    16
    4 1 E
    

    解题思路

    既然是要按照一定的规则输出最优的方案,我们肯定是需要遍历所有可能的情况。所以我最一开始就直接用宽搜找到所有的房间,并且统计每个房间的大小。之后我再枚举每一面墙壁,在枚举墙壁的时候思维比较混乱,因为之前统计房间我没有对房间进行编号,并且没有保存每个房间的面积。所以在枚举墙壁的时候我又用洪泛式的搜索来计算去掉这面墙壁之后可以增加多大的面积。最后的结果当然是超时啦......
    所以后来看了下别人的思路提示,才想起来应该在统计房间大小的时候顺便对房间进行编号,而且还要对房间的大小进行记录,便于之后枚举墙壁的时候直接可以获得这些信息。
    在解题的过程中我们可以发现墙壁的方向是1,2,4,8,这是2的幂次方,所以我们可以用位运算的技巧来判断每个单元格的墙壁情况。这是一个解题技巧,可以让程序变得更加优雅。

    解题代码

    /*
    ID: yinzong2
    PROG: castle
    LANG: C++11
    */
    #define MARK
    #include <iostream>
    #include <map>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int maxn = 55;
    
    int M, N;
    // castle存地图,roomId用来保存每个点所属的房间编号
    int castle[maxn][maxn], roomId[maxn][maxn];
    map<int, int> roomSize; // 房间号映射到房间大小
    int maxSize1, maxSize2;
    
    int dirX[4] = {0, -1, 0, 1};
    int dirY[4] = {-1, 0, 1, 0};
    
    struct Room {
        int x,y;
        int value;
    };
    
    void findRoom(int x, int y, int roomNum) {
        int rSize = 0;
        queue<Room> Q;
        while (!Q.empty())Q.pop();
        Room r;
        r.x = x; r.y = y; r.value = castle[x][y];
        Q.push(r);
        roomId[x][y] = roomNum;
        rSize++;
        while (!Q.empty()) {
            Room r1, r2;
            r1 = Q.front(); Q.pop();
            for (int i = 0; i < 4; ++i) {
                int dir = 1 << i;
                if (0 == (dir&r1.value)) {
                    int tx = r1.x + dirX[i];
                    int ty = r1.y + dirY[i];
                    if (tx >= 1 && tx <= N && ty >= 1 && ty <= M && 0 == roomId[tx][ty]) {
                        roomId[tx][ty] = roomNum;
                        r2.x = tx;
                        r2.y = ty;
                        r2.value = castle[tx][ty];
                        rSize++;
                        Q.push(r2);
                    }
                }
            }
        }
        maxSize1 = max(maxSize1, rSize);
        roomSize[roomNum] = rSize;
    }
    
    int main() {
    #ifdef MARK
        freopen("castle.in", "r", stdin);
        freopen("castle.out", "w", stdout);
    #endif // MARK
        while (cin >> M >> N) {
            for (int i = 1; i <= N; ++i) {
                for (int j = 1; j <= M; ++j) {
                    cin >> castle[i][j];
                    roomId[i][j] = 0;
                }
            }
            maxSize1 = 0;
            int roomNum = 1;
            roomSize.clear();
            for (int i = 1; i <= N; ++i) {
                for (int j = 1; j <= M; ++j) {
                    if (0 == roomId[i][j]) {
                        findRoom(i, j, roomNum);
                        roomNum++;
                    }
                }
            }
            cout << roomNum-1 << endl << maxSize1 << endl;
            // 枚举所有的墙壁,房间从左下往右上方向,墙壁优先选择N,其次为E
            maxSize2 = 0;
            int wallX, wallY;
            char wallDir;
            for (int y = 1; y <= M; ++y) {
                for (int x = N; x >= 1; --x) {
                    int value = castle[x][y];
                    // N 方向
                    if ((value & 2) != 0) {
                        int tx = x + dirX[1];
                        int ty = y + dirY[1];
                        if (tx >= 1 && tx <= N && ty >= 1 && ty <= M && roomId[x][y] != roomId[tx][ty]) {
                            int totSize = roomSize[ roomId[x][y] ] + roomSize[ roomId[tx][ty] ];
                            if (totSize > maxSize2) {
                                maxSize2 = totSize;
                                wallX = x;
                                wallY = y;
                                wallDir = 'N';
                            }
                        }
                    }
                    // E 方向
                    if ((value & 4) != 0) {
                        int tx = x + dirX[2];
                        int ty = y + dirY[2];
                        if (tx >= 1 && tx <= N && ty >= 1 && ty <= M && roomId[x][y] != roomId[tx][ty]) {
                            int totSize = roomSize[ roomId[x][y] ] + roomSize[ roomId[tx][ty] ];
                            if (totSize > maxSize2) {
                                maxSize2 = totSize;
                                wallX = x;
                                wallY = y;
                                wallDir = 'E';
                            }
                        }
                    }
                }
            }
            cout << maxSize2 << endl;
            cout << wallX << " " << wallY << " " << wallDir << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/yinzm/p/7466942.html
Copyright © 2011-2022 走看看