emu in blogjava

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

          考試剛剛結(jié)束,題目帖出來(lái)交流一下。
          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 閱讀(1768) 評(píng)論(19)  編輯  收藏 所屬分類(lèi): 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í)沒(méi)有好好想想。和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中國(guó)編程挑戰(zhàn)賽資格賽真題 -- WordPath(750分) 2005-12-13 18:02 emu
          我難道不是用動(dòng)態(tài)規(guī)劃、三重循環(huán)嗎?  回復(fù)  更多評(píng)論
            

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



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

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

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

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

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

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

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

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

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

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

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

          # re: google中國(guó)編程挑戰(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)論
            

          主站蜘蛛池模板: 彭水| 金塔县| 陆丰市| 大新县| 台南县| 澎湖县| 天台县| 台江县| 桐城市| 疏勒县| 元江| 绍兴县| 太康县| 新宁县| 永康市| 汪清县| 广州市| 桓台县| 右玉县| 浪卡子县| 宁乡县| 平江县| 永安市| 金平| 南乐县| 阳高县| 营山县| 中超| 印江| 双柏县| 象山县| 西贡区| 平度市| 喜德县| 溧水县| 郓城县| 什邡市| 南部县| 八宿县| 奇台县| 黔西|