[UVA][最短路] 12295 - Optimal Symmetric Paths@Morris' Blog|PChome Online 人新台
2013-06-04 08:57:45| 人579| 回2 | 上一篇 | 下一篇

[UVA][最短路] 12295 - Optimal Symmetric Paths

0 收藏 0 0 站台


  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

把正方形折,然後取最短路到角即可。

//折的地方手打,一直 WA


#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#define mod 1000000009
using namespace std;
int n, g[105][105];
int dp[105][105], cnt[105][105], path[105][105];
int inq[105][105];
int dfs(int x, int y) {
    int &v = path[x][y];
    if(inq[x][y])  return v;
    inq[x][y] = 1;
    if(x+1 < n && dp[x][y] == dp[x+1][y]+g[x][y])
        v +=dfs(x+1, y);
    if(v >= mod)    v -= mod;
    if(x-1 >= 0 && dp[x][y] == dp[x-1][y]+g[x][y])
        v += dfs(x-1, y);
    if(v >= mod)    v -= mod;
    if(y+1 < n && dp[x][y] == dp[x][y+1]+g[x][y])
        v += dfs(x, y+1);
    if(v >= mod)    v -= mod;
    if(y-1 >= 0 && dp[x][y] == dp[x][y-1]+g[x][y])
        v += dfs(x, y-1);
    if(v >= mod)    v -= mod;
    return v;
}
int main() {
    int i, j, k;
    while(scanf("%d", &n) == 1 && n) {
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                scanf("%d", &g[i][j]);
        for(i = 0; i < n; i++)
            for(j = 0; i+j+1 < n; j++)
                g[i][j] += g[n-j-1][n-i-1];
        memset(dp, 63, sizeof(dp));
        memset(cnt, 0, sizeof(cnt));
        memset(inq, 0, sizeof(inq));
        memset(path, 0, sizeof(path));
        dp[0][0] = g[0][0], cnt[0][0] = 1;
        path[0][0] = 1;
        queue<int> X, Y;
        int x, y, tx, ty;
        int dx[] = {0,0,1,-1};
        int dy[] = {1,-1,0,0};
        X.push(0), Y.push(0);
        while(!X.empty()) {
            x = X.front(), X.pop();
            y = Y.front(), Y.pop();
            inq[x][y] = 0;
            if(x + y == n-1)    continue;
            for(i = 0; i < 4; i++) {
                tx = x+dx[i], ty = y+dy[i];
                if(tx < 0 || ty < 0 || tx >= n || ty >= n)  continue;
                if(dp[tx][ty] > dp[x][y] + g[tx][ty]) {
                    dp[tx][ty] = dp[x][y] + g[tx][ty];
                    if(inq[tx][ty] == 0) {
                        inq[tx][ty] = 1;
                        X.push(tx), Y.push(ty);
                    }
                }
            }
        }
        int mn = 0xfffffff, ret = 0;
        for(i = 0; i < n; i++)
            mn = min(mn, dp[i][n-i-1]);
        memset(inq, 0, sizeof(inq));
        inq[0][0] = 1;
        for(i = 0; i < n; i++) {
            if(dp[i][n-i-1] == mn) {
                ret += dfs(i, n-i-1);
                if(ret >= mod)
                    ret -= mod;
            }
        }
        printf("%d\n", ret);
    }
    return 0;
}

台: Morris
人(579) | 回(2)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][dp、大] 10328 - Coin Toss
此分上一篇:[UVA] 391 - Mark-up

sexe videos
Thank you..
2018-06-09 23:23:30
sesso videos
Thank you..
2018-06-09 23:24:03
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86