• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 12295 - Optimal Symmetric Paths

你为何这么叼!

Optimal Symmetric Paths 

Time limit: 1.000 seconds

 

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

 

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define INF 0x7fffffff
 7 #define maxn 111
 8 #define mod 1000000009
 9 int n,m;
10 int a[maxn][maxn],val[maxn][maxn];
11 int dx[]={1,0,-1,0};
12 int dy[]={0,1,0,-1};
13 struct Node{int x,y;};
14 int ok(int x,int y){
15     if(x>=0&&x<n&&y>=0&&y<n)return 1;
16     return 0;
17 }
18 void bfs(){
19     queue<Node>q;
20     q.push((Node){0,n-1});
21     while(!q.empty()){
22         Node u=q.front();q.pop();
23         for(int k=0;k<4;k++){
24             Node v;
25             v.x=u.x+dx[k];
26             v.y=u.y+dy[k];
27             if(ok(v.x,v.y)&&val[v.x][v.y]>val[u.x][u.y]+a[v.x][v.y]){
28                 q.push(v);
29                 val[v.x][v.y]=val[u.x][u.y]+a[v.x][v.y];
30             }
31         }
32     }
33 }
34 int dfs(int x,int y){
35     int res=0;
36     if(x==0&&y==n-1)return 1;
37     for(int k=0;k<4;k++){
38         Node u;
39         u.x=x+dx[k];
40         u.y=y+dy[k];
41         if(ok(u.x,u.y)&&val[x][y]==val[u.x][u.y]+a[x][y])res=(res+dfs(u.x,u.y))%mod;
42     }
43     return res;
44 }
45 int main(){
46     while(scanf("%d",&n)&&n){
47         for(int i=n-1;i>=0;i--)for(int j=0;j<n;j++)scanf("%d",&a[j][i]);
48         for(int i=0;i<n;i++)for(int j=i+1;j<n;j++)a[i][j]+=a[j][i];
49         for(int i=0;i<n;i++)for(int j=0;j<n;j++)val[i][j]=INF;
50         val[0][n-1]=a[0][n-1];
51         bfs();
52         int mi=INF,ans=0;
53         for(int i=0;i<n;i++)mi=min(mi,val[i][i]);
54         for(int i=0;i<n;i++)if(val[i][i]==mi)ans=(ans+dfs(i,i))%mod;
55         printf("%d\n",ans);
56     }
57     return 0;
58 }
View Code 2013-10-11 10:55:26 

 

posted @ 2013-10-11 10:57  HaibaraAi  阅读(113)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3