hdoj Jam's maze 5617 (区间DP&&滚动数组)好题
来源:互联网 发布:澳门网络真人赌博攻略 编辑:程序博客网 时间:2024/06/05 13:28
Jam's maze
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 299 Accepted Submission(s): 136
Problem Description
Jam got into a maze, he has to find a palindrome path from (1,1) to (N,N) to get out.
However he is not only thinking about how to get out of the maze,
but also counting the number of ways he can get out.
The maze aN∗N grid. On each cell is a uppercase letter.
Jam is at upper-left corner(1,1) now, and the exit is at lower-right corner (N,N) .
Jam can only go down or go right.
Considering the number of palindrome path may be huge, you just need to output the number mod5201314 .
However he is not only thinking about how to get out of the maze,
but also counting the number of ways he can get out.
The maze a
Jam is at upper-left corner
Jam can only go down or go right.
Considering the number of palindrome path may be huge, you just need to output the number mod
Input
The first line is T(1≤T≤10) means T Case.
For each case:
The first line isN(1≤N≤500) means the size of the maze.
ThenN lines follow, each line has N uppercase letter.
For each case:
The first line is
Then
Output
For each testcase, output the number of palindrome path mod 5201314 .
Sample Input
14ABCDBEFECDEBGCBA
Sample Output
12Hintthere are 1 solutions is"ABCDCBA"there are 1 solutions is"ABCGCBA"there are 4 solutions is"ABEDEBA"there are 6 solutions is"ABEFEBA"问题描述Jam走进了一个迷宫,他要想走出这个迷宫,必须找到一条路径,使得这条路径是回文的当然他可不屑于去走出这个迷宫,聪明的他一定要找出有多少种方案走出这个迷宫在一个N∗N大小的迷宫,这个迷宫全由大写字母组成他会从左上角走到右下角,然后把所有经过的字符连成一个串,当然只能往下和往右走,问有多少种方案可以走出来当然答案会很大,所以答案和5201314取模输出输入描述第一行T(1≤T≤10),表示T组数据。接下来T组数据:每组数据第一行为N(1≤N≤500)表示矩阵的行和列接下来N行N列N∗N个字符输出描述输入样例14ABCDBEFECDEBGCBA输入样例12输出样例有1种走法是"ABCDCBA"有1种走法是"ABCGCBA"有4种走法是"ABEDEBA"有6种走法是"ABEFEBA"所以共有12=6+4+1=1种走法可行//思路:(看的大神的:http://blog.csdn.net/chenzhenyu123456/article/details/50612893)用到区间DP的思想,用dp[x1][y1][x2][y2]表示从(x1,y1)点到(x2,y2)点之间的方法数,但因为(1<=n<=500),所以这样肯定就会爆,所以就用滚动数组dp[g][x1][x2]表示从x1行到x2行之间的方法数,当然,y1,y2也是要确定的,先确定从x1行到x2行之间的步数,然后就可以确定出y1,y2的值,这样一个一个枚举,最终就可以确定dp[g][x1][x2]之间的步数。最终累加dp[i][i]的值就得到所求结果。#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3f#define ll long long#define N 510#define M 5201314using namespace std;char map[N][N];int dp[2][N][N];int main(){int t,n,m,i,j,k;int s;int x1,x2,y1,y2;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++)scanf("%s",map[i]+1);int g=0;memset(dp,0,sizeof(dp));dp[g][1][n]=(map[1][1]==map[n][n]);for(s=1;s<n;s++)//步数 {g^=1;memset(dp[g],0,sizeof(dp[g]));for(x1=1;x1<=n;x1++){for(x2=n;x2>=1;x2--){if(x1-1>s||n-x2>s)continue;int y1=s-(x1-1)+1;int y2=n-(s-(n-x2));if(map[x1][y1]!=map[x2][y2])continue;dp[g][x1][x2]=(dp[g][x1][x2]+dp[g^1][x1][x2])%M;dp[g][x1][x2]=(dp[g][x1][x2]+dp[g^1][x1][x2+1])%M;dp[g][x1][x2]=(dp[g][x1][x2]+dp[g^1][x1-1][x2])%M;dp[g][x1][x2]=(dp[g][x1][x2]+dp[g^1][x1-1][x2+1])%M;}}}int ans=0;for(i=1;i<=n;i++)ans=(ans+dp[g][i][i])%M;printf("%d\n",ans);}return 0;}
0 0
- hdoj Jam's maze 5617 (区间DP&&滚动数组)好题
- hdoj 5617 Jam's maze(滚动数组dp求回文数)
- hdoj 5617 Jam's maze 【dp】
- HDU 5617 Jam's maze(DP)
- HDU 5617 Jam's maze(dp)
- HDU 5617 Jam's maze(dp)
- HDU5617 Jam's maze(dp)
- hdu 5617 Jam's maze(dp)(BestCoder Round #70)
- HDU 5617:Jam's maze 又被DP教育了
- hdu 5617 Jam's maze(双线程dp)
- hdu 5617 Jam's maze
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes、HDU 5617 Jam's maze(dp)
- hdoj Dire Wolf 5115 (区间DP) 好题
- hdoj ZZY’s Dilemma 4152 (DP) 好题
- poj 1141(区间dp 滚动数组)
- 区间dp+ 滚动数组uva1632
- 蓝桥杯 波动数列 (DP&滚动数组 好题)
- HDOJ 5616 Jam's balance (暴力)
- 39. goodbye
- springmvc+maven环境搭建
- 主键和外键
- PAT-1020 D进制的A+B
- 【leetcode】【26】Remove Duplicates from Sorted Array
- hdoj Jam's maze 5617 (区间DP&&滚动数组)好题
- iOS/OS X 内存管理(二):借助工具解决内存问题
- wait and waitpid
- Please verify that your device’s clock is properly set, and that your signing certificate is not exp
- 十七、Uiautomator 快速调试
- karaf 报错 Caused by: org.eclipse.jetty.io.EofException: early EOF
- 前台extjs后台 java poi 读取excel数据到数据库中
- En(2016-02)En渐入佳境
- HDU 1011 士兵杀敌取脑 (树形dp)