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

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

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

    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 閱讀(349) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品123区在线观看| 亚洲熟妇成人精品一区| xxxx日本在线播放免费不卡| 青草青草视频2免费观看| 黑人粗长大战亚洲女2021国产精品成人免费视频| 青青青青青青久久久免费观看| 亚洲国产成AV人天堂无码| 日本亚洲中午字幕乱码| 成人A片产无码免费视频在线观看| 成人毛片免费播放| 亚洲综合中文字幕无线码| 国产一精品一AV一免费孕妇| 国产亚洲精品自在久久| 亚洲熟妇自偷自拍另欧美| 成人在线视频免费| 国产成人 亚洲欧洲| 亚洲国产精品综合久久网络| 两个人的视频www免费| 婷婷亚洲综合五月天小说| 1000部拍拍拍18勿入免费凤凰福利| 精品久久亚洲中文无码| 日韩一级免费视频| 一级毛片a免费播放王色 | 暖暖在线日本免费中文| 亚洲avav天堂av在线网毛片| 四虎影在线永久免费观看| 两个人的视频www免费| 老汉色老汉首页a亚洲| 四虎影视www四虎免费| 亚洲av永久无码精品秋霞电影影院| 福利免费在线观看| 亚洲综合久久成人69| a毛片免费全部播放完整成| 麻豆亚洲av熟女国产一区二| 四虎影视免费在线| 久久99免费视频| 亚洲日韩精品无码AV海量| 国产亚洲一区二区三区在线观看| 青青青国产在线观看免费网站| 麻豆安全免费网址入口| 亚洲欧洲日韩不卡|