Guohe Cu solution to a problem [P1002]

text

Briefly describe the meaning of the questions:

Soldiers want to cross the river, and every time he could walk down a grid, a grid can also go to the right, Tajima step up to the place that can not go, go ask n n line, m m column how many moves

Obviously we should first horse based on the location of the grid will not be able to go do some mark

So, I will write down the code:

void work(long long x,long long y){
    ma[x][y]=1;
    ma[x-1][y-2]=1;
    ma[x-2][y-1]=1;
    ma[x-2][y+1]=1;
    ma[x-1][y+2]=1;
    ma[x+1][y-2]=1;
    ma[x+2][y-1]=1;
    ma[x+2][y+1]=1;
    ma[x+1][y+2]=1;
}

Then it may be used in oersted simple and commonly used method - Standard Method Number

You can give an example :

From the first row and first column of this table, the number of second line went down by the method of the second column is the number of the first row go to the second column of the program number of the first column of a second row + Scheme

I.e. go row x, column y = the number of program lines come to x-1, y column number of the program went + x rows, y-1 program number column (0 to press the sector count)

That is
f [ i ] [ j ] = f [ i 1 ] [ j ] + f [ i ] [ j 1 ] f[i][j]=f[i-1][j]+f[i][j-1]

Because come x x row Y Y of program it is clear from the left side and on top of it, because only these two cells before they can reach this step lattice.

So we can start recursion:

for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
      	if(i==1&&j==1)continue;
       	if(ma[i][j]==0)x[i][j]=x[i-1][j]+x[i][j-1];
    }
}

Here is my code AC

#include <bits/stdc++.h>
using namespace std;
long long a,b,n,m,x[23][23],ma[23][23];
void work(long long x,long long y){
    ma[x][y]=1;
    ma[x-1][y-2]=1;
    ma[x-2][y-1]=1;
    ma[x-2][y+1]=1;
    ma[x-1][y+2]=1;
    ma[x+1][y-2]=1;
    ma[x+2][y-1]=1;
    ma[x+2][y+1]=1;
    ma[x+1][y+2]=1;
}
int main(){
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    a++;
    b++;
    n++;
    m++;
    work(a,b);
    x[1][1]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
        	if(i==1&&j==1)continue;
        	if(ma[i][j]==0)x[i][j]=x[i-1][j]+x[i][j-1];
        }
    }
    printf("%lld",x[n][m]);
    return 0;
}

Postscript and supplement

Watch this picture, we can also find other things, we can find the following line is larger than the value on the line, it is the value of the left side of the grid, so we can optimize one dimension of this question, the code is very simple to achieve .

f [ i ] + = f [ i 1 ] f[i]+=f[i-1]

#include <bits/stdc++.h>
using namespace std;
long long a,b,n,m,f[23],ma[23][23];
void work(long long x,long long y){
    ma[x][y]=1;
    ma[x-1][y-2]=1;
    ma[x-2][y-1]=1;
    ma[x-2][y+1]=1;
    ma[x-1][y+2]=1;
    ma[x+1][y-2]=1;
    ma[x+2][y-1]=1;
    ma[x+2][y+1]=1;
    ma[x+1][y+2]=1;
}
int main(){
    scanf("%lld %lld %lld %lld",&n,&m,&a,&b);
    a++;
    b++;
    n++;
    m++;
    work(a,b);
    x[1][1]=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
			if(i==1&&j==1)continue;
        	if(ma[i][j]==0)f[j]+=f[j-1];
        }
    printf("%lld",f[m]);
    return 0;
}

If my article helpful please you a praise! ! !

Thank you.

Published 40 original articles · won praise 61 · views 616

Guess you like

Origin blog.csdn.net/qq_46230164/article/details/105319292