题目
数据
3 3
3 0 4
2 1 0
1 3 4
8
RDRD
5 5
6 7 2 0 2
8 8 5 4 0
8 2 8 4 1
2 2 3 6 8
8 7 7 6 6
32
RRRRDDDD
20 20
14 30 33 16 20 24 38 44 0 16 13 7 4 16 15 18 39 45 32 24
1 42 33 11 48 4 20 13 35 10 38 37 7 38 43 25 13 17 37 9
36 23 11 24 17 17 14 42 21 4 33 24 6 15 5 48 21 27 42 6
15 34 43 34 40 24 34 49 29 5 48 20 11 12 17 45 40 20 13 7
40 44 47 39 18 35 0 47 15 32 34 46 38 46 3 24 7 31 10 37
44 41 8 39 14 29 49 19 21 4 34 41 5 21 5 21 13 2 23 32
14 41 7 1 20 24 6 8 13 25 27 30 42 8 10 5 24 18 48 15
21 0 27 2 32 36 40 15 38 16 17 36 16 20 12 48 47 32 40 46
7 31 28 43 6 21 14 25 21 25 9 16 25 18 17 46 6 25 19 21
42 9 10 13 44 41 20 3 29 30 46 35 0 9 7 15 11 12 16 37
41 8 42 21 36 17 32 18 30 22 49 17 41 45 2 28 1 2 19 7
18 28 21 16 46 35 14 24 10 46 16 29 3 9 31 43 13 9 18 36
26 45 21 34 46 38 42 1 34 39 1 21 18 19 11 20 13 1 14 9
19 16 18 4 16 18 30 15 10 3 6 9 23 24 14 49 9 35 22 25
19 15 30 2 20 43 2 18 30 29 16 11 31 22 10 48 22 28 40 31
29 37 19 3 16 9 25 6 13 12 12 37 10 28 4 27 33 8 48 22
34 25 26 17 17 21 36 10 20 9 43 2 49 34 43 10 39 12 6 9
37 13 25 10 37 41 8 33 22 18 6 17 46 11 6 34 43 7 8 25
7 14 11 38 9 3 40 41 45 29 11 7 33 46 9 11 28 33 14 45
33 31 4 45 27 7 20 29 19 38 10 7 5 16 47 1 43 29 4 23
540
RRRRRRRRRRRRDDRRDDDDDDDRRDRDDDDDDRDDDR
思路
不难发现,当横坐标纵坐标都为0的时候,他只有一种走法
而横坐标纵坐标均不为0的时候,可以从上走也可以从左走,我们只需要比较两种走路的情况谁走的路比较小,然后选择走路比较小的选择,对应再加一个步数即可
代码
#include <iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int func()
{
}
int main()
{
int arr[200][200];
string brr[200][200];
int crr[200][200];
int M,N;
cin >> M >> N;//M是y N 是x
for(int i = 0; i <M; i++)
{
for(int j = 0; j < N; j++)
{
cin >> arr[i][j];
brr[i][j] = "";
}
}
crr[0][0]=arr[0][0];
for(int y = 0; y <M; y++)
{
for(int x = 0; x < N; x++)
{
int temp1;
int temp2;
if(y == 0 && x == 0)
{
continue;
}
if(y == 0)
{
temp1 = crr[y][x - 1] + arr[y][x];
crr[y][x] = temp1;
brr[y][x] = brr[y][x-1] + "R";
}
else if(x == 0)
{
temp1 = crr[y-1][x] + arr[y][x];
crr[y][x] = temp1;
brr[y][x] = brr[y-1][x] + "D";
}
else
{
temp1 = crr[y - 1][x] + arr[y][x];
temp2 = crr[y][x - 1] + arr[y][x];
int flag;
if(temp1 < temp2)
flag = true;
else flag = false;
if(flag)
{
crr[y][x] = temp1;
brr[y][x] = brr[y - 1][x] + "D";
}
else
{
crr[y][x] = temp2;
brr[y][x] = brr[y][x - 1] + "R";
}
}
}
}
cout << crr[M-1][N-1]<<endl << brr[M-1][N-1];
return 0;
}