题目链接:点击打开链接

给定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. BZOJ 1114 Number theory(莫比乌斯反演+预处理)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738 题意:给你一个整数序列a1, a2, a3, ... , ...

  2. 理解Ajax

    1.优化原则 优化的目的是希望降低程序的整体开销.虽然在程序中有许多因素可以优化,但是通常人们会认为这个开销就是程序的执行时间.其实我们更应该把重点放在对程序整体开销最大的那部分.   2.一切都是权 ...

  3. 创建android 模拟器并在cmd中打开

    因为在运行monkeyrunner之前必须先运行相应的模拟器或连接真机,否则monkeyrunner无法连接到设备,运行模拟器有两种方法:1.通过eclipse中执行模拟器 2.在CMD中通过命令调用 ...

  4. 循环-10. 求序列前N项和(15)

    #include<iostream>#include<iomanip>using namespace std;int main(){    double i,n,t,a,b;  ...

  5. Page Cache, the Affair Between Memory and Files

    Previously we looked at how the kernel manages virtual memory for a user process, but files and I/O ...

  6. Lotus Sametime 服务器的安装和配置

    IBM Lotus Sametime 是一款强大的实时协作软件,目前最新版本是 7.5.1.通过它,您不仅能够进行网络聊天,而且可以方便地召开网络会议.在网络社区中与其他人进行沟通.了解更多关于 Lo ...

  7. js学习笔记第一课(js基础知识)

    1.js代码在浏览器中执行. 2.js代码直接插入网页中需包含在 <script language="javascript"> js代码 </script> ...

  8. RFID电子标签的二次注塑封装

    生活当中,RFID电子标签具有明显的优势,随着RFID电子标签成本的降低.读写距离的提高.标签存储容量增大及处理时间缩短的发展趋势,R F I D电子标签的应用将会越来越广泛. RFID电子标签的应用 ...

  9. Python的MySQLdb模块安装,连接,操作,增删改

    1. 首先确认python的版本为2.3.4以上,如果不是需要升级python的版本     python -V   检查python版本 2. 安装mysql, 比如安装在/usr/local/my ...

  10. [Windows编程] #pragma once 和#ifndef ... #define ... #endif 比较

    C++中防止头文件被多次include 的常见方式有: 1) 用#ifndef ...  #define ... #endif  宏 #ifndef __MYHEADER_H__#define __M ...