Codeforces 41D Pawn 简单dp
题目链接:点击打开链接
给定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的更多相关文章
- Codeforces 279C - Ladder - [简单DP]
题目链接:http://codeforces.com/problemset/problem/279/C 题意: 给出 $n$ 个整数 $a[1 \sim n]$,$m$ 个查询,对于一个查询 $[l_ ...
- Codeforces 180C - Letter - [简单DP]
题目链接:http://codeforces.com/problemset/problem/180/C 题意: 有一段字符串,包含大小写字母,每次可以将其中一个字母由大写变成小写,或者小写变成大写.要 ...
- Codeforces 698A - Vacations - [简单DP]
题目链接:http://codeforces.com/problemset/problem/698/A 题意: 有 $n$ 天假期,每天有四种情况:0.体育馆不开门,没有比赛:1.体育馆不开门,有比赛 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- setInterval()与clearInterval()的用法
setInterval() 方法可按照指定的周期来调用函数或计算表达式. --简单地说就是过一段时间调用一次该函数 setInterval() 方法会不停地调用函数,直到 clearInterval ...
- w3c教程
http://www.w3cfuns.com/course.php http://www.w3cfuns.com/home.php?mod=space&uid=5434413&do=b ...
- 编写一个程序, 将 a.txt 文件中的单词与 b.txt 文件中的 单词交替合并到 c.txt 文件中, a.txt 文件中的单词用回车符 分隔, b.txt 文件中用回车或空格进行分隔。
package cn.itcast; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public ...
- HDU 2841 Visible Trees(莫比乌斯反演)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...
- c 语言 指针 与地址
1.如何实现交换两个数的值 void swap( int *a,int *b) { int tep=*a;//*a其实就是主函数a的值,a是主函数存a数值的地址. *a =*b; *b =tep; ...
- 12,C++中 .* 可以出现在什么地方?有何作用?
.*运算符表示什么意思?好几次遇到.*,但不知道如何使用.后来发现,可以体现在成员函数指针的调用上. 1,函数指针指向公有非静态的成员函数.此时,必须创建一个对象来调用函数指针. class Cont ...
- JavaEE Tutorials (9) - 运行持久化示例
9.1order应用118 9.1.1order应用中的实体关系119 9.1.2order应用中的主键121 9.1.3实体映射到多个数据库表125 9.1.4order应用中的层叠操作125 9. ...
- ActionBarSherlock学习笔记 第一篇——部署
ActionBarSherlock学习笔记 第一篇--部署 ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...
- android 构建数据库SQLite
1.首先我们需要一个空白的eclipse android工程 2.然后修改AndroidManifest.xml 在<application></application>标签里 ...
- BZOJ 1002 轮状病毒 (基尔霍夫矩阵)
题解:http://vfleaking.blog.163.com/blog/static/17480763420119685112649/ #include <iostream> #inc ...