关闭

uva 12295 Optimal Symmetric Paths__DP

457人阅读 评论(0) 收藏 举报

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;
}


0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:329916次
    • 积分:6259
    • 等级:
    • 排名:第3914名
    • 原创:308篇
    • 转载:24篇
    • 译文:0篇
    • 评论:46条
    我的微博
    最新评论