uva 12295 Optimal Symmetric Paths__DP

原创 2012年03月23日 04:59:19

Optimal Symmetric Paths 

You have a grid of n rows and n columns. Each of the unit squares contains a non-zero digit. Youwalk from the top-left square to the bottom-right square. Each step, you can move left, right, up ordown to the adjacent square (you cannot move diagonally), but you cannot visit a square more thanonce. There is another interesting rule: your path must be symmetric about the line connecting thebottom-left square and top-right square. Below is a symmetric path in a6 x 6 grid.

\epsfbox{p12295.eps}

Your task is to find out, among all valid paths, how many of them have the minimal sum of digits?

Input 

There will be at most 25 test cases. Each test case begins with an integer n (2$ \le$n$ \le$100). Each of thenext n lines contains n non-zero digits (i.e. one of 1, 2, 3, ..., 9). These n2 integers are the digits in thegrid. The input is terminated by a test case withn = 0, you should not process it.

Output 

For each test case, print the number of optimal symmetric paths, modulo 1,000,000,009.

Sample Input 

2
1 1
1 1
3
1 1 1
1 1 1
2 1 1
0

Sample Output 

2
3

#include<stdio.h>

#define N 101
#define MAX 1000000009
/*
对路径长度和数量同时DP,
策略是对角线向起终点两端扩展;
*/
int dp[N][N],n,pat[N][N];
int solve()
{
    int i,j,k,f;
    for(i=0;i<n;++i)
            pat[i][i]=1;
    for(k=1;k<n;++k)
        for(i=k,j=0;i<n;++i,++j)
            {
                f=dp[i][j+1]-dp[i-1][j];
                dp[i][j]+=dp[j][i];
                if(f>0)
                {
                    pat[i][j]=pat[i-1][j];
                    dp[i][j]+=dp[i-1][j];
                }
                else if(f<0)
                {
                    pat[i][j]=pat[i][j+1];
                    dp[i][j]+=dp[i][j+1];
                }
                else
                {
                    pat[i][j]=(pat[i][j+1]+pat[i-1][j])%MAX;
                    dp[i][j]+=dp[i][j+1];
                }
            }
    return pat[n-1][0];
}
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        int i,j;
        for(i=n-1;i>=0;--i)
            for(j=0;j<n;++j)
                scanf("%d",&dp[i][j]);
        printf("%d\n",solve());
    }
    return 0;
}


[最短路]uva 12295 Optimal Symmetric Paths

/** uva 12295 Optimal Symmetric Paths__DP 正解,最短路 */ #include #include #include #include #include...

UVA 12295 Optimal Symmetric Paths

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3716 ...
  • yzwdwt
  • yzwdwt
  • 2014年03月29日 14:38
  • 323

DP优化——四边形不等式 uva 10304 - Optimal Binary Search Tree

// 原文地址 http://www.cnblogs.com/zxndgv/archive/2011/08/02/2125242.html 今天学习了一下四边形不等式,这个东西很早以前就看过,但是...
  • count24
  • count24
  • 2012年02月01日 20:17
  • 311

UVa 348 Optimal Array Multiplication Sequence (DP 最优矩阵链乘)

题意   求给定数目矩阵的最优链乘  并输出路径 矩阵链乘在小白P170有讲解 也是经典的动态规划 有了转移方程题目就好做了   设d[i][j]为矩阵i到矩阵j的最优链乘 初始状态d[i][i]=...
  • acvay
  • acvay
  • 2014年08月22日 20:37
  • 424

UVA 10304 Optimal Binary Search Tree (区间dp)

题目大意:二叉搜索树的左子树的节点一定比当前节点小,右子树的的节点一定比当前节点大。 要求建一棵二叉查找树,使得总的cost最低。 sum = f(e1)*cost(e1) + f(e2)*cos...

UVA(348)——Optimal Array Multiplication Sequence(区间dp)

题意: 就是矩阵链乘,然后让你输出符合最小运算量的路径。 思路: 矩阵链乘的状态转移方程是:...

uva-10304 Optimal Binary Search Tree(区间dp)

题意 给一个序列即可 S = (e1,e2,...,en),且e1

uva 10564 Paths through the Hourglass (DP)

uva 10564 Paths through the Hourglass Paths through the Hourglass Input: Standard Input Output: S...

uva 10564 Paths through the Hourglass(DP)

Problem F Paths through the Hourglass Input: Standard Input Output: Standard Output Time Limit:...

Numbering Paths (Uva 125 floyd+dp思想)

题意:给出一张图,求每两个点之间的不同路径的条数,按照矩阵输出,若i到j有无数条则mp[i][j]=-1. 思路:关键是怎样判断无数条,考虑:若一个点经过一条路可以回来,即mp[i][i]>0,那么...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:uva 12295 Optimal Symmetric Paths__DP
举报原因:
原因补充:

(最多只允许输入30个字)