poj2704

简单dp

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
using namespace std;

#define maxn 50

char map[maxn][maxn];
long long f[maxn][maxn];
int n;

void input()
{
for (int i = 0; i < n; i++)
scanf(
"%s", map[i]);
}

void work()
{
memset(f,
0, sizeof(f));
f[
0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
for (int k = 0; k < i; k++)
if (map[k][j] - '0' + k == i)
f[i][j]
+= f[k][j];
for (int k = 0; k < j; k++)
if (map[i][k] - '0' + k == j)
f[i][j]
+= f[i][k];
}
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n != -1)
{
input();
work();
printf(
"%lld\n", f[n - 1][n - 1]);
}
return 0;
}

你可能感兴趣的:(poj)