forked from jaichhabra/hactktoberfesttimebois2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EscapeOfIcarus.cpp
87 lines (78 loc) · 2.24 KB
/
EscapeOfIcarus.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
// QUESTION 1: ICARUS’S ESCAPE
// Icarus is about to enter a labyrinth. It is a M x N matrix. Each cell denotes a room. Each room has an enemy with strength Pi. Icarus starts with a strength of X. He can steal the strength of a person if he enters the room they are in, however he can only enter a room ONCE, and ONLY if Icarus has more strength than the person in it. Icarus has to make it from the top left of the maze to the bottom right accumulating as much strength as possible. If it is not possible to reach the end, print -1.
// CONSTRAINTS:
// 2<=m,n<=10
// 1<=Pi , X<=100
// Arr[0][0] = Arr[m-1][n-1] = 0
// INPUT
// First Line contains m and n which denote the rows and columns of the maze
// Second Line contains the strength of enemies
// Third Line contains Icarus’s strength
// OUTPUT
// Output contains 1 line containing an integer.
// EXAMPLES:
// Sample Input 1:
// 3 3
// 0 3 2 6 7 14 3 8 0
// 5
// Sample Output 1:
// 32
// Sample Input 2:
// 3 3
// 0 10 2 10 3 1 1 9 0
// 5
// Sample Output 2:
// -1
// Sample Input 3:
// 3 3
// 0 1 1 1 1 1 1 1 0
// 2
// Sample Output 3:
// 9
// Solution
#include<iostream>
#include<vector>
using namespace std;
void maxStrength(vector<vector<int>> A, int i, int j, int m,int n,int &maxSum,int curSum) {
if(i==m-1 && j==n-1) {
if(curSum > maxSum) maxSum = curSum;
return;
}
//NOT TO ACCESS UNACCESSIBLE MEMORY
if(i<0 || j<0 || i>=m || j>=n) return;
//CHECK IF ALREADY VISITED THE ROOM OR TOO HIGH ENEMY THERE
if(A[i][j] == -1 || A[i][j] >= curSum) return;
//GO THROUGH ALL DIRECTIONS
curSum += A[i][j];
A[i][j]=-1;
//GO UP
maxStrength(A,i-1,j,m,n,maxSum,curSum);
//GO DOWN
maxStrength(A,i+1,j,m,n,maxSum,curSum);
//GO RIGHT
maxStrength(A,i,j+1,m,n,maxSum,curSum);
//GO LEFT
maxStrength(A,i,j-1,m,n,maxSum,curSum);
}
int main() {
int m,n;
//ARRAY SIZE
cin>>m>>n;
vector<vector<int>> A(m,vector<int>(n));
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
//ARRAY INPUTS
cin>>A[i][j];
}
}
//YOUR STRENGTH
int X;
cin>>X;
int Xcopy = X;
//INPUTS DONE
//LOGIC
maxStrength(A,0,0,m,n,X,X);
if(X == Xcopy) cout<<-1;
else cout<<X;
return 0;
}