https://nanti.jisuanke.com/t/42397
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
n
rows and
m
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
4
.
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
4
Digital Paths marked by different colours.
Input
The first line contains two positive integers
n
and
m
(
1
≤
n
,
m
≤
1000
)
describing the size of the grid.
Each of the next
n
lines contains
m
integers, the
j
-th of which, denoted by
a
i
,
j
(
−
10
7
≤
a
i
,
j
≤
10
7
)
, represents the value of the box in the
i
-th row and the
j
-th column.
Output
Output the number of Digital Paths modulo
(
10
9
+
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
#include<bits/stdc++.h>
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define endl '\n'
#define ps puts("###")
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn = 1e6+10;
const double eps = 1e-7;
const ll mod = 1e9+7;
const double pi=3.1415926536;
ll t,k,n,m,cnt;
ll a[1010][1010];
ll b[maxn];
ll c[maxn];
ll r[maxn];
ll vis[maxn];
ll p1[maxn],p2[maxn],p3[maxn],p4[maxn];
vector<ll>q[maxn];
void dfs(int i)
{
ll s=0;
if(q[i].size()==0)
{
p1[i]=1;
return;
}
for(int j=0;j<q[i].size();j++)
{
ll u=q[i][j];
if(vis[u]==0)
{
vis[u]=1;
dfs(u);
}
p2[i]+=p1[u];
p2[i]%=mod;
p3[i]+=p2[u];
p3[i]%=mod;
p4[i]+=(p3[u]+p4[u])%mod;
p4[i]%=mod;
}
}
int main()
{
cin>>n>>m;
cnt=0;
for(int i=0;i<=n+1;i++)
{
for(int j=0;j<=m+1;j++)
{
a[i][j]=-1e9-7;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
cnt++;
r[cnt]=0;
vis[cnt]=0;
p1[cnt]=0;
p2[cnt]=0;
p3[cnt]=0;
p4[cnt]=0;
q[cnt].clear();
b[cnt]=i;
c[cnt]=j;
}
}
for(int i=1;i<=cnt;i++)
{
ll x=b[i];
ll y=c[i];
//if(a[x][y]>0)
{
if(a[x][y]==a[x-1][y]-1)
{
r[i-m]++;
q[i].push_back(i-m);
}
if(a[x][y]==a[x][y-1]-1)
{
r[i-1]++;
q[i].push_back(i-1);
}
if(a[x][y]==a[x+1][y]-1)
{
r[i+m]++;
q[i].push_back(i+m);
}
if(a[x][y]==a[x][y+1]-1)
{
r[i+1]++;
q[i].push_back(i+1);
}
}
}
ll ans=0;
for(int i=1;i<=cnt;i++)
{
ll x=b[i];
ll y=c[i];
if(r[i]==0)
{
vis[i]=1;
dfs(i);
ans+=p4[i];
ans%=mod;
}
}
cout<<ans<<endl;
}