Spirit

          Google Code Jam - WordPath

          - Implementation
          public class WordPath {    
              
          public int countPaths(String[] grid, String find) {
                  
          int length = grid.length + 2;      
                  
                  
          //convert "grid" from String to char matrix
                  char[][] charGrid = new char[length][length];
                  
          for (int i = 0; i < length; i++) {
                      
          for (int j = 0; j < length; j++) {
                          
          if (i == 0 || i == length - 1 || j == 0 || j == length - 1)
                              charGrid[i][j] 
          = '0';
                          
          else
                              charGrid[i][j] 
          = grid[i - 1].charAt(j - 1);
                      }
                  }
                  
                  
          //convert "find" from String to char array
                  char[] charFind = new char[find.length()];
                  
          for (int k = 0; k < find.length(); k++)
                      charFind[k] 
          = find.charAt(k);

                  
          //use three dimensions "degree" to hold the in-degree of the vertex of graph
                  int[][][] degree = new int[length][length][find.length()];        

                  
          // k=0
                  for (int i = 0; i < length; i++)
                      
          for (int j = 0; j < length; j++) {
                          degree[i][j][
          0= ((charGrid[i][j] == charFind[0]) ? 1 : 0);
                      }
                  
                  
          // fill the degree 
                  for (int k = 1; k < find.length(); k++) {
                      
          for (int i = 0; i < length; i++)
                          
          for (int j = 0; j < length; j++) {
                              
          if (charGrid[i][j] != charFind[k])
                                  degree[i][j][k] 
          = 0;
                              
          else {
                                  degree[i][j][k] 
          = degree[i - 1][j - 1][k - 1+ degree[i - 1][j][k - 1]
                                          
          + degree[i - 1][j + 1][k - 1+ degree[i + 1][j - 1][k - 1]
                                          
          + degree[i + 1][j][k - 1+ degree[i + 1][j + 1][k - 1]
                                          
          + degree[i][j - 1][k - 1+ degree[i][j + 1][k - 1];

                              }

                          }
                  }
                  
                  
          //calculate the sum
                  int sum = 0;
                  
          for (int i = 0; i < length; i++)
                      
          for (int j = 0; j < length; j++) {
                          sum 
          += degree[i][j][find.length() - 1];
                          
          if (sum > 1000000000) {
                              
          return -1;
                          }
                      }
                  
          return sum;
              }
          }

          - TestCase
          public class WordPathTest extends TestCase {
              
          public void testCountPaths() {
                  WordPath wordPath 
          = new WordPath();
                  assertEquals(
          2, wordPath.countPaths(new String[] { "ABC""FED""GAI" }, "ABCDEA"));
                  assertEquals(
          0, wordPath.countPaths(new String[] { "ABC""DEF""GHI" }, "ABCD"));
                  assertEquals(
          108, wordPath.countPaths(new String[] { "AA""AA" }, "AAAA"));
                  assertEquals(
          56448, wordPath.countPaths(new String[] { "ABABA""BABAB""ABABA""BABAB""ABABA" },
                          
          "ABABABBA"));
                  assertEquals(
          -1, wordPath.countPaths(new String[] { "AAAAA""AAAAA""AAAAA""AAAAA""AAAAA" },
                          
          "AAAAAAAAAAA"));
                  assertEquals(
          0, wordPath.countPaths(new String[] { "AB""CD" }, "AA"));
              }
          }

          This testcase finished after 0.015 seconds on my pc.

          http://forum.javaeye.com/viewtopic.php?p=106316#106316

          posted on 2005-12-19 16:36 Spirit 閱讀(358) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 沾化县| 丽水市| 凌云县| 兴和县| 梁河县| 盱眙县| 宜君县| 望都县| 太仓市| 上饶市| 红安县| 固始县| 高唐县| 汤原县| 乌兰浩特市| 应城市| 东阳市| 白城市| 壶关县| 西林县| 图们市| 柯坪县| 无锡市| 通化县| 罗江县| 和林格尔县| 南漳县| 共和县| 广饶县| 安顺市| 丰台区| 和林格尔县| 南雄市| 南投市| 霍林郭勒市| 永修县| 霍山县| 慈利县| 南雄市| 山东省| 大余县|