emu in blogjava

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評(píng)論 :: 2 Trackbacks

          考試剛剛結(jié)束,題目帖出來(lái)交流一下。
          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

          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.

          posted on 2005-12-13 12:00 emu 閱讀(1057) 評(píng)論(2)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評(píng)論

          # emu解 BusStops 2005-12-13 14:23 emu
          public class BusStops
          {
          public int countStops(String[] cityMap, int walkingDistance){
          int x=-1,y=-1,result=0;
          for(int i=0;i<cityMap.length;i++){
          if(cityMap[i].indexOf("X")>-1){
          y=i;x=cityMap[i].indexOf("X");
          break;
          }
          }
          for(int i=0;i<cityMap.length;i++){
          String s = cityMap[i];
          for(int j=0;j<s.length();j++){
          if(s.charAt(j)=='B'){
          int a= Math.abs(y-i)+Math.abs(x-j);
          if(a<=walkingDistance) result++;
          }
          }
          }
          return result;
          }
          public static void main(String[] args)
          {
          BusStops b = new BusStops();
          System.out.println(b.countStops( new String[]{"...B.",".....","..X.B",".....","B...."},3));
          System.out.println(b.countStops( new String[]{"B.B..",".....","B....",".....","....X"},8));
          System.out.println(b.countStops( new String[]{"BBBBB","BB.BB","B.X.B","BB.BB","BBBBB"},1));
          System.out.println(b.countStops( new String[]{"B..B..",".B...B","..B...","..B.X.","B.B.B.",".B.B.B"},3));
          }
          }  回復(fù)  更多評(píng)論
            

          # re: google中國(guó)編程挑戰(zhàn)賽資格賽真題 -- BusStops(250分) 2005-12-18 17:46 guest
          for(int i=0;i<cityMap.length;i++){
          String s = cityMap[i];
          for(int j=0;j<s.length();j++){

          i,j的起點(diǎn)可縮小,不過(guò)這是250分的,就別計(jì)較了,呵呵  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 广饶县| 社旗县| 毕节市| 阿拉善盟| 康平县| 中西区| 青冈县| 石门县| 健康| 鄂伦春自治旗| 嵊泗县| 庄浪县| 黄骅市| 临汾市| 乐安县| 抚州市| 洮南市| 宣城市| 石景山区| 茶陵县| 邳州市| 千阳县| 河东区| 东兰县| 正蓝旗| 扎囊县| 阿拉善左旗| 青浦区| 合江县| 宁城县| 苍梧县| 洪洞县| 泉州市| 唐河县| 汕头市| 来宾市| 南丹县| 济源市| 泗水县| 新余市| 扬中市|