您的位置:首页 > 其它

马拦过河卒(NOIP)

2015-07-05 11:20 246 查看
马拦过河卒(NOIP)
Time Limit:7000MS Memory Limit:65536K

Description
如图,A点有一个过河卒,需要走到目标 B点。卒行走规则:可以向下、或者向右。同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点。例如上图 C点上的马可以控制 9个点(图中的P1,P2 … P8和 C)。卒不能通过对方马的控制点。



棋盘用坐标表示,A点(0,0)、B点(n,m)(n,m为不超过 17的整数,并由键盘输入),同样马的位置坐标是需要给出的(约定: C<>A,同时C<>B)。现在要求你计算出卒从 A点能够到达 B点的路径的条数。
Input
输入B点的坐标(n,m)以及对方马的坐标(X,Y)
Output
屏幕输出一个整数(路径的条数)。
Sample Input

6 6 3 2


Sample Output

17


Source
NOIP2002 普及组

#include <stdio.h>
#define maxn 19
int n, m;
int g[maxn][maxn];
bool flag[maxn][maxn];
int tx[9]={0, -2, -2, -1, -1, 1, 1, 2, 2};
int ty[9]={0, -1, 1, -2, 2, -2, 2, -1, 1};
void _read(int& d)
{
char t=getchar();bool f=false;
while(t<'0'||t>'9') {if(t=='-') f=true; t=getchar();}
for(d=0;t<='9'&&t>='0';t=getchar()) d=d*10+t-'0';
if(f) d=-d;
}
void _out(int d)
{
int o[30],top=1;
if(d==0){putchar('0');return ;}
if(d<0) {putchar('-');d=-d;}
while(d)
{
o[top++]=d%10;
d/=10;
}
for(--top;top;--top) putchar('0'+o[top]);
}
void biaoji(int x, int y)
{
if(x>0&&x<=n&&y>0&&y<=m)	flag[x][y]=1;
}
void rec(int x, int y)
{
if(flag[x-1][y])	g[x][y]+=g[x-1][y];
else
{
rec(x-1, y);
g[x][y]+=g[x-1][y];
}
if(flag[x][y-1])	g[x][y]+=g[x][y-1];
else
{
rec(x, y-1);
g[x][y]+=g[x][y-1];
}
flag[x][y]=1;
}
int main()
{
int x, y, i;
_read(n), _read(m), _read(x), _read(y);
n++;
m++;
x++;
y++;
for(i=0; i<9; i++)	biaoji(x+tx[i], y+ty[i]);
for(i=0; i<=m+1; i++)	flag[0][i]=1;
for(i=0; i<=n+1; i++)	flag[i][0]=1;
g[1][1]=1;
flag[1][1]=1;
rec(n, m);
_out(g
[m]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: