TopCoder: SRM472 DIV2 1000

Problem Statement

     Taro is standing on a rectangular island. The island is divided into width x height cells. The coordinate system is introduced so that cell in the bottom left corner has coordinates (0, 0) and cell in the top right corner has coordinates (width-1, height-1). Initially he is standing on cell (xy). 



He decides to take a random walk. Each step consists of walking one cell in a randomly chosen direction. Only the four cardinal directions are allowed, and each direction has an equal probability of being chosen on each step. If he steps off the island, he will fall into the sea. Taro's walk will consist of exactly steps steps, unless he falls into the sea before he finishes walking. 



Return the probability that he will still be standing on the island after steps steps.

Definition

    
Class: RectangularIsland
Method: theProbablity
Parameters: int, int, int, int, int
Returns: double
Method signature: double theProbablity(int width, int height, int x, int y, int steps)
(be sure your method is public)
    
 

Notes

- The returned value must have an absolute or relative error less than 1e-9.

Constraints

- width will be between 1 and 1000, inclusive.
- height will be between 1 and 1000, inclusive.
- x will be between 0 and width - 1, inclusive.
- y will be between 0 and height - 1, inclusive.
- steps will be between 1 and 5000, inclusive.

Examples

0)  
    
5
8
4
3
1
Returns: 0.75
If Taro chooses up, down or left, he will remain on the island.
1)  
    
5
8
4
7
1
Returns: 0.5
If Taro chooses down or left, he will remain on the island.
2)  
    
2
2
0
1
5
Returns: 0.03125
From any cell, the probability of remaining after one step is 1/2, so the answer is (1/2)^5.
3)  
    
58
85
47
74
1000
Returns: 0.13056659769966347
 
4)  
    
1000
1000
123
456
5000
Returns: 0.9868611148475199
 

我用的是一个O(height*width*steps)的算法,大数据超时,暂时想不到什么好的法子

 1 // BEGIN CUT HERE

 2 

 3 // END CUT HERE

 4 #include <functional>

 5 #include <algorithm>

 6 #include <stdexcept>

 7 #include <iostream>

 8 #include <sstream>

 9 #include <fstream>

10 #include <iomanip>

11 #include <cstdlib>

12 #include <cstring>

13 #include <utility>

14 #include <cctype>

15 #include <vector>

16 #include <string>

17 #include <bitset>

18 #include <queue>

19 #include <stack>

20 #include <ctime>

21 #include <list>

22 #include <map>

23 #include <set>

24 #include <math.h>

25 

26 using namespace std;

27 

28 #define pb push_back

29 #define INF 100000000000

30 #define L(s) (int)((s).size())

31 #define FOR(i,a,b) for (int _n(b), i(a); i<=_n; i++)

32 #define rep(i,n) FOR(i,1,(n))

33 #define rept(i,n) FOR(i,0,(n)-1)

34 #define C(a) memset((a), 0, sizeof(a))

35 #define ll long long

36 #define VI vector<int>

37 #define ppb pop_back

38 #define mp make_pair

39 #define MOD 1000000007

40 int toInt(string s){ istringstream sin(s); int t; sin>>t;return t;}

41 

42 class RectangularIsland

43 {

44         public:

45         double theProbablity(int width, int height, int x, int y, int steps)

46         {

47             vector<vector<double> > prob(width, vector<double>(height));

48             rept(i, width) {

49                 rept(j, height) {

50                     prob[i][j] = 1.0;

51                 }

52             }

53             vector<vector<double> > pre(width, vector<double>(height));

54             for (int k = 1; k <= steps; k++) {

55                 pre = prob;

56                 for (int i = 0; i < width; i++) {

57                     for (int j = 0; j < height; j++) {

58                         double tmp = 0.0;

59                         if (i != 0) tmp += pre[i-1][j]*0.25;

60                         if (i != width-1) tmp += pre[i+1][j]*0.25;

61                         if (j != 0) tmp += pre[i][j-1]*0.25;

62                         if (j != height-1) tmp += pre[i][j+1]*0.25;

63                         prob[i][j] = tmp;

64                     }

65                 }

66             }

67             return prob[x][y];

68         }

69 

70 // BEGIN CUT HERE

71     public:

72     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }

73     private:

74     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }

75     void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }

76     void test_case_0() { int Arg0 = 5; int Arg1 = 8; int Arg2 = 4; int Arg3 = 3; int Arg4 = 1; double Arg5 = 0.75; verify_case(0, Arg5, theProbablity(Arg0, Arg1, Arg2, Arg3, Arg4)); }

77     void test_case_1() { int Arg0 = 5; int Arg1 = 8; int Arg2 = 4; int Arg3 = 7; int Arg4 = 1; double Arg5 = 0.5; verify_case(1, Arg5, theProbablity(Arg0, Arg1, Arg2, Arg3, Arg4)); }

78     void test_case_2() { int Arg0 = 2; int Arg1 = 2; int Arg2 = 0; int Arg3 = 1; int Arg4 = 5; double Arg5 = 0.03125; verify_case(2, Arg5, theProbablity(Arg0, Arg1, Arg2, Arg3, Arg4)); }

79     void test_case_3() { int Arg0 = 58; int Arg1 = 85; int Arg2 = 47; int Arg3 = 74; int Arg4 = 1000; double Arg5 = 0.13056659769966347; verify_case(3, Arg5, theProbablity(Arg0, Arg1, Arg2, Arg3, Arg4)); }

80     void test_case_4() { int Arg0 = 1000; int Arg1 = 1000; int Arg2 = 123; int Arg3 = 456; int Arg4 = 5000; double Arg5 = 0.9868611148475199; verify_case(4, Arg5, theProbablity(Arg0, Arg1, Arg2, Arg3, Arg4)); }

81 

82 // END CUT HERE

83 

84 };

85 // BEGIN CUT HERE

86 int main()

87 {

88         RectangularIsland ___test;

89         ___test.run_test(-1);

90         return 0;

91 }

92 // END CUT HERE

 

你可能感兴趣的:(topcoder)