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

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

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

    Fantasy's World

    世界的小世界,我的大世界^_^

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      6 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

    Problem Statement

    You are given a String[] cityMap representing the layout of a city. The city consists of blocks. The first element of cityMap represents the first row of blocks, etc. A 'B' character indicates a location where there is a bus stop. There will be exactly one 'X' character, indicating your location. All other characters will be '.'. You are also given an int walkingDistance, which is the maximum distance you are willing to walk to a bus stop. The distance should be calculated as the number of blocks vertically plus the number of blocks horizontally. Return the number of bus stops that are within walking distance of your current location.

    Definition

    Class:BusStops
    Method:countStops
    Parameters:String[], int
    Returns:int
    Method signature:int countStops(String[] cityMap, int walkingDistance)
    (be sure your method is public)

    Constraints

    -cityMap will contain between 1 and 50 elements, inclusive.
    -Each element of cityMap will contain between 1 and 50 characters, inclusive.
    -Each element of cityMap will contain the same number of characters.
    -Each character of each element of cityMap will be 'B', 'X', or '.'.
    -There will be exactly one 'X' character in cityMap.
    -walkingDistance will be between 1 and 100, inclusive.

    Examples

    0)

    {"...B.",
     ".....",
     "..X.B",
     ".....",
     "B...."}
    3
    Returns: 2
    You can reach the bus stop at the top (3 units away), or on the right (2 units away). The one in the lower left is 4 units away, which is too far.

    1)

    {"B.B..",
     ".....",
     "B....",
     ".....",
     "....X"}
    8
    Returns: 3
    A distance of 8 can get us anywhere on the map, so we can reach all 3 bus stops.

    2)

    {"BBBBB",
     "BB.BB",
     "B.X.B",
     "BB.BB",
     "BBBBB"}
    1
    Returns: 0
    Plenty of bus stops, but unfortunately we cannot reach any of them.

    3)

    {"B..B..",
     ".B...B",
     "..B...",
     "..B.X.",
     "B.B.B.",
     ".B.B.B"}
    3
    Returns: 7


    說(shuō)實(shí)話我覺(jué)得這一題沒(méi)啥意思,超簡(jiǎn)單,首先先確定X的位置,再用遍歷數(shù)組找B的位置,再求相減的絕對(duì)值然后判斷是否超出給出的最大距離就行了。相對(duì)這題PlayCars卻很有意思,到現(xiàn)在我也沒(méi)想出除了窮舉以外的一個(gè)更好的算法,因?yàn)槲矣X(jué)得窮舉可能會(huì)超時(shí)。有哪位有其它的辦法的話,請(qǐng)告訴我,大家探討一下,謝謝。好了,不廢話了,下面是這題的答案:

    public class BusStops {
     public static void main(String[] arg){
      BusStops total = new BusStops();
      
        System.out.println(total.countStops({"...B.",".....","..X.B",".....","B...."},3));
     }
     
     public int countStops(String[] cityMap, int walkingDistance){
      int sum= 0;
      int locationX = -1;
      int locationY = -1;
      for(int i=0;i<cityMap.length;i++){
       for(int j=0;j<cityMap[i].length();j++){
        if(cityMap[i].charAt(j)=='X'){
         locationX = i;
         locationY = j;
        }
       }
      }
      for(int i=0;i<cityMap.length;i++){
       for(int j=0;j<cityMap[i].length();j++){
        if(cityMap[i].charAt(j)=='B' && (Math.abs(locationX - i) + Math.abs(locationY - j)<=walkingDistance))
         sum++;
       }
      }
      return sum;
     }
    }
    posted on 2005-12-21 18:24 FinalFantasy 閱讀(664) 評(píng)論(1)  編輯  收藏 所屬分類: 讀書(shū)筆記

    Feedback

    # re: Google編程挑戰(zhàn)賽入圍賽250分題及答案——BusStops題 2005-12-22 14:53 dotNet的程序員
    用棧改進(jìn)一下。
    public class BusStops
    {

    public static void Main()
    {
    BusStops total = new BusStops();
    //System.out.println(total.countStops({"...B.",".....","..X.B",".....","B...."},3));
    System.Console.WriteLine(total.countStops(new string[]{"...B.",".....","..X.B",".....","B...."},3));
    System.Console.ReadLine();
    }

    public int countStops(string[] cityMap, int walkingDistance)
    {
    Position xPosition=null;
    System.Collections.Stack stack=new System.Collections.Stack();
    for(int i=0;i<cityMap.Length;i++)
    {
    for(int j=0;j<cityMap[i].Length;j++)
    {
    if(cityMap[i][j]=='X')
    {
    xPosition=new Position();
    xPosition.x = i;
    xPosition.y = j;
    }
    else if(cityMap[i][j]=='B')
    {
    Position busStop=new Position();
    busStop.x=i;
    busStop.y=j;
    stack.Push(busStop);
    }
    }
    }
    if (xPosition==null) throw new Exception("未發(fā)現(xiàn)X點(diǎn)");
    if (stack.Count==0) return 0;
    int sum= 0;
    while (stack.Count>0)
    {
    Position busStop=(Position)stack.Pop();
    if (Math.Abs(busStop.x-xPosition.x)+Math.Abs(busStop.y-xPosition.y)<=walkingDistance)
    sum++;
    }
    return sum;
    }

    public class Position
    {
    public int x=0;
    public int y=0;
    }  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 日韩免费码中文在线观看| 无码国产精品一区二区免费vr| 不卡一卡二卡三亚洲| 一级毛片免费观看| 亚洲另类无码专区丝袜| 国产亚洲无线码一区二区| 精品久久久久成人码免费动漫| 免费观看又污又黄在线观看| 精品无码一区二区三区亚洲桃色| 女人18毛片a级毛片免费 | 成人性生交大片免费看中文| 亚洲一级大黄大色毛片| 亚洲精品国产精品乱码不卡 | 国产亚洲精品岁国产微拍精品| 亚洲性线免费观看视频成熟| 一区二区三区免费在线视频| 亚洲一区免费视频| 亚洲欧洲无码AV电影在线观看 | 又黄又爽的视频免费看| 性xxxxx大片免费视频| 美女免费视频一区二区| 亚洲国产精品综合一区在线| 亚洲AV无码之日韩精品| 一二三四免费观看在线视频中文版| a级毛片免费高清视频| 亚洲日韩精品无码专区| 亚洲一区免费观看| 国产成人综合亚洲AV第一页| 成人免费视频观看无遮挡| 久9热免费精品视频在线观看| 美女被免费网站在线视频免费| 亚洲国产精品张柏芝在线观看 | 亚洲一级免费毛片| 亚洲尹人九九大色香蕉网站 | 亚洲第一街区偷拍街拍| 99亚洲精品高清一二区| 亚洲人成色7777在线观看| 在线永久免费观看黄网站| 97无码免费人妻超级碰碰碰碰 | 精品国产免费观看久久久| 国产精品色拉拉免费看|