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

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

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

    Spirit

    2005年12月19日 #

    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 @ 2005-12-19 16:36 Spirit 閱讀(358) | 評論 (0)編輯 收藏

    2005年12月15日 #

    Google Code Jam - SkipStones的一種遞歸算法

    public class SkipStones {
        
    private String water = "X";

        
    public int maxDistance(String water) {
            
    this.water = water;
            
    int max = 0;
            
    int sum = 0;
            
    for (int initial = 1; initial < water.length() + 1; initial++) {
                sum 
    = bounce(0, initial);
                max 
    = (sum > max ? sum : max);
            }
            
    return max;
        }

        
    private int bounce(int startDistance, int bounceDistance) {
            
    if (bounceDistance == 0)
                
    return startDistance;

            
    if ((startDistance + bounceDistance) > water.length())
                
    return -1;

            
    if (water.charAt(startDistance + bounceDistance - 1== 'X')
                
    return startDistance;

            
    return bounce(startDistance + bounceDistance, bounceDistance / 2);
        }

        
    public static void main(String[] args) {
            SkipStones skipStones 
    = new SkipStones();
            System.out.println(skipStones.maxDistance(args[
    0]));
        }
    }

    posted @ 2005-12-15 13:29 Spirit 閱讀(361) | 評論 (1)編輯 收藏

    僅列出標題  
    主站蜘蛛池模板: 亚洲国产精品成人久久久| 成人久久久观看免费毛片| 国产成人aaa在线视频免费观看| 国产成人综合亚洲| 在线亚洲97se亚洲综合在线| 日本免费一区二区三区| 亚洲天堂2017无码中文| 国产啪亚洲国产精品无码| 88av免费观看| 污污污视频在线免费观看| 亚洲成a人片毛片在线| 国产成人高清精品免费鸭子| 久久免费观看国产精品| 国产亚洲美女精品久久久久| 亚洲国产精品久久久久久| 国产美女精品视频免费观看 | 国产亚洲精品精华液| 在线v片免费观看视频| 中文字幕成人免费高清在线视频| 亚洲国产成人精品久久| 国产亚洲精品无码拍拍拍色欲| 免费视频专区一国产盗摄| 中文精品人人永久免费| 亚洲国产中文在线视频| 毛茸茸bbw亚洲人| 天堂在线免费观看中文版| 无码av免费网站| 日本激情猛烈在线看免费观看| 亚洲AV无码乱码在线观看代蜜桃 | 亚洲AV成人潮喷综合网| 亚洲人成伊人成综合网久久久| 久草在视频免费福利| 久久久国产精品福利免费| 精品在线视频免费| 亚洲精品天堂在线观看| 亚洲精品高清国产一久久| 亚洲国产综合精品中文字幕| 卡1卡2卡3卡4卡5免费视频 | 亚洲成年人啊啊aa在线观看| 亚洲人成网站免费播放| 97在线视频免费公开观看|