Calculator-42397 2019ICPC Nanjing C-Digital Path (memorized search)

Zhe the bully, is condemned by all kinds of evil, like bullying those who are weaker. His teammates have been mistreated for a long time. Finally, they decided not to put up with their buddy any more and flee to Digital Village, with the bully in hot pursuit. Due to difficult terrain and a considerable amount of Digital Paths staggered, they can't be easily arrested.

Getting familiar with the terrain as soon as possible is important for these innocents to escape the threat of bullying. All they need now is to count the number of Digital Paths in Digital Village.

To simplify the problem, Digital Village is abstracted into a grid with nn rows and mm columns filled by integers. A Digital Path is a continuous walk in the grid satisfying the following conditions:

  • adjacent boxes in the walk share a common edge;
  • the walk is maximal, which cannot be extended;
  • the walk contains at least four boxes;
  • going from one end to the other, the increment of values for any two adjacent boxes is exactly one.

Here we have some examples.

The path in Figure 1 is invalid because its length is less than 44.

The path in Figure 2 is invalid because it is not continuous.

The path in Figure 3 is invalid because it can be extended further.

The path in Figure 4 is also invalid because values in the path are not strictly increased by one.

Digital Paths may partially overlap. In Figure 5, there are 44 Digital Paths marked by different colours.

Input

The first line contains two positive integers nn and m (1≤n,m≤1000)m (1≤n,m≤1000) describing the size of the grid.

Each of the next nn lines contains mm integers, the jj-th of which, denoted by ai,j (−107≤ai,j≤107)ai,j (−107≤ai,j≤107), represents the value of the box in the ii-th row and the jj-th column.

Output

Output the number of Digital Paths modulo (109+7)(109+7).

Sample Input

3 5
1 2 3 8 7
-1 -1 4 5 6
1 2 3 8 7

Sample Output

4

Sample Input 2

4 4
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1

Sample Output 2

16

Title:

Ask how many paths in the matrix satisfy these conditions:

(1) The length is at least 4

(2) The path cannot be extended

(3) The weight of points on the path increases by 1 every step

(4) The two points must be adjacent to each other or up and down

Ideas:

Memory search dp [x] [y] [len] (1 \ leq len \ leq 4) means the (x, y) number of paths with length len to the point  . Paths with length >= 4 are marked as length 4.

Transfer equation:, dp[x][y][len] += dp[fx][fy][len - 1]where the point  (x, y) is (fx, fy) adjacent to the point  and mp [x] [y] = mp [fx] [fy] + 1

If len == 4, dp[x][y][len] can also be made  dp[fx][fy][len] over the transfer, that is, dp[x][y][4] += dp[fx][fy][4]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1005;

ll mp[N][N], dp[N][N][5];
int n, m, dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

//判是不是路径终点
bool check1(int x, int y) {
    for(int i = 0; i < 4; ++i) {
        int fx = x + dir[i][0];
        int fy = y + dir[i][1];
        if(fx >= 1 && fx <= n && fy >= 1 && fy <= m && mp[fx][fy] == mp[x][y] + 1) return 0;
    }
    return 1;
}

//判是不是路径起点
bool check2(int x, int y) {
    for(int i = 0; i < 4; ++i) {
        int fx = x + dir[i][0];
        int fy = y + dir[i][1];
        if(fx >= 1 && fx <= n && fy >= 1 && fy <= m && mp[fx][fy] == mp[x][y] - 1) return 0;
    }
    return 1;
}

ll dfs(int x, int y, int len) {
    if(len == 1) {
        if(check2(x, y)) dp[x][y][len] = 1;
        else dp[x][y][len] = 0;
        return dp[x][y][len];
    }
    if(dp[x][y][len] != -1) return dp[x][y][len] % mod;
    ll res = 0;
    for(int i = 0; i < 4; ++i) {
        int fx = x + dir[i][0];
        int fy = y + dir[i][1];
        if(fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] != mp[x][y] - 1) continue;
        res = (res + dfs(fx, fy, len - 1)) % mod;
        if(len == 4) res = (res + dfs(fx, fy, len)) % mod;
    }
    dp[x][y][len] = res % mod;
    return dp[x][y][len];
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            scanf("%lld", &mp[i][j]);
    ll ans = 0;
    memset(dp, -1, sizeof(dp));
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            if(mp[i][j] != -1 && check1(i, j)) {
                ans = (ans + dfs(i, j, 4)) % mod;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}
/*
2 4
1 2 3 4
3 3 4 1
*/

 

Guess you like

Origin blog.csdn.net/weixin_43871207/article/details/110938876