Skip to content

Instantly share code, notes, and snippets.

@Baekjoon

Baekjoon/1520.cc Secret

Last active August 16, 2018 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Baekjoon/1a2832a35769675b6727 to your computer and use it in GitHub Desktop.
Save Baekjoon/1a2832a35769675b6727 to your computer and use it in GitHub Desktop.
1520
#include <cstdio>
#include <cstring>
int n,m;
int a[500][500];
long long d[500][500];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
long long go(int x, int y) {
if (x == n-1 && y == m-1) {
return 1;
}
if (d[x][y] != -1) {
return d[x][y];
}
long long &ans = d[x][y];
ans = 0;
for (int k=0; k<4; k++) {
int nx = x+dx[k];
int ny = y+dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < m) {
if (a[x][y] > a[nx][ny]) {
ans += go(nx,ny);
}
}
}
return ans;
}
int main() {
scanf("%d %d",&n,&m);
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
scanf("%d",&a[i][j]);
}
}
memset(d,-1,sizeof(d));
printf("%lld\n",go(0,0));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment