Total time limit: 1000ms memory limit: 128000kB
describe
A pawn crosses the river at point A on the chessboard and needs to reach point B. Rules for pawns to walk: they can go down or right. At the same time, at a certain point on the chessboard, there is an opposing horse (such as point C). The point where the horse is located and all the points that can be reached in one step are called the control points of the opposing horse, as shown in points C and P1 in Figure 3-1,... P8, the pawn cannot pass through the control point of the square horse. The chessboard is represented by coordinates, point A (0,0), point B (n, m) (n,m is not more than 20 integers), the same horse's position coordinates need to be given, C_A and C_B. Now you are asked to calculate the number of paths from point A to point B.
input
The coordinates of point B (n,m) and the coordinates of the opposite horse (X,Y)
output
The number of paths from point A to point B.
sample input
6 6 3 2
sample output
17
source
noip Popularization Group 2002
#include<iostream> using namespace std; long long fun(long long n,long long m, long long x, long long y){ long long a[21][21]; int dx[9] = {0, 2, 2, 1, 1, -2, -2, -1, -1}; int dy[9] = {0, 1, -1, 2, -2, 1, -1, 2, -2}; bool g1[21][21]; fill(g1[0],g1[0]+21*21,true); for (int i=0; i<=8; i++){ int xx=x+dx[i],yy=y+dy[i]; if (xx>=0 && xx<=n && yy>=0 && yy<=m){ g1[xx][yy]=false; } } // for(int g=0; g<=n; g++) // { // for(int h=0; h<=m; h++) // { // cout<<g1[g][h]<<" "; // } // cout<<endl; // } fill(a[0],a[0]+21*21,0); a[0][0]=1LL; for(int g=0; g<=n; g++) { for(int h=0;h<=m;h++) { if(g1[g][h]){ if(g==0&&h>=1){ a[0][h]=a[g][h-1]; } if(h==0&&g>=1){ a[g][0]=a[g-1][h]; } if(g>=1&&h>=1){ a[g][h] = a[g-1][h]+a[g][h-1]; } } } } // cout<<endl; // for(int g=0; g<=n; g++) // { // for(int h=0;h<=m;h++) // { // cout<<"("<<g<<","<<h<<"):"<<a[g][h]<<" "; // } // cout<<endl; // } return a[n][m]; } int main(){ long long n,m,x,y; cin>>n>>m>>x>>y; cout<<fun(n,m,x,y)<<endl; return 0; }
Crater log
Data type needs long, initial value needs LL