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

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

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

    emu in blogjava

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

    考試剛剛結束,題目帖出來交流一下。
    Problem Statement
    ????
    You are given a String[] grid representing a rectangular grid of letters. You are also given a String find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the grid more than once, but you may not stay on the same cell twice in a row (see example 6 for clarification).
    You are to return an int indicating the number of ways find can be found within the grid. If the result is more than 1,000,000,000, return -1.
    Definition
    ????
    Class:
    WordPath
    Method:
    countPaths
    Parameters:
    String[], String
    Returns:
    int
    Method signature:
    int countPaths(String[] grid, String find)
    (be sure your method is public)
    ????

    Constraints
    -
    grid will contain between 1 and 50 elements, inclusive.
    -
    Each element of grid will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
    -
    Each element of grid will contain the same number of characters.
    -
    find will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
    Examples
    0)

    ????
    {"ABC",
     "FED",
     "GHI"}
    "ABCDEFGHI"
    Returns: 1
    There is only one way to trace this path. Each letter is used exactly once.
    1)

    ????
    {"ABC",
     "FED",
     "GAI"}
    "ABCDEA"
    Returns: 2
    Once we get to the 'E', we can choose one of two directions for the final 'A'.
    2)

    ????
    {"ABC",
     "DEF",
     "GHI"}
    "ABCD"
    Returns: 0
    We can trace a path for "ABC", but there's no way to complete a path to the letter 'D'.
    3)

    ????
    {"AA",
     "AA"}
    "AAAA"
    Returns: 108
    We can start from any of the four locations. From each location, we can then move in any of the three possible directions for our second letter, and again for the third and fourth letter. 4 * 3 * 3 * 3 = 108.
    4)

    ????
    {"ABABA",
     "BABAB",
     "ABABA",
     "BABAB",
     "ABABA"}
    "ABABABBA"
    Returns: 56448
    There are a lot of ways to trace this path.
    5)

    ????
    {"AAAAA",
     "AAAAA",
     "AAAAA",
     "AAAAA",
     "AAAAA"}
    "AAAAAAAAAAA"
    Returns: -1
    There are well over 1,000,000,000 paths that can be traced.
    6)

    ????
    {"AB",
     "CD"}
    "AA"
    Returns: 0
    Since we can't stay on the same cell, we can't trace the path at all.
    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 閱讀(1763) 評論(19)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

    評論

    # emu的錯誤解法 2005-12-13 13:27 emu
    public class WordPath
    {
    int resultCount;
    public int countPaths(String[] grid, String find){
    int rowCount = grid.length;
    int cellCount=grid[0].length();
    resultCount = 0;
    char[][] charGrid = new char[rowCount][cellCount];
    for(int y=0;y<rowCount;y++)
    for(int x=0;x<cellCount;x++)
    charGrid[y][x] = grid[y].charAt(x);
    for(int y=0;y<rowCount;y++){
    for(int x=0;x<cellCount;x++){
    if(charGrid[y][x]==find.charAt(0)){
    doCount(charGrid,find.substring(1),x,y);
    }
    if(resultCount<0) return -1;
    }
    }
    return resultCount;
    }
    private void doCount(char[][] c,String find,int x,int y){
    if(resultCount<0) return;
    if(find.length()==0) {
    resultCount++;
    if(resultCount>10000000) resultCount=-1;
    return;
    }
    if(y>0){
    if(c[y-1][x]==find.charAt(0))
    doCount(c,find.substring(1),x,y-1);
    if(resultCount<0) return ;
    if(x>0 && c[y-1][x-1]==find.charAt(0))
    doCount(c,find.substring(1),x-1,y-1);
    if(resultCount<0) return ;
    if(x<c[0].length-1 && c[y-1][x+1]==find.charAt(0))
    doCount(c,find.substring(1),x+1,y-1);
    }
    if(resultCount<0) return ;
    if(y<(c.length-1)){
    if(c[y+1][x]==find.charAt(0))
    doCount(c,find.substring(1),x,y+1);
    if(resultCount<0) return ;
    if(x>0 && c[y+1][x-1]==find.charAt(0))
    doCount(c,find.substring(1),x-1,y+1);
    if(resultCount<0) return ;
    if(x<c[0].length-1 && c[y+1][x+1]==find.charAt(0))
    doCount(c,find.substring(1),x+1,y+1);
    }
    if(resultCount<0) return ;
    if(x>0 && c[y][x-1]==find.charAt(0))
    doCount(c,find.substring(1),x-1,y);
    if(resultCount<0) return ;
    if(x<c[0].length-1 && c[y][x+1]==find.charAt(0)){
    doCount(c,find.substring(1),x+1,y);
    }
    return;
    }

    public static void main(String[] args){
    WordPath w = new WordPath();
    System.out.println(w.countPaths(new String[]{"ABC","FED","GHI"},"ABCDEFGHI"));
    System.out.println(w.countPaths(new String[]{"ABC","FED","GAI"},"ABCDEA"));
    System.out.println(w.countPaths(new String[]{"ABC","DEF","GHI"},"ABCD"));
    System.out.println(w.countPaths(new String[]{"AA","AA"},"AAAA"));
    System.out.println(w.countPaths(new String[]{"ABABA","BABAB","ABABA","BABAB","ABABA"},"ABABABBA"));
    System.out.println(w.countPaths(new String[]{"AAAAA","AAAAA","AAAAA","AAAAA","AAAAA"},"AAAAAAAAAAA"));
    System.out.println(w.countPaths(new String[]{"AB","CD"},"AA"));
    }
    }

    時間復雜度太高。當時沒有好好想想。和alphabet同出一轍啊!該死!
      回復  更多評論
      

    # emu 更正的解法 2005-12-13 16:48 emu
    看看,和alphabet是不是一樣:

    public class WordPath {
        public int countPaths(String[] grid, String find){
            int rowCount = grid.length;
            int cellCount=grid[0].length();
            char[][] charGrid = new char[rowCount][cellCount];
            int[][] m = new int[rowCount][cellCount],m1 = new int[rowCount][cellCount];
            for(int y=0;y<rowCount;y++)
                for(int x=0;x<cellCount;x++)
                    if (find.charAt(0)==(charGrid[y][x]=grid[y].charAt(x))) m[y][x]=1;
            for(int i=1;i<find.length();i++){
                char ch = find.charAt(i);
                for(int y=0;y<rowCount;y++){
                    for(int x=0;x<cellCount;x++){
                        if(ch == charGrid[y][x]){
                            if(y>0){
                                if(x>0)m1[y][x]+=m[y-1][x-1];
                                m1[y][x]+=m[y-1][x];
                                if(x<cellCount-1) m1[y][x]+=m[y-1][x+1];
                            }
                            if(x>0)m1[y][x]+=m[y][x-1];
                            if(x<cellCount-1) m1[y][x]+=m[y][x+1];
                            if(y<rowCount-1){
                                if(x>0) m1[y][x]+=m[y+1][x-1];
                                m1[y][x]+=m[y+1][x];
                                if(x<cellCount-1) m1[y][x]+=m[y+1][x+1];
                            }
                        }
                    }
                }
                m=m1;m1= new int[rowCount][cellCount];
            }
            int result = 0;
            for(int i=0;i<rowCount;i++)for(int j=0;j<cellCount;j++) if((result += m[i][j])>1000000000)return -1;
            return result;
        }

        public static void main(String[] args){
            WordPath w = new WordPath();
            System.out.println(w.countPaths(new String[]{"ABC","FED","GHI"},"ABCDEFGHI"));
            System.out.println(w.countPaths(new String[]{"ABC","FED","GAI"},"ABCDEA"));
            System.out.println(w.countPaths(new String[]{"ABC","DEF","GHI"},"ABCD"));
            System.out.println(w.countPaths(new String[]{"AA","AA"},"AAAA"));
            System.out.println(w.countPaths(new String[]{"ABABA","BABAB","ABABA","BABAB","ABABA"},"ABABABBA"));
            System.out.println(w.countPaths(new String[]{"AAAAA","AAAAA","AAAAA","AAAAA","AAAAA"},"AAAAAAAAAAA"));
            System.out.println(w.countPaths(new String[]{"AB","CD"},"AA"));
        }
    }
      回復  更多評論
      

    # 用動態規劃,三重循環就能搞定 2005-12-13 17:42 zzs
    11  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-13 18:02 emu
    我難道不是用動態規劃、三重循環嗎?  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-14 10:19 zzs
    你用的java我沒看:)。對JAVA不是很懂。
    不過貌似狀態是三維的吧。
    遞推狀態矩陣State[k][i][j]表示第k步時第[i,j]點的累計方法數。
    差不多這樣子。



      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-14 12:47 emu
    上回貼的時候沒有做好符號替換,貼出來的代碼很多都顯示不出來,剛剛才發現。難怪你看了覺得不象動態規劃。
    遞推的過程只需要保留上一步驟的結果和當前計算中的步驟的中間結果就可以了,沒有必要用三維數組保存全部推導狀態。  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-14 18:12 靈犀
    初賽比的是速度,怎么簡單怎么寫。
    google在出題上并沒有在時間和空間上給予難度。
    數據都相當小= =
    居然只有50。。。。
      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-14 21:55 emu
    >>google在出題上并沒有在時間和空間上給予難度。
    其實還是會有的。上次東亞我記得是8秒,這次有人說限定是2秒。這個要求對于正確的算法是非常寬松的,但是對于錯誤的算法就是一個過不去的坎了。象這道題,我第一次的解法就是在時間復雜度上有問題。  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 10:18 靈犀
    恩~你說的沒錯,時限是兩秒。
    以前的比賽你參加了?
    那是什么比賽呢?
      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 10:59 emu
    就是今年的東亞code jam大賽啊。不過在資格賽就刷下來了,我覺得有點冤的,入圍的不見得都是什么高手。模擬題和資格賽題收集在http://www.tkk7.com/emu/category/2769.html
      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 11:27 zming
    emu,你的正確的代碼自己運行需要多少秒時間?我只跑
    System.out.println(w.countPaths(new String[]{"AAAAA","AAAAA","AAAAA","AAAAA","AAAAA"},"AAAAAAAAAAA"));
    的例子需要近20秒呀?  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 13:14 zming
    emu,對不起,剛才測試的有問題,我運行了你的更正的解法的代碼,速度的確很快!  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 15:26 emu
    :) 用動態規劃,時間只是隨參數大小線性增長的,再慢都很快  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 15:52 emu
    試試更大的測試數據:

    public class WordPath {
        public long countPaths(String[] grid, String find){
      int rowCount = grid.length;
      int cellCount=grid[0].length();
      char[][] charGrid = new char[rowCount][cellCount];
      long[][] m = new long[rowCount][cellCount],m1 = new long[rowCount][cellCount];
      for(int y=0;y<rowCount;y++)
       for(int x=0;x<cellCount;x++)
        if (find.charAt(0)==(charGrid[y][x]=grid[y].charAt(x))) m[y][x]=1;
      for(int i=1;i<find.length();i++){
       char ch = find.charAt(i);
       for(int y=0;y<rowCount;y++){
        for(int x=0;x<cellCount;x++){
         if(ch == charGrid[y][x]){
          if(y>0){
           if(x>0)m1[y][x]+=m[y-1][x-1];
           m1[y][x]+=m[y-1][x];
           if(x<cellCount-1) m1[y][x]+=m[y-1][x+1];
          }
          if(x>0)m1[y][x]+=m[y][x-1];
          if(x<cellCount-1) m1[y][x]+=m[y][x+1];
          if(y<rowCount-1){
           if(x>0) m1[y][x]+=m[y+1][x-1];
           m1[y][x]+=m[y+1][x];
           if(x<cellCount-1) m1[y][x]+=m[y+1][x+1];
          }
         }
        }
       }
       m=m1;m1= new long[rowCount][cellCount];
      }
      long result = 0;
      for(int i=0;i<rowCount;i++)for(int j=0;j<cellCount;j++) if(m[i][j]<0||(result += m[i][j])<0)return -1;
      return result;
     }

     public static void main(String[] args){
      WordPath w = new WordPath();
      System.out.println(w.countPaths(new String[]{"ABC","FED","GHI"},"ABCDEFGHI"));
      System.out.println(w.countPaths(new String[]{"ABC","FED","GAI"},"ABCDEA"));
      System.out.println(w.countPaths(new String[]{"ABC","DEF","GHI"},"ABCD"));
      System.out.println(w.countPaths(new String[]{"AA","AA"},"AAAA"));
      System.out.println(w.countPaths(new String[]{"ABABA","BABAB","ABABA","BABAB","ABABA"},"ABABABBA"));
      System.out.println(w.countPaths(new String[]{"AAAAA","AAAAA","AAAAA","AAAAA","AAAAA"},"AAAAAAAAAAA"));
      System.out.println(w.countPaths(new String[]{"AB","CD"},"AA"));
      System.out.println(w.countPaths(new String[]{"AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA"},"AAAAAAAAAAAAAAAAAAAA"));
      System.out.println(w.countPaths(new String[]{"AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA","AAAAAAAA"},"AAAAAAAAAAAAAAAAAAAAA"));
     }
    }

      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 17:08
    這里能貼 c# 么? 怯怯的問。。。
    我也寫了一個解法  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-15 18:00 靈犀
    我這是第一次參加CodeJam的比賽。
    呵呵~算是菜鳥拉。
    我的測了最大的數據(50*50個"A"),然后給出一個長度為50的全"A"串,
    用它的系統測大概是0.01s
      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-16 16:56 RoadNorth
    我也做了一個解法,用C++

    http://blog.csdn.net/RoadNorth/archive/2005/12/16/554027.aspx


    歡迎大家指正。

      回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-17 01:12 emu
    你的解法倒是很別出心裁,有必要一定要倒過來查嗎,正著查不是一樣?
    不過看看代碼量,估計是超過1個小時的。  回復  更多評論
      

    # re: google中國編程挑戰賽資格賽真題 -- WordPath(750分) 2005-12-23 12:56 Tendy
    我做了一個,回復在
    http://blog.csdn.net/zmxj/archive/2005/12/13/551492.aspx
    運行第五個例子大概花了 2 分鐘(CPU PIII 866MHZ,內存512M)
    PS:
    我的代碼如果要連續運行所有example,
    要在函數 int countPaths(String[] grid, String find)
    里面加一句 total = 0;
      回復  更多評論
      

    主站蜘蛛池模板: 亚洲成熟xxxxx电影| 亚洲乱码一二三四区乱码| 久久99精品视免费看| 亚洲一级毛片在线播放| 浮力影院第一页小视频国产在线观看免费| 曰批全过程免费视频免费看 | 国产免费怕怕免费视频观看| 中文字幕在线观看免费| 亚洲白嫩在线观看| 免费A级毛片无码A| 最近最新高清免费中文字幕| 亚洲jizzjizz少妇| 日韩亚洲欧洲在线com91tv| 天天操夜夜操免费视频| 99视频免费在线观看| 亚洲一卡2卡4卡5卡6卡残暴在线| 免费a级毛片无码av| 5555在线播放免费播放| 成人精品综合免费视频| 亚洲无人区视频大全| 亚洲午夜精品久久久久久浪潮 | 无遮挡免费一区二区三区| 亚洲精品视频免费在线观看| 免费在线视频一区| 中国在线观看免费国语版| 中国国语毛片免费观看视频| 亚洲中文精品久久久久久不卡| 亚洲AV无码专区亚洲AV伊甸园| 国产免费怕怕免费视频观看| 国产成人免费网站| 99精品视频在线免费观看| 一区二区免费国产在线观看| 亚洲精品第一国产综合野| 亚洲AV综合色区无码一区爱AV| 国产高清免费在线| 男女免费观看在线爽爽爽视频 | 亚洲国产成人久久三区| 国产精品亚洲视频| 日韩在线免费电影| 国产精品99精品久久免费| 草久免费在线观看网站|