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 ...
随机推荐
- NSLog 输出文件名、方法名、行号
项目中经常会需要根据日志输出来寻找源代码,通过以下方法可以让它自动输出文件名.方法.行号,非常方便. 找到项目的pch文件,添加以下内容即可: ...为三个英文句号(复制粘贴后可能会变化). /** ...
- WPF之DataGrid应用(转)
原文:http://blog.csdn.net/sanjiawan/article/details/6785394 前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功 ...
- BZOJ 1483: [HNOI2009]梦幻布丁( 链表 + 启发式合并 )
把相同颜色的串成一个链表, 然后每次A操作就启发式合并, 然后计算对答案的影响. ----------------------------------------------------------- ...
- Hadoop 相关问题
1.MR Job 输入非常多,启动map 非常多,如何提高MapTask 启动速度(附加条件:集群很空闲,资源多多): 参考答案: a.重写调度器算法,降低时间复杂度 b.Out-of-bound h ...
- eclipse maven SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder
现象:运行eclipse maven build,console 有红色日志如下: SLF4J: Failed to load class "org.slf4j.impl.StaticLog ...
- codeforces 620F. Xors on Segments
题目链接 定义一种操作f(u, v) = u^u+1^.......^v. (u<=v), 给n个数, q个询问, 每个询问给出一个区间[l, r], 求这个区间里的f(a[i], a[j]) ...
- ipc$爆破密码
FOR /L %%i IN (1,1,99) DO net use \\192.168.1.1\ipc$ /user:test %%i && echo %%i>1.txt
- app 转caf 音频 代码
afconvert /Users/xiaoye/Downloads/cat.caf /Users/xiaoye/Downloads/cat1.caf -d ima4 -f caff -v;
- (Problem 73)Counting fractions in a range
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- SVN 在 linux 下的配置
0.服务器主机需要打开websharing: sudo su (进入root,需要输入密码) apachectl start (没有反应即打开成功) 1.建立想要保存软件仓库的目录 最好在/users ...