【动态规划】TopCoder SRM 573 division2 WolfPackDivTwo
2013-03-25 10:03
507 查看
比赛的时候没做出来,当时的思路是计算组合数,这个思路应该也没错,但是有一些情况我不会求。比如走m步回到原地的方案数,到现在也没想明白……
参考答案是动态规划的方式,将原问题分解成一个个小的问题,避开了求组合数,同时利用备忘录的方法大大减小了时间复杂度。
我的代码:
参考答案是动态规划的方式,将原问题分解成一个个小的问题,避开了求组合数,同时利用备忘录的方法大大减小了时间复杂度。
我的代码:
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #define MOD 1000000007 using namespace std; const int dir_x[] = {0, 1, 0, -1}; const int dir_y[] = {1, 0, -1, 0}; // Divide original problem into smaller problems // And then using dynamic programming algorithm class WolfPackDivTwo { public: // dx, dy, m int f[55][55][55]; int calc(vector <int>, vector <int>, int); // Get the result of dx, dy, m int get(int dx, int dy, int m){ // Boundary conditions if(m == 0){ if(dx==0 && dy==0) return 1; else return 0; // return (dx==0 && dy==0); } if(f[dx][dy][m] == -1){ f[dx][dy][m] = 0; for(int i=0; i<4; i++){ int tx = abs(dx+dir_x[i]); int ty = abs(dy+dir_y[i]); if(tx+ty <= m-1) f[dx][dy][m] = (f[dx][dy][m]+get(tx, ty, m-1))%MOD; } } return f[dx][dy][m]; } }; int WolfPackDivTwo::calc(vector <int> x, vector <int> y, int m) { int length = x.size(); long long res = 0; // Initialization for(int i=0; i<55; i++) for(int j=0; j<55; j++) for(int k=0; k<55; k++) f[i][j][k] = -1; // For every position for(int i=-51; i<105; i++) for(int j=-51; j<105; j++){ long long tmp = 1; for(int k=0; k<length; k++){ if(abs(x[k]-i)+abs(y[k]-j) > m){ tmp = 0; break; } else{ tmp = (tmp*get(abs(x[k]-i), abs(y[k]-j), m))%MOD; } } res = (res+tmp)%MOD; } return res; } //<%:testing-code%> //Powered by [KawigiEdit] 2.0!
相关文章推荐
- Topcoder SRM 573 WolfPackDivTwo
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- TopCoder SRM 646 Div2 Problem 500 - TheGridDivTwo (BFS + 优先队列)
- Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划
- topcoder-SRM565-div2-第二题-500分--搜索/动态规划
- topcoder SRM 619 DIV2 GoodCompanyDivTwo
- TopCoder SRM 727 Div2 500-point TwoDiagonals
- topcoder-srm-604-div2
- topcoder SRM 625 DIV2 B
- topcoder srm 704 div1
- Topcoder SRM 660 DIV1 500 Privateparty(数学)
- Topcoder SRM 701 Div2 900 ThueMorseGame
- Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论
- Topcoder SRM 628 DIV 2
- TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%
- TopCoder 250 points 12-SRM 149 DIV 2 103.92/250 41.57%
- TopCoder 250 points 14-SRM 150 DIV 2 86.25/250 34.50%
- [topcoder]SRM 633 DIV 2
- TopCoder 350 points 21-SRM 154 DIV 1 105/350 30%
- SRM 583 Div Level Two:IDNumberVerification