简单动态规划题。用取模实现第一行与最后一行连续,注意取字典序即可。

我的解题代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; #define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a>b)?a:b)
#define UL(i) ((i+M-1)%M)
#define DL(i) ((i+1)%M)
#define Maxm 12
#define Maxn 105
const int INF = 0x7fffffff; int M,N;
int Table[Maxm][Maxn];
int MinWeight[Maxm][Maxn], RightRow[Maxm][Maxn];
bool vis[Maxm][Maxn]; int dp(int i, int j)
{
if(vis[i][j]) return MinWeight[i][j]; vis[i][j] = true; if(j==N-1) return MinWeight[i][j] = Table[i][j]; int tmp = min ( dp(UL(i),j+1), min ( dp(i,j+1), dp(DL(i),j+1)));
RightRow[i][j] = Maxm;
if(tmp == MinWeight[i][j+1]) RightRow[i][j] = i;
if(tmp == MinWeight[UL(i)][j+1] && UL(i)<RightRow[i][j]) RightRow[i][j] = UL(i);
if(tmp == MinWeight[DL(i)][j+1] && DL(i)<RightRow[i][j]) RightRow[i][j] = DL(i); return MinWeight[i][j] = tmp+Table[i][j];
}
int main()
{
while(cin >> M >> N)
{
for(int i=0; i<M; i++)
for(int j=0; j<N; j++)
cin >> Table[i][j];
memset(vis,false,sizeof(vis)); int nMinWeight = INF, iRow;
for(int i=0; i<M; i++)
{
if(nMinWeight > dp(i,0))
{
nMinWeight = MinWeight[i][0];
iRow = i;
}
}
cout << iRow+1;
for(int j=0; j<N-1; j++) cout << ' ' << (iRow = RightRow[iRow][j])+1;
cout << endl << nMinWeight << endl;
}
return 0;
}

最新文章

  1. ios 配置https
  2. Cloneable接口和Object的clone()方法
  3. java 26 - 8 网络编程之 TCP协议上传图片
  4. Android之判断当前指定App是否在前台
  5. Linux时间同步配置方法
  6. MySQL 5.5.35 单机多实例配置详解
  7. javascript设计模式-享元模式
  8. 如何删除Oracle数据库
  9. return break continue 的区别
  10. flex中为控件添加监听器并计算
  11. 从源码理解Spring原理,并用代码实现简易Spring框架
  12. startup alter.log spfile.ora
  13. iOS -- Effective Objective-C 阅读笔记 (7)
  14. 如何解决 React 官方脚手架不支持 Less 的问题
  15. windows内核驱动内存管理之Lookaside使用
  16. [转]Anatomy of a Program in Memory
  17. 6E - 寒冰王座
  18. B - Dropping tests
  19. 学习Java并发的课程
  20. 摄像头驱动OV7725学习笔记连载(二):0V7725 SCCB时序的实现之寄存器配置

热门文章

  1. Event Delivery: The Responder Chain(事件传递,响应链)
  2. [转] Linux下查看用户列表
  3. id名和class名有什么区别
  4. 利用eclipse开发php&lt;转&gt;
  5. Android Studio Gradle 版本不同报错解决方法
  6. 微软分布式缓存 appfabric
  7. 【译】addEventListener 第二个参数
  8. iOS9中通过UIStackView实现类似大众点评中的效果图
  9. php中函数不确定参数个数时获取所有参数
  10. 软件测试 homework1