<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 閱讀(665) 評(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)論
      

    主站蜘蛛池模板: 亚洲AV无码专区国产乱码电影| 亚洲精品无码成人片在线观看| 亚洲国产精品嫩草影院在线观看| 成人免费视频一区二区| 青青草国产免费久久久下载 | 又粗又硬免费毛片| 亚洲日韩AV一区二区三区四区 | 中文字幕在线免费观看| 亚洲av无码潮喷在线观看| 国产色无码精品视频免费| 亚洲av之男人的天堂网站| 嫩草成人永久免费观看| 亚洲最新永久在线观看| 精品国产sm捆绑最大网免费站| 久久精品国产亚洲av麻豆图片| 在线播放高清国语自产拍免费 | 午夜网站在线观看免费完整高清观看 | 亚洲AV无码专区国产乱码电影| 99热这里只有精品6免费| 亚洲第一成年人网站| 成人无码区免费视频观看| 美女被爆羞羞网站在免费观看 | 黄页网站在线观看免费高清| 亚洲一久久久久久久久| 免费又黄又爽的视频| a在线观看免费视频| 亚洲国产精品午夜电影| 永久黄网站色视频免费观看| 人禽伦免费交视频播放| 亚洲三级电影网站| 女人18毛片水真多免费看| 一级毛片不卡免费看老司机| 亚洲欧洲日韩不卡| 永久中文字幕免费视频网站| 国产精品美女久久久免费 | 亚洲福利视频导航| 国产免费久久久久久无码| 亚洲一区二区在线免费观看| 好大好深好猛好爽视频免费| 一区二区三区在线观看免费 | 亚洲中文字幕无码爆乳AV|