zoukankan      html  css  js  c++  java
  • 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
    ",ans[n][m]);
        //system("pause");
        return 0;
    }
  • 相关阅读:
    蒙特卡洛法(随即取样法) 数模 笔记
    【数模学习】Matlab 符号微积分 计算微分、雅可比矩阵、不定积分与定积分、求解微分方程
    Length of Last Word
    基于视频深度学习的人物行为识别 资料汇总
    3S比赛预定
    求解一元多次方程 牛顿迭代法
    LeetCode | Climbing Stairs
    LeetCode | Palindrome Number
    LeetCode | Merge Sorted Array
    LeetCode | Valid Palindrome
  • 原文地址:https://www.cnblogs.com/dyzll/p/5775569.html
Copyright © 2011-2022 走看看