HDU 5617 Jam's maze dp+滚动数组

题目链接:

hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003

题解:

设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5
 6 const int maxn = 505;
 7 const int mod = 5201314;
 8
 9 int dp[maxn][maxn][2];
10 char str[maxn][maxn];
11
12 int n;
13
14 void init() {
15     memset(dp, 0, sizeof(dp));
16 }
17
18 inline int add(int x, int y) {
19     x += y;
20     if (x > mod) x -= mod;
21     return x;
22 }
23
24 int main() {
25     int tc;
26     scanf("%d", &tc);
27     while (tc--) {
28         init();
29         scanf("%d", &n);
30         for (int i = 1; i <= n; i++) scanf("%s", str[i] + 1);
31         if (str[1][1] == str[n][n]) dp[1][n][0] = 1;
32         //printf("dp[1][n][0]:%d\n", dp[1][n][0]);
33         int u = 1;
34         for (int i = 2; i <= n; i++) {
35             for (int j = 1; j <= i; j++) {
36                 for (int k = 1; k <= i; k++) {
37                     int x1, y1, x2, y2;
38                     x1 = j; y1 = i + 1 - x1;
39                     x2 = n + 1 - k; y2 = 2 * n - i + 1 - x2;
40
41                     //滚动数组非常需要注意的地方就是!!!每次都要初始化!!!初始化!!!初始化!!!
42                     dp[x1][x2][u] = 0;
43
44                     //if (i == n&&x1 != x2) continue;
45
46                     if (str[x1][y1] == str[x2][y2]) {
47                         //printf("fuck!");
48                         int &cur = dp[x1][x2][u];
49                         cur = add(cur, dp[x1][x2][!u]);
50                         cur = add(cur, dp[x1][x2 + 1][!u]);
51                         cur = add(cur, dp[x1 - 1][x2][!u]);
52                         cur = add(cur, dp[x1- 1][x2 + 1][!u]);
53                     }
54                 }
55             }
56             u = !u;
57         }
58         int ans = 0;
59         for (int i = 1; i <= n; i++) ans = add(ans, dp[i][i][!u]);
60         printf("%d\n", ans);
61     }
62     return 0;
63 }

HDU 5617 Jam's maze dp+滚动数组

时间: 2024-10-08 05:34:57

HDU 5617 Jam's maze dp+滚动数组的相关文章

HDU 5617 Jam&#39;s maze(DP)

题目链接:点击打开链接 题意:给你一个n*n的矩阵.  求从(1,1)走到(n,n)所组成的回文串个数. 思路:一开始傻逼把状态写成了d[x][y][s],s表示一个串, 用map存的, 后来发现极不可行, 因为这个状态简直太大了, 包括了s串的所有情况. 只是相当于一个dfs中的剪枝罢了. 后来想到, 其实串是不必记录的, 我们只要统计个数, 所以不妨在DP的过程中就判断回文串的情况, 那么就需要同时记录两头的情况.  为了不爆内存, 将状态表示成d[i][x1][x2], 表示走了i步, 左

HDU 5617 Jam&#39;s maze 巧妙DP

题意:给你一个字符矩阵,从(1,1)到(n,n)有很多种走法,每一种走法形成一个字符串,问有多少种走法形成的字符串是回文的 分析:(粘贴BC题解) 的是回文串,有人会想到后缀数组自动机马拉车什么的,其实只要求方案数很多,所以我们应该想到动态规划,首先是状态的定义,我们可以想着从(1,1)(1,1)和(n,n)(n,n)开始走,然后走到相遇.所以我们定义状态可以定义为f[x_1][y_1][x_2][y_2]f[x?1??][y?1??][x?2??][y?2??]表示所对应的两个点(x_1,y_

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

HDU - 4576 Robot(概率dp+滚动数组)

题意:所有的格子围成一个圈,标号为1~n,若从格子1出发,每次指令告知行走的步数,但可能逆时针也可能顺时针走,概率都是1/2,那么问走了m次指令后位于格子l~r(1≤l≤r≤n)的概率. 分析: 1.因为m次指令后不知道会走到哪,会有很多种可能,但是知道从哪里出发,所以起始状态是已知的,在最初的状态,位于格子1是必然的,概率为1. 2.本题应用滚动数组,因为每次指令后都会延伸出无数种可能,这些可能是在前一种状态的基础上延伸的,而且延伸过后前一种状态的值不再有意义,完全可以被当前状态所覆盖. 3.

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the