UVA 12295 Optimal Symmetric Paths(重复DP)

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 a 6 × 6 grid.

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

There will be at most 25 test cases. Each test case begins with an integer n (2 ≤ n ≤ 100). Each ofthe next n lines contains n non-zero digits (i.e. one of 1, 2, 3, . . . , 9). These n2integers are the digitsin the grid. The input is terminated by a test case with n = 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



题意:

给一个数字n

然后给一个n*n的图的每点权值,求从左上角到右下角权值最小路径的数量。(刚开始以为是最短路径ORZ

要求

路径要关于,沿左下到右上的对角线,对称。

可以上下左右移动


思路:

目前和网上最短路写法(应该没毛病),跑了10000组随机数据没有毛病。

因为有4个方向,DP一次是不够的,这里提供一组数据,结果应该是1,而不是2。

    5
    1 9 1 1 1
    1 1 1 9 1
    9 9 9 1 1
    9 9 9 1 9
    9 9 9 1 1


从左上到右下 DP

多次重复dp找到每点最小路径,和该路径的路径数。

每重复一次说明能往左或往上走一次(因为回头走是为了绕过较大权值点,但是权值最大才9,地图才100*100,所以重复500次应该够了)


详见代码。

#include<stdio.h>
#include<string.h>

#define For(a,b,c) for(int a = b; a <= c; a++)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 100000009
#define INF 0x3f3f3f3f

//v图, ans存每点当前最短路径,path存储每点 的 当前最短路径 的 方案数。
//mov用long long竟然比int快了不少
long long v[105][105], ans[105][105], path[105][105];
int n, mov[4][2] = {0,1,1,0,0,-1,-1,0};
//book表示每点某个方向的path到过没
bool book[105][105][4];

void show() //debug用,可无视
{
    printf("ans:\n");
    For(i,1,n)
    {
        For(j,1,n)
        {
            if(i + j > n + 1) break;
            printf("%3.lld ",ans[i][j]);
        }
        printf("\n");
    }
    printf("path:\n");
    For(i,1,n)
    {
        For(j,1,n)
        {
            if(i + j > n + 1) break;
            printf("%3.lld ",path[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

long long solve()
{
    mem(ans,0x3f);
    mem(path,0);
    mem(book,false);
    ans[1][1] = v[1][1];
    path[1][1] = 1;
    int x, y;

    For(ToRe,1,500)//重复DP
    {
        For(i,1,n)
        {
            For(j,1,n)
            {
                if(j+i > n) break;
                For(k,0,3)//每个(i,j)点4个移动方向,因为存储从1开始不会越界
                {
                    x = i + mov[k][0];
                    y = j + mov[k][1];
                    
                    //如果该点路径最小值相等,且没有来过
                    if(ans[x][y] == ans[i][j]+v[x][y] && book[x][y][k] == false)
                    {
                        book[x][y][k] = true;
                        path[x][y] = (path[x][y]+path[i][j]) % mod;
                    }
                    if(ans[x][y] > ans[i][j] + v[x][y])//如果刷新最短路径
                    {
                        ans[x][y] = ans[i][j] + v[x][y];
                        book[x][y][0] = book[x][y][1] = book[x][y][2] = book[x][y][3] = false;
                        book[x][y][k] = true;
                        path[x][y] = path[i][j];
                    }
                }
            }
        }
//        show();
    }
    long long maxn = 0x3f3f3f3f3f3f3f3f, sum = 0;
    For(i,1,n) //找到对角线中,最短路径的路径数和
    {
        if(ans[i][n+1-i] == maxn)
        {
            sum = (sum + path[i][n+1-i])%mod;
        }
        if(ans[i][n+1-i] < maxn)
        {
            maxn = ans[i][n+1-i];
            sum = path[i][n+1-i];
        }
    }
    return sum;
}

int main()
{
//freopen("D:\\c\\duipai\\data.txt","r",stdin);
//freopen("D:\\c\\duipai\\myout.txt","w",stdout);
    while(~scanf("%d",&n),n)
    {
        For(i,1,n)
        {
            For(j,1,n)
            {
                scanf("%lld",&v[i][j]);
                if(i+j > n+1) v[n+1-j][n+1-i] += v[i][j];//对角线的右下半全部对称加到左上半
            }
        }
//        For(i,1,n)
//        {
//            For(j,1,n) printf("%3.lld",v[i][j]);
//            printf("\n");
//        }
        printf("%lld\n",solve());
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值