<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 97,  comments - 93,  trackbacks - 0
    Problem Statement

    People enjoy mazes, but they also get them dirty. Arrows, graffiti, and chewing gum are just a few of the souvenirs people leave on the walls. You, the maze keeper, are assigned to whiten the maze walls. Each face of the wall requires one liter of paint, but you are only required to paint visible faces. You are given a map of the maze, and you must determine the amount of paint needed for the job.
    The maze is described by a String[] maze, where each character can be either '#' (a wall) or '.' (an empty space). All '.' characters on the perimeter of the map are considered entrances to the maze. Upon entering the maze, one can only move horizontally and vertically through empty spaces, and areas that are not reachable by these movements are not considered visible. Each '#' represents a square block with four wall faces (each side of the square is a face). A face is visible if it is not directly adjacent to another wall (and is in a reachable area of the maze). For example, two adjacent blocks can have at most six visible faces since two of their faces are directly adjacent to each other. All exterior faces on the perimeter are considered visible.
    For example, the following picture represents a trivial maze with just one (wide) entrance and only four empty reachable spaces: 
     
    To whiten this maze you must paint the faces highlighted in yellow above: 16 for its perimeter, plus 8 interior faces. Note that there are faces that are not visible and thus need not be painted.
    Definition

    Class:
    TroytownKeeper
    Method:
    limeLiters
    Parameters:
    String[]
    Returns:
    int
    Method signature:
    int limeLiters(String[] maze)
    (be sure your method is public)


    Constraints
    -
    maze will contain between 1 and 50 elements, inclusive.
    -
    Each element of maze will contain between 1 and 50 characters, inclusive.
    -
    All elements of maze will have the same number of characters.
    -
    All characters in maze will be either '.' or '#'.
    Examples
    0)


    {"##..#"
    ,"#.#.#"
    ,"#.#.#"
    ,"#####"}
    Returns: 24
    Example from the problem statement.
    1)


    {"##",
     "##"}
    Returns: 8
    Only the perimeter of the maze (which has no interior!) has to be painted.
    2)


    {"######"
    ,"#....."
    ,"#.####"
    ,"#.#..#"
    ,"#.##.#"
    ,"#....#"
    ,"######"}
    Returns: 56

    3)


    {"######"
    ,"#....."
    ,"#..#.."
    ,"#....."
    ,"######"}
    Returns: 36

    4)


    {"#.#.#.#"
    ,".#.#.#."
    ,"#.#.#.#"
    ,".#.#.#."}
    Returns: 36
    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.


     1 /**
     2  * @author Nicky Qu
     3  * All Rights Reserved. Oct 21th, 2007
     4  */
     5 public class TroytownKeeper {
     6 
     7     private int mazeRows,mazeCols,elementsSum,blankSpace,temp;
     8     private char[][] charDimensionArray;
     9     private int[][] d;
    10 
    11     public int limeLiters(String[] maze) {
    12         mazeRows = maze.length;
    13         mazeCols = maze[0].length();
    14         elementsSum = 2 * (mazeRows + mazeCols);
    15         charDimensionArray = new char[mazeRows][mazeCols];
    16         d = new int[mazeRows][mazeCols];
    17 
    18         for (int i = 0; i < mazeRows; i++) {
    19             charDimensionArray[i] = maze[i].toCharArray();
    20         }
    21 
    22         for (int i = 0; i < mazeRows; i++) {
    23             for (int j = 0; j < mazeCols; j++) {
    24                 if (charDimensionArray[i][j] == '.') {
    25                     blankSpace += 1;
    26                 }
    27                 if (charDimensionArray[i][j] == '.' && (i == 0 || i == mazeRows - 1 || j == 0 || j == mazeCols - 1&& d[i][j] == 0) {
    28                     d[i][j] = 1;
    29                     elementsSum -= 1;
    30                     if ((i == 0 && j == 0|| (i == 0 && j == mazeCols - 1|| (i == mazeRows - 1 && j == 0|| (i == mazeRows - 1 && j == mazeCols - 1)) {
    31                         elementsSum -= 1;
    32                     }
    33                     if ((mazeRows - 1 == 0|| (mazeCols - 1 == 0)) {
    34                         elementsSum -= 1;
    35                     }
    36                 }
    37             }
    38         }
    39         for (int l = 0; l < blankSpace; l++) {
    40             for (int i = 0; i < mazeRows; i++) {
    41                 for (int j = 0; j < mazeCols; j++) {
    42                     if (charDimensionArray[i][j] == '.' && d[i][j] == 1) {
    43                         temp = i;
    44                         while (temp > 0 && charDimensionArray[temp - 1][j] == '.' && d[temp - 1][j] == 0) {
    45                             d[temp - 1][j] = 1;
    46                             temp--;
    47                         }
    48                         temp = i;
    49                         while (temp < mazeRows - 1 && charDimensionArray[temp + 1][j] == '.' && d[temp + 1][j] == 0) {
    50                             d[temp + 1][j] = 1;
    51                             temp++;
    52                         }
    53                         temp = j;
    54                         while (temp > 0 && charDimensionArray[i][temp - 1== '.' && d[i][temp - 1== 0) {
    55                             d[i][temp - 1= 1;
    56                             temp--;
    57                         }
    58                         temp = j;
    59                         while (temp < mazeCols - 1 && charDimensionArray[i][temp + 1== '.' && d[i][temp + 1== 0) {
    60                             d[i][temp + 1= 1;
    61                             temp++;
    62                         }
    63                     }
    64                 }
    65             }
    66         }
    67 
    68         for (int i = 0; i < mazeRows; i++) {
    69             for (int j = 0; j < mazeCols; j++) {
    70                 if (d[i][j] == 1) {
    71                     if (i > 0 && charDimensionArray[i - 1][j] == '#') {
    72                         elementsSum += 1;
    73                     }
    74                     if (i < mazeRows - 1 && charDimensionArray[i + 1][j] == '#') {
    75                         elementsSum += 1;
    76                     }
    77                     if (j > 0 && charDimensionArray[i][j - 1== '#') {
    78                         elementsSum += 1;
    79                     }
    80                     if (j < mazeCols - 1 && charDimensionArray[i][j + 1== '#') {
    81                         elementsSum += 1;
    82                     }
    83                 }
    84             }
    85         }
    86 
    87         return elementsSum;
    88     }
    89 }


    posted on 2007-10-21 21:28 wqwqwqwqwq 閱讀(1460) 評論(1)  編輯  收藏 所屬分類: Data Structure && Algorithm

    FeedBack:
    # re: TopCoder TCHS3
    2007-10-22 14:02 | 曲強 Nicky
    public class TroytownKeeper {
    char[][] maze;
    boolean[][] visited;
    int ct;
    public int limitLiters(String[] Smaze){
    ct = 0;
    maze = new char[Smaze.length+2][Smaze[0].length()+2];
    visited = new boolean[maze.length][maze[0].length];
    for(int i = 0;i<maze.length;i++)
    if(i == 0|| i == maze.length-1)
    maze[i] = (Smaze[0].replace("#", ".")+"..").toCharArray();
    else
    maze[i] = ("."+Smaze[i-1]+".").toCharArray();
    dfs(0,0);
    return ct;
    }
    void dfs(int x,int y){
    if(x<0||y<0||x>=maze.length||y>maze[0].length||visited[x][y])
    return;
    if(maze[x][y] == '#'){
    ct++;
    return;
    }
    visited[x][y] = true;
    dfs(x-1,y);
    dfs(x+1,y);
    dfs(x,y-1);
    dfs(x,y+1);
    }
    }  回復  更多評論
      
    <2007年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術網站

    java技術相關

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網網絡電話,中國最優秀的網絡電話
    主站蜘蛛池模板: 亚洲免费在线视频观看| 日韩精品内射视频免费观看 | 亚洲狠狠婷婷综合久久久久| 亚洲Aⅴ无码专区在线观看q| 亚洲毛片免费视频| 国产产在线精品亚洲AAVV| 一级毛片高清免费播放| 最近在线2018视频免费观看| 免费人成视频在线观看不卡| 亚洲精品无码久久久久久久| 未满十八18禁止免费无码网站 | 最近2019年免费中文字幕高清| 国产免费观看a大片的网站| 精品亚洲麻豆1区2区3区| 色窝窝亚洲av网| 国产成人精品免费视频动漫| 亚洲日本在线播放| 国产在线播放线91免费| 国产成人免费网站在线观看| 亚洲网站在线免费观看| 国产精品久久免费| 亚洲经典在线观看| 啦啦啦中文在线观看电视剧免费版| 国产亚洲一区二区精品| 日本免费一区二区久久人人澡| 久久久久亚洲AV无码专区网站| 亚洲国产精品网站在线播放 | 午夜视频免费成人| 亚洲精品乱码久久久久久下载| 57PAO成人国产永久免费视频 | 亚洲性久久久影院| 亚洲AV无码AV日韩AV网站| 国产在线jyzzjyzz免费麻豆| 亚洲乱码中文字幕在线| 成年男女免费视频网站| 亚洲天堂一区在线| 四虎影视永久免费观看地址| 日本不卡免费新一区二区三区| 亚洲午夜国产精品| 国产片AV片永久免费观看| 亚洲日韩精品无码专区网站|