Description

 

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.

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 ( 2n100). 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

  1. 2
  2. 1 1
  3. 1 1
  4. 3
  5. 1 1 1
  6. 1 1 1
  7. 2 1 1
  8. 0

Sample Output

  1. 2
  2. 3
    思路:要求是要关于那条线对称的,所一把上半角和下半角叠加起来,然后求到那条线的最短路即可,用迪杰斯特拉求。建图也比较简单,就是每个点向四个方向的点连边
    。在求最短路的时候开一个数组记录当前走到该点的最短路有多少条就行,最后求到斜边点上等于最短路的种数和即可。
    复杂度n*n
  1. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<string.h>
  5. 5 #include<math.h>
  6. 6 #include<queue>
  7. 7 #include<vector>
  8. 8 using namespace std;
  9. 9 int ma[200][200];
  10. 10 typedef struct pp
  11. 11 {
  12. 12 int x;
  13. 13 int y;
  14. 14 int cost;
  15. 15 int id;
  16. 16 bool flag;
  17. 17 } ss;
  18. 18 const int mod=1e9+9;
  19. 19 typedef long long LL;
  20. 20 LL sum[10005];
  21. 21 LL d[10005];
  22. 22 bool flag[10005];
  23. 23 ss node[10005];
  24. 24 vector<ss>vec[10005];
  25. 25 int dd[200][200];
  26. 26 void dj(int n,int id);
  27. 27 int main(void)
  28. 28 {
  29. 29 int i,j,k;
  30. 30 while(scanf("%d",&k),k!=0)
  31. 31 {
  32. 32 memset(dd,-1,sizeof(dd));
  33. 33 memset(flag,0,sizeof(flag));
  34. 34 memset(sum,0,sizeof(sum));
  35. 35 for(i=0;i<10005;i++)
  36. 36 vec[i].clear();
  37. 37 for(i=0; i<k; i++)
  38. 38 {
  39. 39 for(j=0; j<k; j++)
  40. 40 {
  41. 41 scanf("%d",&ma[i][j]);
  42. 42 }
  43. 43 }
  44. 44 for(i=0; i<k; i++)
  45. 45 {
  46. 46 for(j=0; j<(k-i); j++)
  47. 47 {
  48. 48 if(i+j!=k-1)
  49. 49 {
  50. 50 ma[i][j]+=ma[k-j-1][k-i-1];
  51. 51 }
  52. 52 }
  53. 53 }
  54. 54 int id=0;
  55. 55 for(i=0; i<k; i++)
  56. 56 {
  57. 57 for(j=0; j<(k-i); j++)
  58. 58 {
  59. 59 if(i+j==k-1)
  60. 60 {
  61. 61 node[id].flag=true;
  62. 62 node[id].x=i;
  63. 63 node[id].y=j;
  64. 64 node[id].id=id;
  65. 65 }
  66. 66 else
  67. 67 {
  68. 68 node[id].flag=false ;
  69. 69 node[id].x=i;
  70. 70 node[id].y=j;
  71. 71 node[id].id=id;
  72. 72 }
  73. 73 dd[i][j]=id;
  74. 74 if(i-1>=0)
  75. 75 {
  76. 76 ss cc;
  77. 77 cc.x=i-1;
  78. 78 cc.y=j;
  79. 79 cc.id=dd[i-1][j];
  80. 80 cc.cost=ma[i-1][j];
  81. 81 vec[id].push_back(cc);
  82. 82 cc.x=i;
  83. 83 cc.y=j;
  84. 84 cc.id=dd[i][j];
  85. 85 cc.cost=ma[i][j];
  86. 86 vec[dd[i-1][j]].push_back(cc);
  87. 87 }
  88. 88 if(j-1>=0)
  89. 89 {
  90. 90 ss cc;
  91. 91 cc.x=i;
  92. 92 cc.y=j-1;
  93. 93 cc.id=dd[i][j-1];
  94. 94 cc.cost=ma[i][j-1];
  95. 95 vec[id].push_back(cc);
  96. 96 cc.x=i;
  97. 97 cc.y=j;
  98. 98 cc.id=dd[i][j];
  99. 99 cc.cost=ma[i][j];
  100. 100 vec[dd[i][j-1]].push_back(cc);
  101. 101 }
  102. 102 if(i+1<k&&dd[i+1][j]!=-1)
  103. 103 {
  104. 104 ss cc;
  105. 105 cc.x=i+1;
  106. 106 cc.y=j;
  107. 107 cc.id=dd[i+1][j];
  108. 108 cc.cost=ma[i+1][j];
  109. 109 vec[id].push_back(cc);
  110. 110 cc.x=i;
  111. 111 cc.y=j;
  112. 112 cc.id=dd[i][j];
  113. 113 cc.cost=ma[i][j];
  114. 114 vec[dd[i+1][j]].push_back(cc);
  115. 115 }
  116. 116 if(j+1<k&&dd[i][j+1]!=-1)
  117. 117 {
  118. 118 ss cc;
  119. 119 cc.x=i;
  120. 120 cc.y=j+1;
  121. 121 cc.id=dd[i][j+1];
  122. 122 cc.cost=ma[i][j+1];
  123. 123 vec[id].push_back(cc);
  124. 124 cc.x=i;
  125. 125 cc.y=j;
  126. 126 cc.id=dd[i][j];
  127. 127 cc.cost=ma[i][j];
  128. 128 vec[dd[i][j+1]].push_back(cc);
  129. 129 }
  130. 130 id++;
  131. 131 }
  132. 132 }
  133. 133 dj(0,id);
  134. 134 LL maxx=1e18;
  135. 135 for(i=0; i<id; i++)
  136. 136 {
  137. 137 if(node[i].flag)
  138. 138 {
  139. 139 if(maxx>d[i])
  140. 140 {
  141. 141 maxx=d[i];
  142. 142 }
  143. 143 }
  144. 144 }
  145. 145 LL akk=0;
  146. 146 for(i=0; i<id; i++)
  147. 147 {
  148. 148 if(maxx==d[i]&&node[i].flag)
  149. 149 {
  150. 150 akk=akk+sum[i];
  151. 151 akk%=mod;
  152. 152 }
  153. 153 }
  154. 154 printf("%lld\n",akk);
  155. 155 }
  156. 156 return 0;
  157. 157 }
  158. 158 void dj(int n,int id)
  159. 159 {
  160. 160 int i,j,k;
  161. 161 fill(d,d+10005,1e9);
  162. 162 d[n]=ma[0][0];
  163. 163 memset(flag,0,sizeof(flag));
  164. 164 while(true)
  165. 165 {
  166. 166 int l=-1;
  167. 167 for(i=0; i<id; i++)
  168. 168 {
  169. 169 if((l==-1||d[i]<d[l])&&flag[i]==false)
  170. 170 {
  171. 171 l=i;
  172. 172 }
  173. 173 }
  174. 174 if(l==-1)
  175. 175 {
  176. 176 return ;
  177. 177 }
  178. 178 flag[l]=true;
  179. 179 ss ask=node[l];
  180. 180 int x=ask.x;
  181. 181 int y=ask.y;
  182. 182 int ac=ask.id;
  183. 183 if(l==0)
  184. 184 {
  185. 185 sum[l]=1;
  186. 186 }
  187. 187 else
  188. 188 {
  189. 189
  190. 190 for(i=0; i<vec[ac].size(); i++)
  191. 191 {
  192. 192 ss pp=vec[ac][i];
  193. 193 if(d[pp.id]+(LL)ma[x][y]==d[l])
  194. 194 sum[l]=sum[pp.id]+sum[l];
  195. 195 sum[l]%=mod;
  196. 196 }
  197. 197 }
  198. 198 for(i=0; i<vec[ac].size(); i++)
  199. 199 {
  200. 200 ss pp=vec[ac][i];
  201. 201 if(d[pp.id]>d[l]+pp.cost)
  202. 202 d[pp.id]=d[l]+pp.cost;
  203. 203 }
  204. 204 }
  205. 205 }

Optimal Symmetric Paths(UVA12295)的更多相关文章

  1. FAQ: Automatic Statistics Collection (文档 ID 1233203.1)

    In this Document   Purpose   Questions and Answers   What kind of statistics do the Automated tasks ...

  2. Contest2073 - 湖南多校对抗赛(2015.04.06)

    Contest2073 - 湖南多校对抗赛(2015.04.06) Problem A: (More) Multiplication Time Limit: 1 Sec  Memory Limit:  ...

  3. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

  4. POJ2112 Optimal Milking (网络流)(Dinic)

                                             Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  5. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  6. POJ 2112 Optimal Milking (Dinic + Floyd + 二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19456   Accepted: 6947 ...

  7. POJ2112 Optimal Milking

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 17811   Accepted: 6368 ...

  8. Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)

      Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19347   Accepted: 690 ...

  9. POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 20262   Accepted: 7230 ...

随机推荐

  1. 20-Integer to Roman-Leetcode

    比较简单的思路:用map存放各个位的数字到罗马字符的映射 然后从个位依次遍历高位加上映射即可. Given an integer, convert it to a roman numeral. Inp ...

  2. Python中的随机采样和概率分布(二)

    在上一篇博文<Python中的随机采样和概率分布(一)>(链接:https://www.cnblogs.com/orion-orion/p/15647408.html)中,我们介绍了Pyt ...

  3. Flink(三)【核心编程】

    目录 一.Environment 二.Source 从集合读取数据 从文件读取数据 从kakfa读取数据(常用) 自定义数据源 三.Transform map Rich版本函数 flatMap key ...

  4. CentOS7 搭建maven私服Nexus

    下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...

  5. 【STM32】基于正点原子『探索者』开发板的烧录

    项目需要一个功能,开发板范例正好有,就买了一块,不过还是有点贵 我手边没有J-Link 用的都是串口烧录 烧录时,先打开右上的开关 如果是仿真器烧录,它无法供电,需要接12V适配器或是杜邦线供电 然后 ...

  6. Java Web 实现Mysql 数据库备份与还原

    前段时间某某删库事故付出的惨重代价告诉我们: 数据备份的必要性是企业数据管理极其重要的一项工作. 1. Mysql备份与还原命令 备份命令: mysqldump -h127.0.0.1 -uroot ...

  7. Linux基础命令---vmstat显示虚拟内存状态

    vmstat vmstat指令用来显示虚拟内存使用状态,同时也可以显示进程.cpu活动情况.vmstat报告有关进程.内存.分页.块IO.陷阱和CPU活动的信息.生成的第一份报告给出了自上次重新启动以 ...

  8. java实现链式线性表

    package ch9; public class LinkList <T>{ private class Node { //保存节点的数据 private T data; //指向下一个 ...

  9. SpringAOP浅析

    1.问题 问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设计,此处不讲解,缺点一荣俱荣,一损 ...

  10. entfrm-boot开发平台一览【entfrm开源模块化无代码开发平台】

    介绍 entfrm-boot是一个以模块化为核心的无代码开发平台,能够让中小企业快速从零搭建自己的开发平台:开箱即用,可插拔可自由组合:以模块化的方式,最大化的代码复用,避免重复开发:无代码可视化开发 ...