• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            USACO 2.1.1 The Castle 分析及代碼

            The Castle
            IOI'94 - Day 1

            In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.

            Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.

            Your task is to help Farmer John know the exact room count and sizes.

            The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.

            Consider this annotated floorplan of a castle:

                 1   2   3   4   5   6   7
              #############################
            1 #   |   #   |   #   |   |   #
              #####---#####---#---#####---#
            2 #   #   |   #   #   #   #   #
              #---#####---#####---#####---#
            3 #   |   |   #   #   #   #   #
              #---#########---#####---#---#
            4 # ->#   |   |   |   |   #   #
              #############################
            #  = Wall     -,|  = No wall
            -> = Points to the wall to remove to
            make the largest possible new room
            

            By way of example, this castle sits on a 7 x 4 base. A "room" includes any set of connected "squares" in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).

            Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.

            The castle always has at least two rooms and always has a wall that can be removed.

            PROGRAM NAME: castle

            INPUT FORMAT

            The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

            Each module number tells how many of the four walls exist and is the sum of up to four integers:

            • 1: wall to the west
            • 2: wall to the north
            • 4: wall to the east
            • 8: wall to the south

            Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.
            Line 1: Two space-separated integers: M and N
            Line 2..: M x N integers, several per line.

            SAMPLE INPUT (file castle.in)

                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
                

            OUTPUT FORMAT

            The output contains several lines:
            Line 1: The number of rooms the castle has.
            Line 2: The size of the largest room
            Line 3: The size of the largest room creatable by removing one wall
            Line 4: The single wall to remove to make the largest room possible

            Choose the optimal wall to remove from the set of optimal walls by choosing the wall farthest to the west (and then, if still tied, farthest to the south). Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving the location of the wall with respect to the module.

            SAMPLE OUTPUT (file castle.out)

            5
                        9
                        16
                        4 1 E
                       

            Analysis

            Algorithm:

            Initially, we need to change the bitnumber into an exact wall map. Later, determine the connection of two rooms nearby. In order to determine the room, just use the traditional Floodfill algorithm. Well,everything cames quite easy.

            Code:
                        
            /*
            ID:braytay1
            PROG:castle
            LANG:C++
            */

            #include 
            <fstream>

            using namespace std;

            bool wall[50][50][4];
            int n,m,r_num,size[50*50+1],room[50][50];

            void dfs(int,int);

            int main() {
                ifstream fin (
            "castle.in");
                ofstream fout (
            "castle.out");
                
            int s_max=0,c_max=0,ans_x,ans_y;
                
            char ans_w;
                fin 
            >> n >> m;
                
            for (int i=0;i<m;i++
                    
            for (int j=0;j<n;j++{
                        
            int tem;
                        fin 
            >> tem;
                        
            for (int k=0;k<=3;k++)  wall[i][j][k]=(tem>>k)&1;
                    }

                
            //Initialization
                for (int i=0;i<m;i++
                    
            for (int j=0;j<n;j++
                        
            if (!room[i][j]) {
                            r_num
            ++;
                            size[r_num]
            =0;
                            dfs(i,j);
                            s_max
            =max(s_max,size[r_num]);
                        }

                
            //room mark
                for (int i=0;i<n;i++
                    
            for (int j=m-1;j>=0;j--{
                        
            int a=room[j][i],b=room[j][i<n-1?i+1:0],c=room[j>0?j-1:0][i];
                        
            if (j>0 && wall[j][i][1&& a!=&& size[a]+size[c]>c_max) {
                            c_max
            =size[a]+size[c];
                            ans_x
            =j;
                            ans_y
            =i;
                            ans_w
            ='N';
                        }

                        
            if (i<n-1 && wall[j][i][2&& a!=&& size[a]+size[b]>c_max) {
                            c_max
            =size[a]+size[b];
                            ans_x
            =j;
                            ans_y
            =i;
                            ans_w
            ='E';
                        }

                    }

                
            //break walls
                fout << r_num << endl << s_max << endl << c_max << endl << ans_x+1 << ' ' << ans_y+1 << ' ' << ans_w << endl;
                fin.close();
                fout.close();
                
            return 0;
            }


            void dfs(int x,int y) {
                
            if (room[x][y]==r_num) return;
                room[x][y]
            =r_num;
                size[r_num]
            ++;
                
            if (!wall[x][y][0]) dfs(x,y-1);
                
            if (!wall[x][y][1]) dfs(x-1,y);
                
            if (!wall[x][y][2]) dfs(x,y+1);
                
            if (!wall[x][y][3]) dfs(x+1,y);
            }

            posted on 2008-08-07 14:37 幻浪天空領主 閱讀(406) 評論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲国产成人久久综合区| 亚洲AV日韩精品久久久久| 国产精品美女久久久m| 国产成人久久精品一区二区三区| 国产精品久久久久久影院 | 久久久久黑人强伦姧人妻| 久久夜色撩人精品国产| 无码人妻久久一区二区三区免费丨 | 一本久久知道综合久久| 久久精品成人免费看| 色妞色综合久久夜夜| 国产99久久精品一区二区| 久久综合久久美利坚合众国| 精品久久久久久无码专区| 久久人妻少妇嫩草AV无码蜜桃 | 亚洲国产另类久久久精品黑人| 99久久精品国产免看国产一区| 国产伊人久久| 精品一区二区久久| 亚洲人成伊人成综合网久久久 | 日韩va亚洲va欧美va久久| 色偷偷888欧美精品久久久| 99久久国产综合精品女同图片| 国产精品永久久久久久久久久| 一本久久知道综合久久| 亚洲国产精品成人AV无码久久综合影院| 久久精品国产亚洲AV高清热| 东方aⅴ免费观看久久av| 污污内射久久一区二区欧美日韩 | 亚洲伊人久久大香线蕉苏妲己| 欧美喷潮久久久XXXXx| 国产成人精品综合久久久久| 天天做夜夜做久久做狠狠| 国产成人无码精品久久久久免费 | 久久久久久久人妻无码中文字幕爆| 午夜精品久久久久久| 久久这里只精品99re66| 久久精品无码一区二区WWW| 久久综合色老色| 久久精品国产久精国产一老狼| 久久久久久精品久久久久|