hdoj1428 -- 漫步校园 (记忆化搜索)
生活随笔
收集整理的这篇文章主要介绍了
hdoj1428 -- 漫步校园 (记忆化搜索)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
漫步校园
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3744 Accepted Submission(s): 1133
Input 每组测试数据的第一行为n(2=<n<=50),接下来的n行每行有n个数,代表经过每个区域所花的时间t(0<t<=50)(由于寝室与机房均在三楼,故起点与终点也得费时)。
Output 针对每组测试数据,输出总的路线数(小于2^63)。
Sample Input 3 1 2 3 1 2 3 1 2 3 3 1 1 1 1 1 1 1 1 1
Sample Output 1 6
Author LL
Source ACM暑期集训队练习赛(三)
Recommend linle | We have carefully selected several similar problems for you: 1427 1414 1074 1430 1072 #include <queue> #include <cstdio> #include <cstring> const int MAX = 51; typedef long long LL ; using namespace std;int n; LL dp[MAX][MAX]; int dis[MAX][MAX], G[MAX][MAX]; int ac[4][2]={0, 1, 0, -1, -1, 0, 1, 0};struct Matrix {int x, y; }s, t, r;void bfs() {queue<Matrix> q;s.x=n; s.y=n; dis[n][n]=G[n][n];q.push(s);while(!q.empty()){t=q.front(); q.pop(); for(int i=0; i<4; i++){r.x=t.x+ac[i][0];r.y=t.y+ac[i][1];if(r.x<1 || r.x>n || r.y<1 || r.y>n)continue;if(dis[r.x][r.y]>dis[t.x][t.y]+G[r.x][r.y] || dis[r.x][r.y]==-1){dis[r.x][r.y]=dis[t.x][t.y]+G[r.x][r.y];q.push(r); } } } }LL dfs(int x, int y) {if(dp[x][y])return dp[x][y];if(x==n && y==n)return 1;dp[x][y]= 0;for(int i=0; i<4; i++){int xx= x+ac[i][0];int yy= y+ac[i][1];if(xx> n|| yy>n|| xx <1|| yy < 1|| dis[xx][yy]>=dis[x][y])continue;dp[x][y]+=dfs(xx, yy);}return dp[x][y]; } int main() {while(scanf("%d", &n) != EOF){memset(dis, -1, sizeof(dis));memset(dp, 0, sizeof(dp));for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)scanf("%d", &G[i][j]);bfs();printf("%lld\n", dfs(1, 1));}return 0; }
转载于:https://www.cnblogs.com/soTired/p/5348256.html
总结
以上是生活随笔为你收集整理的hdoj1428 -- 漫步校园 (记忆化搜索)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 软中断amp;taskletamp;工作
- 下一篇: 在Elasticsearch中查询Ter