题目链接:点击打开链接

给定n*m 的矩阵 常数k

以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示

问:

从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0

每一个格子仅仅能向↖或↗走一步

求:最大的路径和

最后一行的哪个位置作为起点

从下到上的路径

思路:

简单dp

#include <cstdio>
#include <algorithm>
#include<iostream>
#include<string.h>
#include <math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define N 105
#define inf 10000000
#define ll int
int n,m,k;
int dp[N][N][12];
int px[N][N][12], py[N][N][12], sum[N][N][12];
int mp[N][N];
vector<pair<int,int> >ans;
int main(){
int i, j, z;
while(~scanf("%d %d %d",&n,&m,&k)){
k++;
memset(sum, -1, sizeof sum);
memset(px, -1, sizeof px);
memset(py, -1, sizeof py);
memset(dp, 0, sizeof dp);
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
scanf("%1d",&mp[i][j]);
for(i = 1; i <= m; i++)
dp[n][i][mp[n][i] % k] = 1, sum[n][i][mp[n][i] % k] = mp[n][i]; for(i = n-1; i ; i--){
for(j = 1; j <= m; j++) {
int x = i+1, y = j-1;
if(y>=1)
{
for(z = 0; z <= k; z++)
if(dp[x][y][z] && sum[i][j][(z+mp[i][j])%k] < sum[x][y][z]+mp[i][j])
{
dp[i][j][(z+mp[i][j])%k] = 1;
px[i][j][(z+mp[i][j])%k] = x;
py[i][j][(z+mp[i][j])%k] = y;
sum[i][j][(z+mp[i][j])%k] = sum[x][y][z]+mp[i][j];
}
}
y = j+1;
if(y<=m)
{
for(z = 0; z <= k; z++)
if(dp[x][y][z] && sum[i][j][(z+mp[i][j])%k] < sum[x][y][z]+mp[i][j])
{
dp[i][j][(z+mp[i][j])%k] = 1;
px[i][j][(z+mp[i][j])%k] = x;
py[i][j][(z+mp[i][j])%k] = y;
sum[i][j][(z+mp[i][j])%k] = sum[x][y][z]+mp[i][j];
}
}
}
}
int posx = 1, posy = -1, mod = 0, anssum = -1;
for(i = 1; i <= m; i++)
if(dp[1][i][0] && anssum<sum[1][i][0])
posy = i, anssum = sum[1][i][0];
if(posy==-1){puts("-1");continue;}
ans.clear();
while(posy!=-1) {
ans.push_back(pair<int,int>(posx, posy));
int tx = px[posx][posy][mod];
int ty = py[posx][posy][mod];
mod = ((mod-mp[posx][posy])%k+k)%k;
posx = tx, posy = ty;
}
cout<<anssum<<endl; int x = ans[ans.size()-1].first, y = ans[ans.size()-1].second;
cout<<y<<endl;
for(i = ans.size()-2; i >= 0; i--){
int nowx = ans[i].first, nowy = ans[i].second;
if(nowy>y)
printf("R");
else printf("L");
x = nowx, y = nowy;
}
puts("");
}
return 0;
}

Codeforces 41D Pawn 简单dp的更多相关文章

  1. Codeforces 279C - Ladder - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/279/C 题意: 给出 $n$ 个整数 $a[1 \sim n]$,$m$ 个查询,对于一个查询 $[l_ ...

  2. Codeforces 180C - Letter - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/180/C 题意: 有一段字符串,包含大小写字母,每次可以将其中一个字母由大写变成小写,或者小写变成大写.要 ...

  3. Codeforces 698A - Vacations - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/698/A 题意: 有 $n$ 天假期,每天有四种情况:0.体育馆不开门,没有比赛:1.体育馆不开门,有比赛 ...

  4. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  5. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  6. Codeforces Round #302 (Div. 2) C. Writing Code 简单dp

    C. Writing Code Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/prob ...

  7. Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. Hibernate与iBATIS的比较

    1.出身 hibernate 是当前最流行的o/r mapping框架,它出身于sf.net,现在已经成为jboss的一部分了. ibatis 是另外一种优秀的o/r mapping框架,目前属于ap ...

  2. Objective-c 中的变量

    OC中的语言变量,按作用域可分为两种:局部变量和全局变量. 局部变量:也称为内部变量,局部变量是在方法内部声明的.其作用域仅限于方法内,离开该方法再使用这个变量就是非法的. 全局变量:也称为外部变量, ...

  3. HTML,JAVASCRIPT代码美化demo

    看见别人的博客里面的源码展示十分漂亮,一时兴起,就自己做了个. 当然,网上已经有别人做好的非常完善的codemirror.highlight.prettify.而我在写自己的这个小demo之前呢,也没 ...

  4. C# 课堂总结3-语句

    一.顺序语句 二.条件,分支语句 1.if语句 关键是能够熟练运用 if的嵌套.要考虑好所有的情况. 如果说 条件是两种+情况相互对应的,那么就可以只用 if 与else .但必须要想好 每个else ...

  5. CUG 数学进阶

    题目链接:http://acm.cug.edu.cn/JudgeOnline/contest.php?cid=1047 . . . I 题目链接:http://acm.cug.edu.cn/Judge ...

  6. Git Version recovery command introduction - git reset

    reset命令有3种方式: git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 git re ...

  7. inline-block 垂直居中

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Bootstrap 响应式瀑布流 (使用wookmark)

    使用瀑布布局 官方 http://www.wookmark.com/jquery-plugin GitHub https://github.com/GBKS/Wookmark-jQuery  (下载后 ...

  9. ajax Session失效如何跳转到登录页面

    在Struts应用中,我们发出的请求都会经过 相应的拦截器进行相关处理,一般都会有一个用户登录拦截(Session失效拦截):一般请求的话,如果Session失效时,我们会跳到登录页面,可是如果我们采 ...

  10. MapList 自己封装的

    //// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)/ ...