zoukankan      html  css  js  c++  java
  • USACO 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 module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose 'N' before 'E'. 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
    这其中我们可以看出来可以分成两部分求解,第一部分先求解城堡的房间数目和最大的房间的大小。
    首先我们可以看出可以用图来解决的。这里我一开始是把每个房间当成一个点,每个墙当成一条边,后来发现并没有用到点,于是就删掉了。
    这里的边就是我代码里面的wall,wall是一个三维数组,前两位是表示坐标,第三位是表示方向。可以看出来0w,1n,2e,3s(第0号元素表示west方向,诸如此类)。
    这里的顺序是算出来的。具体是这样的,第一次%2得到的值是用来判断是否是2的倍数,如果是2的倍数,则表示不含有1,
    同理,在temp除以2之后,temp%2的值就表示是否是4的倍数,如果是4的倍数,则不含2……同理推下去。
    此时把wall[i][j][4]赋值为0,为的是之后给这个变量赋值颜色。
    此时我们对所有的元素遍历,进行flood_fill。
    至此前两问就解决了。

    至于问合并后的房间大小,这个也很好解决。由于考虑到优先级的问题,所以这里遍历的顺序需要注意一下,另外只需要看两个方向就好了。

    我认为本题的主要难点在于对于x,y不要写反掉。
    最后放一下代码。
      1 /**
      2 ID: njuwz151
      3 TASK: castle
      4 LANG: C++
      5 **/
      6 #include <bits/stdc++.h>
      7 
      8 #define MAXN 55
      9 
     10 using namespace std;
     11 
     12 int wall[MAXN][MAXN][5] = {0};
     13 bool visit[MAXN][MAXN] = {false};
     14 int m, n;
     15 int color = 1;
     16 int roomSize[MAXN * MAXN] = {0};
     17 int dx[] = {0, -1, 0, 1};
     18 int dy[] = {-1, 0, 1, 0};
     19 int max_after = -1;
     20 int x_a;
     21 int y_a;
     22 char dir; 
     23 
     24 void flood_fill(int x, int y);
     25 
     26 void merge(int x, int y);
     27 
     28 int main() {
     29     freopen("castle.in", "r", stdin);
     30     freopen("castle.out", "w", stdout);
     31 
     32     cin >> m >> n;
     33     int t;
     34     for(int i = 0; i < n; i++) {
     35         for(int j = 0; j < m; j++) {
     36             cin >> t;
     37             /**
     38             1: wall to the west
     39             2: wall to the north
     40             4: wall to the east
     41             8: wall to the south
     42             */
     43             for(int k = 0; k < 4; k++) {
     44 //                cout << i << " " << j << " " << k << " " << t%2 << endl;
     45                 wall[i][j][k] = t%2;
     46                 t /= 2;
     47             }
     48             wall[i][j][4] = 0;
     49         }
     50     }
     51     
     52     for(int i = 0; i < n; i++) {
     53         for(int j = 0; j < m; j++) {
     54             if(!visit[i][j]) {
     55                 roomSize[color] = 0;
     56                 flood_fill(i, j);
     57                 color++;
     58             }
     59         }
     60     }
     61     int max = 0;
     62     for(int i = 1; i < color; i++) {
     63         if(roomSize[i] > max) {
     64             max = roomSize[i];
     65         }
     66 //        cout << roomSize[i] << " ";
     67     }
     68     cout << color - 1 << endl;
     69     cout << max << endl;
     70     
     71     
     72     for(int i = 0; i < m; i++) {
     73         for(int j = n-1; j >= 0; j--) {
     74             merge(j, i);
     75         }
     76     }
     77     cout << max_after << endl;
     78     cout << x_a+1 << " " << y_a+1 << " " << dir << endl;
     79 } 
     80 
     81 void flood_fill(int x, int y) {
     82     visit[x][y] = true;
     83     roomSize[color]++;
     84     wall[x][y][4] = color;
     85     for(int i = 0; i < 4; i++) {
     86         int nx = x + dx[i];
     87         int ny = y + dy[i];
     88         if(nx < 0 || ny < 0) {
     89             continue;
     90         }
     91         if(nx >= n || ny >= m) {
     92             continue;
     93         }
     94         if(visit[nx][ny]) {
     95             continue;
     96         }
     97         if(wall[x][y][i]) {
     98             continue;
     99         }
    100         flood_fill(nx, ny);
    101     }
    102 
    103 }
    104 
    105 void merge(int x, int y) {
    106     int sum;
    107     if(x - 1 >= 0 && wall[x][y][4] != wall[x-1][y][4]) {
    108         sum = roomSize[wall[x][y][4]] + roomSize[wall[x-1][y][4]];
    109         if(sum > max_after) {
    110             max_after = sum;
    111             x_a = x;
    112             y_a = y;
    113             dir = 'N';
    114         }
    115     }
    116     if(y + 1 < m && wall[x][y][4] != wall[x][y+1][4]) {
    117         sum = roomSize[wall[x][y][4]] + roomSize[wall[x][y+1][4]];
    118         if(sum > max_after) {
    119             max_after = sum;
    120             x_a = x;
    121             y_a = y;
    122             dir = 'E';
    123         }
    124     }
    125 }
    castle
     

     

  • 相关阅读:
    hdu6060[贪心+dfs] 2017多校3
    Codeforces 547B. Mike and Feet[单调栈/队列]
    Codeforces 545E. Paths and Trees[最短路+贪心]
    gitignore使用
    es学习
    google浏览器安装jsonview
    sychronized关键字底层详解加锁升级过程
    idea 中 “XXX has broken path” 错误解决
    kafka高并发读写的原因
    window redis版本 安装
  • 原文地址:https://www.cnblogs.com/NJUWZ/p/7045841.html
Copyright © 2011-2022 走看看