Fantasy's World

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

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            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


          說實話我覺得這一題沒啥意思,超簡單,首先先確定X的位置,再用遍歷數組找B的位置,再求相減的絕對值然后判斷是否超出給出的最大距離就行了。相對這題PlayCars卻很有意思,到現在我也沒想出除了窮舉以外的一個更好的算法,因為我覺得窮舉可能會超時。有哪位有其它的辦法的話,請告訴我,大家探討一下,謝謝。好了,不廢話了,下面是這題的答案:

          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) 評論(1)  編輯  收藏 所屬分類: 讀書筆記

          Feedback

          # re: Google編程挑戰賽入圍賽250分題及答案——BusStops題 2005-12-22 14:53 dotNet的程序員
          用棧改進一下。
          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("未發現X點");
          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;
          }  回復  更多評論
            

          主站蜘蛛池模板: 牡丹江市| 灯塔市| 商城县| 遂平县| 伊金霍洛旗| 古田县| 灵川县| 榆林市| 西和县| 兴海县| 察哈| 揭阳市| 桂阳县| 泰安市| 新建县| 佛学| 平泉县| 阿勒泰市| 南岸区| 永嘉县| 赞皇县| 柘城县| 台湾省| 德化县| 丰原市| 南京市| 甘泉县| 图们市| 葵青区| 静乐县| 东兰县| 清徐县| 德格县| 伊金霍洛旗| 镇平县| 凤凰县| 奉新县| 璧山县| 雅安市| 蒙阴县| 南皮县|