如图


代码如下:
#include<bits/stdc++.h>
#include<bits/extc++.h>
//#define int long long
#define lowbit(x) (x&-x)
#define ls(x) st[(x)<<1]
#define rs(x) st[((x)<<1)+1]
using namespace std;
//using namespace __gnu_pbds;
//tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> tr;
using ll=long long;
using ull=unsigned long long;
using i128=__int128_t;
using u128=__uint128_t;
//inline int ask_rk(int x)
//{
// return tr.order_of_key(x)+1;
//}
//inline int ask_val(int x)
//{
// return tr.find_by_order(x-1);
//}
//inline int ask_pre(int x)
//{
// return *(--tr.lower_bound(x));
//}
//inline int ask_nxt(int x)
//{
// return *(tr.upper_bound(x));
//}
//inline int power(int x,int y)
//{
// int res=1;
// while (y)
// {
// if (y&1)
// res=res*x%p;
// x=x*x%p;
// y>>=1;
// }
// return res;
//}
int n,m,k,dp[105][105][20],pre[105][105],a[105][105];
char s[110];
inline void print(int x,int y)
{
if (x==n)
{
cout<<y<<'\n';
return ;
}
if (pre[x][y])
print(x+1,y-1),cout<<'R';
else
print(x+1,y+1),cout<<'L';
}
signed main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%d %d %d\n",&n,&m,&k);
for (int i=1;i<=n;i++)
{
scanf("%[^\n]\n",s);
for (int j=0;j<m;j++)
a[i][j+1]=s[j]-'0';
}
memset(dp,-1,sizeof dp);
for (int i=1;i<=m;i++)
dp[n][i][a[n][i]%(k+1)]=a[n][i];
for (int i=n;i>1;i--)
for (int j=1;j<=m;j++)
for (int l=0;l<=k;l++)
if (dp[i][j][l]!=-1)
{
if (dp[i-1][j-1][(l+a[i-1][j-1]%(k+1))%(k+1)]<dp[i][j][l]+a[i-1][j-1])
dp[i-1][j-1][(l+a[i-1][j-1]%(k+1))%(k+1)]=dp[i][j][l]+a[i-1][j-1],pre[i-1][j-1]=0;
if (dp[i-1][j+1][(l+a[i-1][j+1]%(k+1))%(k+1)]<dp[i][j][l]+a[i-1][j+1])
dp[i-1][j+1][(l+a[i-1][j+1]%(k+1))%(k+1)]=dp[i][j][l]+a[i-1][j+1],pre[i-1][j+1]=1;
}
int ma=-1,mai=0;
for (int i=1;i<=m;i++)
{
if (dp[1][i][0]>ma)
ma=dp[1][i][0],mai=i;
}
cout<<ma<<'\n';
if (ma!=-1)
print(1,mai);
return 0;
}