Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int dp[100][100];
- int matrica[100][100];
- int x, y;
- pair<int, int> path[100][100];
- int rec(int i, int j){
- if((i==x-1)and(j==y-1)){
- path[i][j] = make_pair(-1, -1);
- return(matrica[i][j]);
- }
- if(dp[i][j]!=-1){
- return(dp[i][j]);
- }
- int result=0;
- int down = 0, right = 0;
- if(i+1<x){
- down = rec(i+1, j)+matrica[i][j];
- }
- if(j+1<y){
- right = rec(i, j+1)+matrica[i][j];
- }
- result = max(down, right);
- if(down > right) {
- path[i][j] = make_pair(i + 1, j);
- }
- else {
- path[i][j] = make_pair(i, j + 1);
- }
- dp[i][j]=result;
- return(result);
- }
- int main()
- {
- cin>>x>>y;
- for(int i=0; i<x; i++){
- for(int j=0; j<y; j++){
- cin>>matrica[i][j];
- }
- }
- for(int i=0; i<x; i++){
- for(int j=0; j<y; j++){
- dp[i][j]=-1;
- }
- }
- cout<<rec(0, 0) << endl;
- for(pair<int, int> i = make_pair(0, 0); i != make_pair(-1, -1); i = make_pair(path[i.first][i.second].first, path[i.first][i.second].second)) {
- cout << i.first + 1 << " " << i.second + 1 << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement