Cow Hopscotch

Cow Hopscotch

题目描述

Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has surprisingly not deterred the cows from attempting to play nearly every afternoon.

The game is played on an R by C grid (2 <= R <= 750, 2 <= C <= 750), where each square is labeled with an integer in the range 1..K (1 <= K <= R*C). Cows start in the top-left square and move to the bottom-right square by a sequence of jumps, where a jump is valid if and only if

1) You are jumping to a square labeled with a different integer than your current square,

2) The square that you are jumping to is at least one row below the current square that you are on, and

3) The square that you are jumping to is at least one column to the right of the current square that you are on.

Please help the cows compute the number of different possible sequences of valid jumps that will take them from the top-left square to the bottom-right square.

输入

The first line contains the integers R, C, and K. The next R lines will each contain C integers, each in the range 1..K.

输出

Output the number of different ways one can jump from the top-left square to the bottom-right square, mod 1000000007.

样例输入

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

样例输出

5
分析:2个难点,状态转移和分治处理;
   状态转移:ans[x][y]=∑ans[i][j](i<x,j<y)-Σans[i][j](i<x,j<y,a[i][j]=a[x][y]);
   分治:由于处理左半部分,上半部分时和右半部分,下半部分没有半毛钱关系,所以分治处理;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,a[maxn][maxn],ans[maxn][maxn],check[maxn*maxn];
void gao(int l,int r)
{
    if(l==r)return;
    int mid=l+r>>1;
    gao(l,mid);
    memset(check,0,sizeof(check));
    int all=0;
    for(int j=1;j<=m;j++)
    {
        for(int i=mid+1;i<=r;i++)
            ans[i][j]=((ans[i][j]+all-check[a[i][j]])%mod+mod)%mod;
        for(int i=l;i<=mid;i++)
            all+=ans[i][j],all%=mod,check[a[i][j]]+=ans[i][j],check[a[i][j]]%=mod;
    }
    gao(mid+1,r);
}
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&k);
    rep(i,1,n)rep(j,1,m)scanf("%d",&a[i][j]);
    ans[1][1]=1;
    gao(1,n);
    printf("%d\n",ans[n][m]);
    //system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/5775569.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值