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

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

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

    將Java進行到底
    將Java進行到底
    posts - 15,  comments - 66,  trackbacks - 0

    Problem Statement

         A simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. The upper left corner of the canvas is at (0, 0) and the lower right corner is at (19, 19). You are given a String[], commands, each element of which contains one of two possible commands. A command of the form "FORWARD x" means that the cursor should move forward by x pixels. Each pixel on its path, including the start and end points, is painted black. The only other command is "LEFT", which means that the cursor should change its direction by 90 degrees counterclockwise. So, if the cursor is initially pointing straight down and it receives a single "LEFT" command, it will end up pointing straight to the right. Execute all the commands in order and return the resulting 20 x 20 pixel canvas as a String[] where character j of element i represents the pixel at (i, j). Black pixels should be represented as uppercase 'X' characters and blank pixels should be represented as '.' characters.

    Definition

        
    Class: DrawLines
    Method: execute
    Parameters: String[]
    Returns: String[]
    Method signature: String[] execute(String[] commands)
    (be sure your method is public)
        

    Notes

    - The cursor only paints the canvas if it moves (see example 1).

    Constraints

    - commands will contain between 1 and 50 elements, inclusive.
    - Each element of commands will be formatted as either "LEFT" or "FORWARD x" (quotes for clarity only), where x is an integer between 1 and 19, inclusive, with no extra leading zeros.
    - When executing the commands in order, the cursor will never leave the 20 x 20 pixel canvas.

    Examples

    0)
        
    {"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"}
    Returns: 
    {"XXXXXXXXXXXXXXXXXXXX",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "X..................X",
     "XXXXXXXXXXXXXXXXXXXX" }
    This sequence of commands draws a 20 x 20 outline of a square. The cursor is initially at (0, 0) pointing straight down. It then travels to (0, 19) after the first FORWARD command, painting each pixel along its path with a '*'. It then rotates 90 degrees left, travels to (19, 19), rotates 90 degrees left, travels to (19, 0), rotates 90 degrees left, and finally travels back to (0, 0).
    1)
        
    {"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"}
    Returns: 
    {"....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "...................." }
    The cursor spins round and round, but never actually paints any pixels. The result is an empty canvas.
    2)
        
    {"FORWARD 1"}
    Returns: 
    {"X...................",
     "X...................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "...................." }
    Going forward by one pixel creates a line that is 2 pixels long because both the start and end points are painted.
    3)
        
    {"LEFT", "FORWARD 19", "LEFT", "LEFT", "LEFT",
     "FORWARD 18", "LEFT", "LEFT", "LEFT", "FORWARD 17",
     "LEFT", "LEFT", "LEFT", "FORWARD 16", "LEFT",
     "LEFT", "LEFT", "FORWARD 15", "LEFT", "LEFT", "LEFT",
     "FORWARD 14", "LEFT", "LEFT", "LEFT", "FORWARD 13",
     "LEFT", "LEFT", "LEFT", "FORWARD 12", "LEFT", "LEFT",
     "LEFT", "FORWARD 11", "LEFT", "LEFT", "LEFT", "FORWARD 10",
     "LEFT", "LEFT", "LEFT", "FORWARD 9", "LEFT", "LEFT",
     "LEFT", "FORWARD 8", "LEFT", "LEFT", "LEFT", "FORWARD 7"}
    Returns: 
    {"XXXXXXXXXXXXXXXXXXXX",
     "...................X",
     "..XXXXXXXXXXXXXXXX.X",
     "..X..............X.X",
     "..X.XXXXXXXXXXXX.X.X",
     "..X.X..........X.X.X",
     "..X.X.XXXXXXXX.X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.X........X.X.X",
     "..X.X.XXXXXXXXXX.X.X",
     "..X.X............X.X",
     "..X.XXXXXXXXXXXXXX.X",
     "..X................X",
     "..XXXXXXXXXXXXXXXXXX",
     "...................." }

    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.

    答案:

      1public class DrawLines {
      2
      3
      4    public String[] execute(String[] commands){
      5        if (commands == null || commands.length == 0return null;
      6
      7        // initialize result array
      8        String[][] canvas = new String[20][20];
      9        for(int i = 0; i < 20; i++){
     10            for(int j = 0; j < 20; j++){
     11                canvas[i][j] = ".";
     12            }

     13        }

     14
     15        // execute commands
     16        int m = 0;
     17        int n = 1;
     18        int x = 0;
     19        int y = 0;
     20        int movelength = 0;
     21        for(int index = 0; index < commands.length; index++){
     22            if("LEFT".equalsIgnoreCase(commands[index])){
     23                System.out.println("LEFT");
     24                if(m == 0 && n == 1){
     25                    m = 1;
     26                    n = 0;
     27                }
    else if(m == 1 && n == 0){
     28                    m = 0;
     29                    n = -1;
     30                }
    else if(m == 0 && n == -1){
     31                    m = -1;
     32                    n = 0;
     33                }
    else if(m == -1 && n == 0){
     34                    m = 0;
     35                    n = 1;
     36                }

     37                System.out.println("m, n:" + m + "," + n);
     38                continue;
     39            }

     40
     41            if("FORWARD".equalsIgnoreCase(commands[index].substring(07))){
     42                movelength = Integer.parseInt(commands[index].substring(8, commands[index].length()));
     43                System.out.println("movelength:" + movelength);
     44                if(m == 0 && n == 1){
     45                    for(int j = y; j <= y + movelength * n; j += n){
     46                        canvas[x][j] = "X";
     47                    }

     48                    y = y + movelength * n;
     49                }

     50                if(m == 0 && n == -1){
     51                    for(int j = y; j >= y + movelength * n; j += n){
     52                        canvas[x][j] = "X";
     53                    }

     54                    y = y + movelength * n;
     55                }

     56                if(n == 0 && m == 1){
     57                    for(int i = x; i <= x + movelength * m; i += m){
     58                        canvas[i][y] = "X";
     59                    }

     60                    x = x + movelength * m;
     61                }

     62                if(n == 0 && m == -1){
     63                    for(int i = x; i >= x + movelength * m; i += m){
     64                        canvas[i][y] = "X";
     65                    }

     66                    x = x + movelength * m;
     67                }

     68                System.out.println("x, y:" + x + "," + y);
     69            }

     70        }

     71
     72        // format result
     73        String[] result = new String[20];
     74        for(int j = 0; j < 20; j++){
     75            result[j] = "";
     76            for(int i = 0; i < 20; i++){
     77                result[j] += canvas[i][j];
     78            }

     79        }

     80        return result;
     81
     82
     83    }

     84
     85    /**
     86     * @param args
     87     */

     88    public static void main(String[] args) {
     89        DrawLines drawLines = new DrawLines();
     90        String[] input = new String[7];
     91        //input = {"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"};
     92        input[0= "FORWARD 19";
     93        input[1= "LEFT";
     94        input[2= "FORWARD 19";
     95        input[3= "LEFT";
     96        input[4= "FORWARD 19";
     97        input[5= "LEFT";
     98        input[6= "FORWARD 19";
     99
    100        String[] result = drawLines.execute(input);
    101
    102        for(int i = 0; i < result.length; i++){
    103            System.out.println(result[i]);
    104        }

    105    }

    106
    107}

    108
    posted on 2005-11-27 23:37 風蕭蕭 閱讀(1157) 評論(0)  編輯  收藏 所屬分類: 雜談

    <2005年11月>
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    常用鏈接

    留言簿(8)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    myfriends

    opensource

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费乱码中文字幕网站| 无码免费午夜福利片在线| 亚洲国产一区二区a毛片| 日韩免费一区二区三区在线播放| 国产精品久久亚洲不卡动漫| 亚洲Av无码乱码在线播放| 免费无码H肉动漫在线观看麻豆| 亚洲制服丝袜在线播放| 免费成人黄色大片| **实干一级毛片aa免费| 黄色毛片视频免费| 亚洲精品美女久久久久9999| 波多野结衣免费视频观看| 最近免费中文字幕mv电影| 边摸边吃奶边做爽免费视频99| 久久精品国产精品亚洲毛片| 亚洲国产成人久久综合野外| 精品国产sm捆绑最大网免费站| 免费看一级高潮毛片| 亚洲成av人片不卡无码| 国产亚洲一区二区三区在线不卡| 97人伦色伦成人免费视频| 在线观看免费黄网站| 亚洲精品无码专区久久| 少妇中文字幕乱码亚洲影视| 国产国拍亚洲精品福利| 免费高清小黄站在线观看| 最近中文字幕高清免费中文字幕mv | 亚洲一区二区三区免费视频| 中美日韩在线网免费毛片视频| 亚洲 欧洲 自拍 另类 校园| 亚洲国产成人片在线观看无码| 四虎影视永久免费观看网址| 亚洲日产无码中文字幕| 精品国产免费观看久久久| 亚洲一区二区免费视频| 99精品视频免费在线观看| 国产精品极品美女自在线观看免费 | 国精无码欧精品亚洲一区 | 亚洲国产成人精品无码区在线网站| 中文字幕专区在线亚洲|