emu in blogjava

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

          考試剛剛結(jié)束,題目帖出來交流一下。
          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 閱讀(1764) 評(píng)論(19)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評(píng)論

          # emu的錯(cuò)誤解法 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"));
          }
          }

          時(shí)間復(fù)雜度太高。當(dāng)時(shí)沒有好好想想。和alphabet同出一轍啊!該死!
            回復(fù)  更多評(píng)論
            

          # 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"));
              }
          }
            回復(fù)  更多評(píng)論
            

          # 用動(dòng)態(tài)規(guī)劃,三重循環(huán)就能搞定 2005-12-13 17:42 zzs
          11  回復(fù)  更多評(píng)論
            

          # re: google中國編程挑戰(zhàn)賽資格賽真題 -- WordPath(750分) 2005-12-13 18:02 emu
          我難道不是用動(dòng)態(tài)規(guī)劃、三重循環(huán)嗎?  回復(fù)  更多評(píng)論
            

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



            回復(fù)  更多評(píng)論
            

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

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

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

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

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

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

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

          # re: google中國編程挑戰(zhàn)賽資格賽真題 -- WordPath(750分) 2005-12-15 15:26 emu
          :) 用動(dòng)態(tài)規(guī)劃,時(shí)間只是隨參數(shù)大小線性增長的,再慢都很快  回復(fù)  更多評(píng)論
            

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

          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"));
           }
          }

            回復(fù)  更多評(píng)論
            

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

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

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

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


          歡迎大家指正。

            回復(fù)  更多評(píng)論
            

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

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

          主站蜘蛛池模板: 新昌县| 吴桥县| 常德市| 白城市| 乐清市| 武乡县| 牡丹江市| 昌江| 宁阳县| 新丰县| 南昌县| 理塘县| 平凉市| 扬中市| 宣恩县| 天津市| 珲春市| 福建省| 平舆县| 武平县| 溧阳市| 安图县| 肥乡县| 隆尧县| 灵武市| 普兰县| 六盘水市| 姚安县| 六安市| 安国市| 南江县| 德安县| 方山县| 梁平县| 大足县| 镇宁| 黎城县| 漳州市| 清远市| 大理市| 文化|