forked from AadeshSalecha/Zonal-Computing-Olympiad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSave Spaceman Spiff.cpp
38 lines (36 loc) · 1.26 KB
/
Save Spaceman Spiff.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
cin >> n >> m >> K;
bool safe[n][m]; memset(safe, true, sizeof(safe));
for(long long i = 0; i < K; i++)
{
cin >> row >> col >> start >> freq;
row--; col--;
for (int j = 0; j < n; j++) {
// same row
int cur = j+row;
int norm = cur-abs(j-col);
if(norm >= start and (norm-start) % freq == 0)
safe[row][j] = false;
}
for (int j = 0; j < m; j++) {
// same col
int cur = j + col;
int norm = cur-abs(j-row);
if(norm >= start and (norm-start) % freq == 0)
safe[j][col] = false;
}
}
long long grid[n][m];
for(long long i = n-1; i >= 0; i--)
for(long long j = n-1; j >= 0; j--)
{
grid[i][j] = -1024;
if(safe[i][j] and i == n-1 and j == n-1) grid[i][j] = 1;
else if(i == n-1 and safe[i][j] and grid[i][j+1] == 1) grid[i][j] = 1;
else if(j == n-1 and safe[i][j] and grid[i+1][j] == 1) grid[i][j] = 1;
else if(safe[i][j] and (grid[i+1][j] == 1 or grid[i][j+1] == 1)) grid[i][j] = 1;
}
if(grid[0][0] > 0)
cout << "YES\n" << n-1+m-1 << endl;
else
cout << "NO\n";
}