forked from AadeshSalecha/Zonal-Computing-Olympiad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLittle Red Riding Hood.cpp
90 lines (81 loc) · 1.87 KB
/
Little Red Riding Hood.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
89
90
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <climits>
using namespace std;
int main(void)
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n, m, row, col, str, a, b;
cin >> n >> m;
int grid[n][n];
bool safe[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
cin >> grid[i][j];
safe[i][j] = false;
}
for(int i = 0; i < m; i++)
{
cin >> row >> col >> str;
for(int j = row-1-str; j < row+str; j++)
for(int k = col-1-str; k < col+str; k++)
{
if((j >= 0 and j < n) and (k >= 0 and k < n))
if(abs(row-1-j) + abs(col-1-k) <= str)
safe[j][k] = true;
}
}
int new_grid[n][n];
for(int i = n-1; i >= 0; i--)
for(int j = n-1; j >= 0; j--)
{
if(i == n-1 and j == n-1)
{
new_grid[i][j] = grid[i][j];
continue;
}
new_grid[i][j] = -123456;
if(!safe[i][j])continue;
if(i == n-1) // bottom row
{
if(safe[i][j+1])
new_grid[i][j] = grid[i][j] + new_grid[i][j+1];
}
else if(j == n-1)
{
if(safe[i+1][j]) // bottom safe
{
new_grid[i][j] = grid[i][j] + new_grid[i+1][j];
}
}
else
{
if(safe[i][j+1] and safe[i+1][j]) // both safe
{
new_grid[i][j] = grid[i][j] + max(new_grid[i][j+1], new_grid[i+1][j]);
}
else if(safe[i+1][j]) // one safe
{
new_grid[i][j] = grid[i][j] + new_grid[i+1][j];
}
else if(safe[i][j+1]) // other safe
{
new_grid[i][j] = grid[i][j] + new_grid[i][j+1];
}
}
}
// for(int i = 0; i < n; i++)
// {
// for(int j = 0; j < n; j++)
// cout << new_grid[i][j] << " ";
// cout << endl;
// }
if(new_grid[0][0] > -100000)
cout << "YES\n" << new_grid[0][0] << endl;
else
cout << "NO\n";
}