zufeoj_马的遍历

题目链接:http://acm.ocrosoft.com/problem.php?cid=1172&pid=50


题目描述

   中国象棋半张棋盘如图4(a)所示。马自左下角往右上角跳。今规定只许往右跳,不许往左跳。比如图4(a)中所示为一种跳行路线,并将所经路线打印出来。打印格式为:0,0->2,1->3,3->1,4->3,5->2,7->4,8…


输入

无输入

输出

输出上述棋盘的可以从(0,0)走到(4,8)点的方案数。


#include<bits/stdc++.h>
using namespace std;
int mp[5][9];
int sum=0;
struct HORSE 
{
    int x;
    int y;
} ss[111]; 
int dir[4][2]={{-2,1},{-1,2},{1,2},{2,1}};
void dfs(int x,int y,int ans){
    if(x==4&&y==8){
        sum++;
        return;
    }
    for(int i=0;i<=4;i++){
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
         if(xx>=0 && xx<5 && yy>=0 && yy<9 && mp[xx][yy]==0)
        {
            mp[xx][yy]=1;
            ss[ans].x=xx;
            ss[ans].y=yy;
            ans++;
            dfs(xx,yy,ans);
            mp[xx][yy]=0;
            ans--;
        }
    }
}
int main(){
    mp[0][0]=1;
    dfs(0,0,1);
    cout<<sum<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/80761633
今日推荐