空军

skyiv studio

导航

Google中国编程挑战赛资格赛

Google™ Code Jam 中国编程挑战赛资格赛2005年12月12日中午12时-13日中午12时在线举行,参赛者会抽到5组题中的1组,每组题有2道难度不同的题目,分数各为250和750,难度高的题目分数较高。参赛者有60分钟可以完成全部的2题或其中1题。我抽到的是第3组题,以237.97分的成绩晋级下一轮比赛,在入围的500名参赛者中排名第189位。

Qualification Groups 3/7/15/17/23 Problem 250 Statement

You have several identical balls that you wish to place 
in several baskets. Each basket has the same maximum capacity. You are given an int baskets, the number of baskets you have. You are given an int capacity, the maximum capacity of each basket. Finally you are given an int balls, the number of balls to sort into baskets. Return the number of ways you can divide the balls into baskets. If this cannot be done without exceeding the capacity of the baskets, return 0.
Each basket 
is distinct, but all balls are identical. Thus, if you have two balls to place into two baskets, you could have (02), (11), or (20), so there would be three ways to do this.

Definition

Class:
FillBaskets
Method:
countWays
Parameters:
intintint
Returns:
int
Method signature:
int countWays(int baskets, int capacity, int balls)
(be sure your method 
is public)

Constraints
-
baskets will be between 
1 and 5, inclusive.
-
capacity will be between 
1 and 20, inclusive.
-
balls will be between 
1 and 100, inclusive.

Examples

0)

2
20
2
Returns: 
3
The example from the problem statement.

1)

3
20
1
Returns: 
3
We have only 
1 ball, so we must choose which of the three baskets to place it in.

2)

3
20
2
Returns: 
6
We can place both balls 
in the same basket (3 ways to do this), or one ball in each of two baskets (3 ways to do this).

3)

1
5
10
Returns: 
0
We have more balls than our basket can hold.

4)

4
5
10
Returns: 
146

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.
 1 public class FillBaskets
 2 {
 3   public int countWays(int baskets, int capacity, int balls)
 4   {
 5     if (baskets * capacity < balls) return 0;
 6     if (baskets == 1return 1;
 7     int n = 0;
 8     int b = (balls < capacity ? balls : capacity);
 9     for (int i = 0; i <= b; i++)
10     {
11       n += countWays(baskets - 1, capacity, balls - i);
12     }
13     return n;
14   }
15 }

 250分的题比较简单,使用递归可以了。

Qualification Groups 3/7/15/17/23 Problem 750 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,000return -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.
 1 public class WordPath
 2 {
 3   const int MAX = 1000000000;
 4   int[,] g;
 5   int[]  f;
 6   int    flen;
 7   
 8   int paths(int i0, int j0, int k)
 9   {
10     if (k == flen) return 1;
11     int n = 0;
12     for (int i = i0 - 1; i <= i0 + 1; i++)
13     {
14       for (int j = j0 - 1; j <= j0 + 1; j++)
15       {
16         if (i == i0 && j == j0) continue;
17         if (f[k] == g[i, j])
18         {
19           n += paths(i, j, k+1);
20           if (n > MAX) return MAX + 1;
21         }
22       }
23     }
24     return n;
25   }
26   
27   public int countPaths(string[] grid, string find)
28   {
29     flen = find.Length;
30     f    = new int[flen];
31     for (int i = 0; i < flen; i++)
32     {
33       f[i] = find[i];
34     }
35     int rows = grid.Length;
36     int cols = grid[0].Length;
37     g = new int[rows+2, cols+2];
38     for (int i = 0; i < rows; i++)
39     {
40       for (int j = 0; j < cols; j++)
41       {
42         g[i+1, j+1= grid[i][j];
43       }
44     }
45     int n = 0;
46     for (int i = 1; i <= rows; i++)
47     {
48       for (int j = 1; j <= cols; j++)
49       {
50         if (f[0== g[i, j])
51         {
52           n += paths(i, j, 1);
53           if (n > MAX) return -1;
54         }
55       }
56     }
57     return n;
58   }
59 }

750分的题我也使用了递归,结果对 Example 5 的输入计算时间超过2秒而在系统测试阶段被判为0分。资格赛结束后看了 happyyellow 的解答:

 1 using System.Drawing;
 2 using System.Collections;
 3 
 4 public class WordPath
 5 {
 6   public int countPaths(string[] grid, string find)
 7   {
 8     ArrayList[] lists = new ArrayList[26];
 9     for (int i = 0; i < lists.Length; i++)
10     {
11       lists[i] = new ArrayList();
12     }
13     
14     int il = grid.Length;
15     int jl = grid[0].Length;
16     for (int i = 0; i < il; i++)
17     {
18       for (int j = 0; j < jl; j++)
19       {
20         lists[grid[i][j] - 'A'].Add(new Point(i+1, j+1));
21       }
22     }
23     
24     long[,] m = new long[il+2, jl+2];
25     foreach (Point p in lists[find[0- 'A'])
26     {
27       m[p.X, p.Y] = 1;
28     }
29     for (int d = 1; d < find.Length; d++)
30     {
31       long[,] mm = new long[il+2, jl+2];
32       foreach (Point p in lists[find[d] - 'A'])
33       {
34         mm[p.X, p.Y] =
35           m[p.X-1, p.Y+1+ m[p.X, p.Y+1+ m[p.X+1, p.Y+1+
36           m[p.X-1, p.Y  ] +                 m[p.X+1, p.Y  ] +
37           m[p.X-1, p.Y-1+ m[p.X, p.Y-1+ m[p.X+1, p.Y-1];
38       }
39       m = mm;
40     }
41     long n = 0;
42     for (int i = 1; i <= il; i++)
43     {
44       for (int j = 1; j <= jl; j++)
45       {
46         n += m[i, j];
47       }
48     }
49     if (n > 1000000000return -1;
50     return (int)n;
51   }
52 }

posted on 2005-12-15 16:20  空军  阅读(972)  评论(1编辑  收藏  举报