uva 12295 - Optimal Symmetric Paths(最短路 + 递推)

  Optimal Symmetric Paths 
You have a grid of n rows and n columns. Each of the unit squares contains a non-zero digit. You walk from the top-left square to the bottom-right square. Each step, you can move left, right, up or down to the adjacent square (you cannot move diagonally), but you cannot visit a square more than once. There is another interesting rule: your path must be symmetric about the line connecting the bottom-left square and top-right square. Below is a symmetric path in a 6 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 the next n lines contains n non-zero digits (i.e. one of 1, 2, 3, ..., 9). These n2 integers are the digits in 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




The Seventh Hunan Collegiate Programming Contest 

Problemsetter: Rujia Liu, Special Thanks: Yiming Li & Jane Alam Jan


找从左上角到右下角的,按y=x对称的路径,最短路径有多少条。
因为是对称的,所以只用找到y=x上的点的路径,可以先按对称性做一个预处理,把对称点的digit加起来。最后做最短路计数。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 100 + 5;
const int mod = 1000000000 + 9;

int m[maxn][maxn];
int dp[maxn][maxn];
int countn[maxn][maxn];
int n;
const int cx[] = {-1, 0, 1, 0};
const int cy[] = {0, 1, 0, -1};

bool valid(int x, int y){
    return x < n && x >= 0 && y < n && y >= 0;
}

struct Node{
    int x, y, dis;
    int lx, ly;

    Node(int x, int y, int dis, int lx, int ly){
        this->x = x;
        this->y = y;
        this->dis = dis;
        this->lx = lx;
        this->ly = ly;
    }

    bool operator < (const Node &a) const{
        return a.dis < dis;
    }
};

void solve(){
    priority_queue<Node> pq;
    for(int i = 0;i < maxn;i++)
        fill(dp[i], dp[i]+maxn, mod);
    countn[0][0] = 1;
    pq.push(Node(0, 0, m[0][0], 0, 0));// !!!
    while(!pq.empty()){
        Node tem = pq.top();pq.pop();
        int x = tem.x;
        int y = tem.y;
        int dis = tem.dis;
        int lx = tem.lx;
        int ly = tem.ly;

        if(dis < dp[x][y]){
            dp[x][y] = dis;
            countn[x][y] = countn[lx][ly];
        }
        else if(dis == dp[x][y]){
            countn[x][y] = (countn[x][y] + countn[lx][ly]) % mod;
            continue;
        }
        else if(dis > dp[x][y]) continue;

        if(x + y == n-1) continue;
        for(int i = 0;i < 4;i++){
            int tx = x + cx[i];
            int ty = y + cy[i];
            if(valid(tx, ty)){
                pq.push(Node(tx, ty, dis+m[tx][ty], x, y));
            }
        }
    }
}

int main(){
    while(scanf("%d", &n) != EOF){
        if(n == 0) break;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                scanf("%d", &m[i][j]);
            }
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n-i-1;j++){
                m[i][j] += m[n-j-1][n-i-1];
            }
        }

        solve();
        int Min = mod;
        for(int i = 0;i < n;i++){
            Min = min(Min, dp[i][n-i-1]);
        }
        int ans = 0;
        for(int i = 0;i < n;i++){
            if(dp[i][n-i-1] == Min)
                ans = (ans + countn[i][n-i-1]) % mod;
        }
        printf("%d\n", ans);
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值