棋盘村 | ||||||
| ||||||
Description | ||||||
一名骑着马的强盗闯进了原本平静祥和的棋盘村,为了通知村里的士兵来打败强盗,你必须要通知位于棋盘村最下方的兵营。棋盘村的地形就像是一张棋盘,你所在的位置为A点(0,0),兵营位于棋盘村的右下角B点(n,m)。你每次只能走一步,可以选择向下走,也可以选择向右走。但是强盗所在的位置和强盗的马一次所能跳到的位置是不可以走过去的(强盗的马的移动方法与象棋中的马相同)。请计算出从A点能够走到B点的所有路径条数。
| ||||||
Input | ||||||
首先输入一个整数t,代表有t组测试数据。 每组测试数据为四个整数,即B点的坐标(n,m)和强盗的坐标(x,y)。 1 < n,m < 20. | ||||||
Output | ||||||
输出一个整数即路径的条数。 | ||||||
Sample Input | ||||||
2 6 6 3 2 8 8 3 5 | ||||||
Sample Output | ||||||
17 1393 |
这个题用普通搜索基本过不了,用dp就快多了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
long long dp[100][100];
bool Map[100][100];
int main(){
int kase;
scanf("%d",&kase);
int m,n;
int x,y;
int i,j,k;
while(kase--){
memset(Map,false,sizeof(Map));
memset(dp,0,sizeof(dp));
scanf("%d %d %d %d",&n,&m,&x,&y);
Map[x][y]=1;Map[x-2][y-1]=Map[x-2][y+1]=1;
Map[x+2][y-1]=Map[x+2][y+1]=1;
Map[x-1][y+2]=Map[x-1][y-2]=1;
Map[x+1][y+2]=Map[x+1][y-2]=1;
for(i=0;i<=n;i++)
if(Map[i][0]!=1)
dp[i][0]=1;
else
break;
for(i=0;i<=m;i++)
if(Map[0][i]!=1)
dp[0][i]=1;
else
break;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(Map[i][j])
dp[i][j]=0;
else
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
printf("%lld\n",dp[n][m]);
}
//哈哈哈。。。
return 0;
}