洛谷1002
-
搜索树 超时
-
dp问题 解题:马所在的位置和马能走的位置,路径条数为0 其他点的路径条数等于当前点的左边点路径条数与上面点路径条数之和
-
出现问题:测例3,4 一直WA 解决:sum数组为long long int 型
-
代码:
/*
棋盘上 AA 点有一个过河卒,需要走到目标 BB 点。
卒行走的规则:可以向下、或者向右。同时在棋盘上 CC 点有一个对方的马
该马所在的点和所有跳跃一步可达的点称为对方马的控制点。因此称之为“马拦过河卒”。
*/
/*
* 解题:dp类问题
* 马所在的位置和马能走的位置,路径条数为0
* 其他点的路径条数等于当前点的左边点路径条数与上面点路径条数之和
*/
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#define max 100
using namespace std;
int n, m, x, y;
bool b[max][max]; //用来判断当前点是否可以走
long long int sum[max][max];
//将不能走的点对应的位置的b数组置1
bool change(int m,int n,int x,int y)
{
b[x][y] = 1;
int i, j;
i = x - 2;
j = y - 1;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n)) //1
b[i][j] = 1;
i = x - 1;
j = y - 2;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))//2
b[i][j] = 1;
i = x - 2;
j = y + 1;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))//3
b[i][j] = 1;
i = x - 1;
j = y +2;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))//4
b[i][j] = 1;
i = x + 1;
j = y - 2;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))//5
b[i][j] = 1;
i = x + 2;
j = y - 1;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))//6
b[i][j] = 1;
i = x + 1;
j = y + 2;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))
b[i][j] = 1;
i = x + 2;
j = y + 1;
if ((i >= 0) && (i <= m) && (j >= 0) && (j <= n))
b[i][j] = 1;
return b;
}
int main()
{
cin >> m >> n >> x >> y;
memset(b, 0, sizeof(b));
change(m,n,x, y);
//初始化数组
int i;
int j;
for (i = 0; ((i <= m) && (!b[i][0])); i++)
sum[i][0] = 1;
for (j = 0; ((j <= n) && (!b[0][j])); j++)
sum[0][j] = 1;
//开始计算
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
if (b[i][j])
sum[i][j] = 0;
else
sum[i][j] = sum[i - 1][j] + sum[i][j - 1];
}
}
cout << sum[m][n];
return 0;
}

浙公网安备 33010602011771号