-
Notifications
You must be signed in to change notification settings - Fork 0
/
41_D.cpp
88 lines (81 loc) · 1.33 KB
/
41_D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<cstring>
using namespace std;
int dp[102][102][1002];
char a[102][102];
char path[102][102][1002];
int m,n,k;
void solve(int r,int c,int k)
{
// cout<<r<<" "<<c<<" "<<k<<endl;
if(dp[r][c][k] != -1)
return;
dp[r][c][k] = 1;
static int dx[] = {-1,-1};
static int dy[] = {-1,1};
for(int i=0;i<2;i++){
int X = r + dx[i];
int Y = c + dy[i];
if(X>=0 && X<m && Y>=0 && Y<n)
{
//dp[X][Y][k+a[X][Y]-48] = 1;
if(i==0)
path[X][Y][k+a[X][Y]-48]='L';
else
path[X][Y][k+a[X][Y]-48]='R';
solve(X,Y,k+a[X][Y]-48);
}
}
}
string retracePath(int x,int y,int K,string p)
{
if(x==m-1)
{
cout<<(y+1)<<endl;
return p;
}
if(path[x][y][K] == 'L')
{
return retracePath(x+1,y+1,K-a[x][y]+48,"L" + p);
}
else
{
return retracePath(x+1,y-1,K-a[x][y]+48,"R" + p);
}
}
int main()
{
cin>>m>>n>>k;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++){
// dp[m-1][i][a[m-1][i] - 48] = 1;
solve(m-1,i,a[m-1][i]- 48);
}
int maxm = -1;
int indy = 0;
for(int i=0;i<n;i++){
for(int j=0;j<1002;j++){
if(dp[0][i][j]!=-1 && j%(k+1)==0)
{
if(j>maxm)
{
maxm = j;
indy = i;
}
}
}
}
if(maxm==-1)
cout<<"-1"<<endl;
else
{
cout<<maxm<<endl;
cout<<retracePath(0,indy,maxm,"")<<endl;
}
return 0;
}