總時間限制: 1000ms 內存限制: 128000kB
描述
棋盤上A點有一個過河卒,需要走到目標B點。卒行走的規則:可以向下、或者向右。同時在棋盤上的某一點有一個對方的馬(如C點),該馬所在的點和所有跳躍一步可達的點稱爲對方馬的控制點,如圖3-1中的C點和P1,……,P8,卒不能通過對方馬的控制點。棋盤用座標表示,A點(0,0)、B點(n, m) (n,m爲不超過20的整數),同樣馬的位置座標是需要給出的,C≠A且C≠B。現在要求你計算出卒從A點能夠到達B點的路徑的條數。
輸入
B點的座標(n,m)以及對方馬的座標(X,Y)
輸出
從A點能夠到達B點的路徑的條數。
樣例輸入
6 6 3 2
樣例輸出
17
來源
noip普及組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;
}
踩坑日誌
數據類型需要long long, 賦初值 需要加LL