• 49. 跳马问题


    49. 跳马问题

    ★   输入文件:horse.in   输出文件:horse.out   简单对比
    时间限制:1 s   内存限制:128 MB

    【问题描述】

        有一只中国象棋中的 “ 马 ” ,在半张棋盘的左上角出发,向右下角跳去。规定只许向右跳(可上,可下, 但不允许向左跳)。请编程求从起点 A(1,1) 到终点 B(m,n) 共有多少种不同跳法。

     
     【输入格式】
     
        输入文件只有一行,两个整数m和n(1≤m,n≤20),两个数之间有一个空格。
     【输出格式】
     
        输出文件只有一个整数,即从 A 到 B 全部的走法。
     
     【输入输出样例】
     
       输入文件(horse.in)
       5 9
     
      输出文件(horse.out)
       37
    思路:宽搜
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n,m,ans;
    int vis[110][110];
    struct nond{
        int x,y;
    };
    queue<nond>que;
    int dx[5]={2,-2,1,-1};
    int dy[5]={1,1,2,2};
    int main(){
        freopen("horse.in","r",stdin);
        freopen("horse.out","w",stdout);
        scanf("%d%d",&n,&m);
        nond be;
        be.x=1;be.y=1;
        que.push(be);
        while(!que.empty()){
            nond now=que.front();
            que.pop();
            for(int i=0;i<4;i++){
                int cx=now.x+dx[i];
                int cy=now.y+dy[i];
                if(cx==n&&cy==m)    ans++;
                else if(cx>=1&&cx<=n&&cy>=1&&cy<=m){
                    nond cc;
                    cc.x=cx;
                    cc.y=cy;
                    que.push(cc);
                }
            }
        }
        printf("%d",ans);
    }
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int f[21][21];
    int n,m,ans;
    int dfs(int x,int y){
        if(x<1||x>n||y<1||y>m)    return 0;
        if(f[x][y])    return f[x][y];
        return f[x][y]+=(dfs(x-1,y-2)+dfs(x-2,y-1)+dfs(x+1,y-2)+dfs(x+2,y-1));
    }
    int main(){
        freopen("horse.in","r",stdin);
        freopen("horse.out","w",stdout);
        scanf("%d%d",&n,&m);
        f[1][1]=1;
        dfs(n,m);
        cout<<f[n][m];
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    什么是MSI文件?
    学习window系统下的注册表
    AngularJS学习手册
    学习ajax 总结
    jquery基础教程读书总结
    overflow:hidden清除浮动原理解析及清除浮动常用方法总结
    javascript进阶-原型prototype
    javascript-函数进阶
    小技巧之a标签自动解析URL
    Myeclipse出现 java文件中文乱码问题
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/9609750.html
Copyright © 2020-2023  润新知