DFS练习-codevs-1010过河卒

1010 过河卒

2002年NOIP全国联赛普及组
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold

题目描述 Description

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

这里写图片描述
  棋盘用坐标表示,A 点(0,0)、B 点(n,m)(n,m 为不超过 20 的整数,并由键盘输入),同样马的位置坐标是需要给出的(约定: C不等于A,同时C不等于B)。现在要求你计算出卒从 A 点能够到达 B 点的路径的条数。

1<=n,m<=15
输入描述 Input Description

 键盘输入
   B点的坐标(n,m)以及对方马的坐标(X,Y){不用判错}
输出描述 Output Description

  屏幕输出
    一个整数(路径的条数)。
样例输入 Sample Input

 6 6 3 2
样例输出 Sample Output

17
数据范围及提示 Data Size & Hint

如描述

#include <stdio.h>
#include <string.h>

int sum;
bool book[20][20];

struct coordinate
{
    int x;
    int y;
}final,hourse,origin;

void sign()
{
    book[hourse.x][hourse.y]=1;
    if (hourse.x>=2) {
        book[hourse.x-2][hourse.y+1]=1;
        if (hourse.y>=1) {
            book[hourse.x-2][hourse.y-1]=1;
        }
    }
    if (hourse.x>=1) {
        if (hourse.y>=2) {
            book[hourse.x-1][hourse.y-2]=1;
        }
        book[hourse.x-1][hourse.y+2]=1;
    }
    if (hourse.y>=2) {
        book[hourse.x+1][hourse.y-2]=1;
    }
    if (hourse.y>=1) {
        book[hourse.x+2][hourse.y-1]=1;
    }
    book[hourse.x+1][hourse.y+2]=1;
    book[hourse.x+2][hourse.y+1]=1;

}

void dfs(struct coordinate now)//先下后右
{
    if (now.x==final.x && now.y==final.y) {
        sum++;
        return;
    }
    if (now.x<final.x && !book[now.x+1][now.y]) {
        now.x++;
        dfs(now);
        now.x--;
    }
    if (now.y<final.y && !book[now.x][now.y+1]) {
        now.y++;
        dfs(now);
        now.y--;
    }
    return;

}

int main(int argc, const char * argv[]) {
    scanf("%d%d%d%d",&final.x,&final.y,&hourse.x,&hourse.y);
    memset(book,0,sizeof(book));
    origin.x=0;
    origin.y=0;
    sign();
    dfs(origin);
    printf("%d",sum);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值