emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks

          Problem Statement

          ???? You want to send a group of salespeople from location 0 to location 1, but no two of them can travel through the same location (other than 0 and 1). This removes the possibility of trying to sell a customer the same product twice. Character j of element i (both 0-based) of adj denotes whether locations i and j are connected by a symmetric link ('1' for connected, '0' otherwise). Return the greatest number of salespeople that can be sent. The constraints will guarantee that locations 0 and 1 do not share a link.

          Definition

          ????
          Class: SalesRouting
          Method: howMany
          Parameters: String[]
          Returns: int
          Method signature: int howMany(String[] adj)
          (be sure your method is public)
          ????

          Constraints

          - adj will contain between 3 and 12 elements, inclusive.
          - Each element of adj will contain exactly N characters, where N is the number of elements in adj.
          - Each character in adj will be '0' (zero) or '1' (one).
          - Character i of element j of adj will be the same as character j of element i.
          - Character i of element i of adj will be '0'.
          - Character 1 of element 0 of adj will be '0'.

          Examples

          0)
          ????
          {
          "001",
          "001",
          "110"
          }
          Returns: 1
          We can send a single salesperson from location 0 to location 2, and finally to location 1.
          1)
          ????
          {
          "0010",
          "0010",
          "1100",
          "0000"
          }
          Returns: 1
          Same as before, but now there is an isolated location 3.
          2)
          ????
          {
          "001100",
          "000001",
          "100010",
          "100010",
          "001101",
          "010010"
          }
          Returns: 1
          The only location that is directly connected to location 1 is 5, so only 1 salesperson can be sent.
          3)
          ????
          {
          "001111",
          "001111",
          "110000",
          "110000",
          "110000",
          "110000"
          }
          Returns: 4
          4)
          ????
          {
          "00000",
          "00000",
          "00000",
          "00000",
          "00000"
          }
          Returns: 0

          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 2006-09-05 10:19 emu 閱讀(2044) 評論(4)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評論

          # re: 250分模擬題 SalesRouting 2006-09-05 19:29 frog
          public class SalesRouting {

          public int howMany(String[] adj) {
          int rowCount = adj.length;
          int cellCount = adj[0].length();
          int[] m = new int[cellCount];
          //先看看Location0&1有沒有直接連同的,就是只通過一個Location就到
          //等于2的就是可行的通道
          for(int i=2; i<cellCount; i++){
          if(adj[0].charAt(i) == '1')
          m[i] += 1;
          if(adj[1].charAt(i) == '1')
          m[i] += 1;
          }
          //只搜索右上的三角,用m[]表示連通狀態,現在m是Location0&1的連通狀態
          //0表示同0&1的不連通,1表示連通,2表示有通路。
          //用m1表示剩下的Location的連通狀態
          for(int j=2; j<rowCount; j++){
          //for(int k=0; k<rowCount; k++)
          // System.out.print(m[k]+" ");
          //System.out.println(" ");
          //這里判斷m[]==1非常重要,
          //m[]==0就是集合不連通,m[]==2就是已經連通,不需要再判斷了
          if(m[j] == 1){
          for(int i=j; i<cellCount; i++){
          if((adj[j].charAt(i) == '1') && (m[i] != 2)){
          m[i]++;
          }
          }
          }
          }

          //計數
          int result = 0;
          for(int i=2; i<cellCount; i++){
          if(m[i] == 2)
          result++;
          }

          return result;
          }

          public static void main(String[] args) {
          SalesRouting sr = new SalesRouting();

          int result = sr.howMany(new String[]{"001","001","110"});
          System.out.println(result);
          result = sr.howMany(new String[]{"0010","0010","1100","0000"});
          System.out.println(result);
          result = sr.howMany(new String[]{"001100","000001","100010","100010","001101","010010"});
          System.out.println(result);
          result = sr.howMany(new String[]{"001111","001111","110000","110000","110000","110000"});
          System.out.println(result);
          result = sr.howMany(new String[]{"00000","00000","00000","00000","00000"});
          System.out.println(result);
          }
          }   回復  更多評論
            

          # re: 250分模擬題 SalesRouting 2006-09-05 19:30 frog
          500合1000的還沒做出來,
          1000的我感覺好難,不知有誰已經成功了  回復  更多評論
            

          # re: 250分模擬題 SalesRouting 2006-09-05 19:53 bood
          500的用DP求出所有子串是否回文即可
          1000的沒做出來,不過看到有人直接用公式2^(a+b)+2^c次……汗
          frog我倒是250的看不懂你的解法,這樣肯定能保證最大么?  回復  更多評論
            

          # re: 250分模擬題 SalesRouting 2008-07-14 22:08 mabusyao
          package salesRouting;

          public class SalesRouting {

          public static int howMany(String[] adj) {
          matrix = new int[adj.length][adj[0].length()];
          flags = new int[adj.length];

          for(int i = 0; i < adj.length; i++) {
          flags[i] = 0;
          for(int j = 0; j < adj[0].length(); j++) {
          matrix[i][j] = adj[i].charAt(j) - 48;

          }
          }

          return iterate(0);
          }

          private static int[][] matrix;
          private static int[] flags;
          private static int iterate(int index) {
          int result = 0;
          if(index == 1) {
          System.out.print(index+ " ");
          return 1;
          }

          int next = 1;
          while(next < matrix.length) {
          if(matrix[index][next] == 1 && flags[next] == 0 && flags[index] == 0) {
          if(index != 0 && index != 1) flags[index] = 1;
          int success = iterate(next);
          if (success == 1) {
          if(index == 0) {
          System.out.println(0 + " ");
          } else System.out.print(index+ " ");
          result++;
          }else flags[index] = 0;
          }
          next++;
          }
          return result;
          }
          }



          到底該怎么保證最大呢? 我找了個例子:
          String[] str={"001100","000011","100110","101001","011000","010100"};

          想了半天沒有想出來該怎么保證結果是最大。  回復  更多評論
            

          主站蜘蛛池模板: 南开区| 林口县| 轮台县| 江西省| 临猗县| 临沧市| 永顺县| 缙云县| 尼玛县| 呼图壁县| 泌阳县| 芒康县| 北流市| 英吉沙县| 西和县| 太保市| 乌拉特前旗| 沧源| 桐柏县| 西峡县| 呼伦贝尔市| 阿瓦提县| 吉隆县| 博白县| 鹤岗市| 疏勒县| 富民县| 平湖市| 山西省| 平利县| 云霄县| 黄龙县| 星子县| 贵港市| 广灵县| 青浦区| 虞城县| 蒙山县| 广安市| 师宗县| 文山县|