维基 OJ 1010 过河卒

http://wikioi.com/problem/1010/

简单的DP ,不过要注意边界的处理。

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
//#define max(a,b) a>b?a:b
#define inf 0x7fffffff;
int a[7],dp[20][20],sum;
struct A
{
    int x,y;
}horse[10];
int n,m,flag;
bool check(int a,int b)
{
    flag = true;
    for(int i = 1; i <= 9; i++)
    if(a == horse[i].x && b == horse[i].y) flag = false;
    return flag;
}
int main()
{
    int x,y,i,j;
    while(cin>>n>>m>>x>>y)
    {
        memset(dp,0,sizeof(dp));
        n += 1; m+=1; x += 1; y += 1;
        horse[1].x = x + 2, horse[1].y = y + 1;
        horse[2].x = x + 1, horse[2].y = y + 2;
        horse[3].x = x - 1, horse[3].y = y + 2;
        horse[4].x = x - 2, horse[4].y = y + 1;
        horse[5].x = x - 2, horse[5].y = y - 1;
        horse[6].x = x - 1, horse[6].y = y - 2;
        horse[7].x = x + 1, horse[7].y = y - 2;
        horse[8].x = x + 2, horse[8].y = y - 1;
        horse[9].x = x, horse[9].y = y;
        //cout<<dp[0][1]<<"**"<<endl;
        dp[1][1] = 1;
        for(i = 2; i <= m; i++)
        {
            if(check(1,i) && check(1,i-1)) dp[1][i] = dp[1][i-1];
            else
            dp[1][i] = 0;
        }
        for(i = 2; i <= n; i++)
        {
            if(check(i,1) && check(i-1,1))dp[i][1] = dp[i-1][1];
            else
            dp[i][1] = 0;
        }
        dp[1][1] = 0;
        //cout<<endl;
        for(i = 2; i <= n; i++)
        for(j = 2; j <= m; j++)
        {
            if(!check(i,j)) dp[i][j] = 0;
            else
            dp[i][j]  = dp[i - 1][j] + dp[i][j-1];  
        }
        cout<<dp[n][m]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值