可以用递归的
f(i,j) = f(i+1,j)+f(i,j+1);
不过可能会超时
coding & enjoying
[此贴子已经被作者于2006-12-2 9:20:19编辑过]
/*SK-CHINA 特别为你翻译的,应该是最好的算法*/
int dx[9] = {0,-2, -1, 1, 2, 2, 1, -1, -2};
int dy[9] = {0,1, 2, 2, 1, -1, -2, -2, -1};
int n, m, x, y, i, j;
int g[21][21];
double f[21][21];
int main(void) {
scanf("%d%d%d%d",&n,&m,&x,&y);
memset(g,0,sizeof(g));
g[x][y]=1;
for(i=1;i<=8;i++)
if((x + dx[i] >= 0)&&(x + dx[i] <= n)&&(y + dy[i] >= 0)&&(y + dy[i] <= m)) g[x + dx[i]][ y + dy[i]] = 1;
f[0][0] = 1;
for(i=1;i<=n;i++)
if(g[i][0] == 0) f[i][0] = f[i - 1][0];
for(i=1;i<=m;i++)
if(g[0][i] == 0) f[0][i] = f[0][i - 1];
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(g[i][j] == 0) f[i][j] = f[i - 1][ j] + f[i][j - 1];
printf("%.0lf",f[n][m]);
getch();
return 0;
}