题意:
给定N∗M,N,M≤500的字母迷宫,从(1,1)出发去(n,m),只能向右和向下走
问路径形成的字符串是回文串的路径有多少,答案对MOD取模
分析:
考虑有2个人分别从(1,1)和(n,m)同时出发,在中间相遇
考虑状态f[x1][y1][x2][y2]:=第1个人到(x1,y1),第2个人到(x2,y2)符合条件的路径数
显然O(n4)会T,考虑一个常见优化,由于2个人走的步数是相同的,只要记录步数以及x坐标就可以算出对应的y坐标了
新的状态f[i][x1][x2]:=2个人走了i步,且第1个人的x坐标为x1,第2个人的为x2,的符合条件的路径数
转移就枚举,第1个人向右向下,第2个人向上向左,4种转移即可
统计答案的时候需要枚举相遇的点,显然ans+=∑ni=1f[step][i][i]
但是对于路径步数是偶数的,还可以ans+=∑ni=1f[step][i][i+1]
O(n3)的空间会炸,滚动数组优化一下就好了
代码:
//
// Created by TaoSama on 2016-01-30
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 500 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m;
char s[N][N];
int f[2][N][N];
void add(int &x, int y) {
if((x += y) >= MOD) x -= MOD;
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
int p = 0;
memset(f[p], 0, sizeof f[p]);
f[p][1][n] = s[1][1] == s[n][m];
int step = (n + m - 2) / 2;
for(int i = 1; i <= step; ++i) {
memset(f[!p], 0, sizeof f[!p]);
for(int x1 = 1; x1 <= i + 1; ++x1) {
for(int x2 = n; x2 >= n - i; --x2) {
int y1 = i + 2 - x1, y2 = n + m - i - x2;
if(s[x1][y1] != s[x2][y2]) continue;
add(f[!p][x1][x2], f[p][x1][x2]);
add(f[!p][x1][x2], f[p][x1][x2 + 1]);
add(f[!p][x1][x2], f[p][x1 - 1][x2]);
add(f[!p][x1][x2], f[p][x1 - 1][x2 + 1]);
}
}
p = !p;
}
int ans = 0;
for(int i = 1; i <= n; ++i) {
add(ans, f[p][i][i]);
if(n + m & 1) add(ans, f[p][i][i + 1]);
}
printf("%d\n", ans);
}
return 0;
}
HDU这个题是弱化版,不用考虑路径长度是偶数的情况
代码:
//
// Created by TaoSama on 2016-01-30
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 500 + 10, INF = 0x3f3f3f3f, MOD = 5201314;
int n, m;
char s[N][N];
int f[2][N][N];
void add(int &x, int y) {
if((x += y) >= MOD) x -= MOD;
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
int p = 0;
memset(f[p], 0, sizeof f[p]);
f[p][1][n] = s[1][1] == s[n][n];
for(int i = 1; i < n; ++i) {
memset(f[!p], 0, sizeof f[!p]);
for(int x1 = 1; x1 <= i + 1; ++x1) {
for(int x2 = n; x2 >= n - i; --x2) {
int y1 = i + 2 - x1, y2 = 2 * n - i - x2;
if(s[x1][y1] != s[x2][y2]) continue;
add(f[!p][x1][x2], f[p][x1][x2]);
add(f[!p][x1][x2], f[p][x1][x2 + 1]);
add(f[!p][x1][x2], f[p][x1 - 1][x2]);
add(f[!p][x1][x2], f[p][x1 - 1][x2 + 1]);
}
}
p = !p;
}
int ans = 0;
for(int i = 1; i <= n; ++i) add(ans, f[p][i][i]);
printf("%d\n", ans);
}
return 0;
}