| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 336 人关注过本帖
标题:高手进来看啊!马的遍历补充修改下程序
只看楼主 加入收藏
cjchnwq
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-6-1
收藏
 问题点数:0 回复次数:1 
高手进来看啊!马的遍历补充修改下程序
#include"stdlib.h"
#include"stdio.h"


struct Elem
{
    char c;
    int mark_direct[8];
};
Elem chessboard[13][14];
struct direct_increment
{
    int dx;
    int dy;
};
struct Postion
{
    int x;
    int y;
};
direct_increment direct_ay[8]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},
{-1,2}};

void init_chessboard(Elem chessboard[13][14])
{
    int i,j,k;
    for( i=0;i<13;i++)
        for( j=0;j<14;j++)
            (chessboard[i][j]).c='#';
    for( i=2;i<11;i++)
        for( j=2;j<12;j++)
        {
            (chessboard[i][j]).c='0';
            for( k=0;k<8;k++)
                ((chessboard[i][j]).mark_direct)[k]=0;
        }
}

bool check_postion(Postion pos)
{
    if(pos.x<2||pos.x>9||pos.y<2||pos.y>9)return false;
    else return true;
}

bool is_last_point(Elem chessboard[13][14],Postion pos)
{
    char cc=chessboard[pos.x][pos.y].c;
    if(cc=='1')return false;
    else
        for(int i=2;i<11;i++)
            for(int j=2;j<12;j++)
                if(chessboard[i][j].c=='0'&&!
(i==pos.x&&j==pos.y))return false;
    return true;
}


int count_way(Postion pos)
{
    int counter=0,i;
    for( i=0;i<8;i++)
    {
        Postion pos1;
        pos1.x=pos.x+direct_ay[i].dx;
        pos1.y=pos.y+direct_ay[i].dy;
        if((chessboard[pos.x][pos.y].mark_direct)[i]==0&&(chessboard
[pos1.x][pos1.y]).c=='0')
            ++counter;
    }
    return counter;
}


bool is_visited(Elem chessboard[13][14],Postion pos)
{
    ////
}


void mark_step(Elem chessboard[13][14],Postion pos)
{
    ////
}

bool Select_bestpost(Postion *ppos,Postion *bpos)
{
    int ct;int i;int direct,direct_ct=8;
    Postion pos1,pos_best;
    int flag=0;
    for( i=0;i<8;i++)
    {
        if(((chessboard[ppos->x][ppos->y]).mark_direct)[i]==0)
        {
            pos1.x=ppos->x+direct_ay[i].dx;
            pos1.y=ppos->y+direct_ay[i].dy;
            if(check_postion(pos1)&&!is_visited(chessboard,pos1))
                if(is_last_point(chessboard,pos1))ct=1;
                else
                    ct=count_way(pos1);
            else ct=-1;
            if(ct<=direct_ct&&ct>0)
            {
                pos_best.x=pos1.x;
                pos_best.y=pos1.y;
                flag=1;
                direct_ct=ct;
                direct=i;
            }
        }
        else continue;
    }
    if(flag)
    {
        bpos->x=pos_best.x;
        bpos->y=pos_best.y;
        int reverse_direct=(direct)>4?direct-4:direct+4;
        (chessboard[ppos->x][ppos->y]).mark_direct[direct]=1;
        (chessboard[bpos->x][bpos->y]).mark_direct[reverse_direct]=1;
        return true;
    }
    else
        return false;
}


void clear_pos(Postion p,Postion next_pos)
{
   
/////
}



visit_complete(chessboard)
{
/////
}

bool Visit_Recursion(Elem chessboard[13][14],Postion pos)
{
    Postion next_pos;
    static Postion pos_beg=pos;
    int Succ=0;static ct=0;
    int i;
    mark_step(chessboard,pos);
    if(visit_complete(chessboard))
    {
        Succ=1;
        ct=0;
        return true;
    }
    else
        for(i=0;!Succ&&i<8;i++)
        {
            if(!Select_bestpost(&pos,&next_pos))continue;
            if(!Visit_Recursion(chessboard,next_pos))
            {
                clear_pos(chessboard,next_pos);
               
                continue;
            }
            else Succ=1;
            ++ct;
            printf("<=%d:%d",next_pos.x,next_pos.y);
            if(ct%8==0)printf("\n");
        }
    if(!Succ)return false;
}


void main()
{
    Postion pos1;
    char ch;
    init_chessboard(chessboard);
    while(1)
    {
        printf("请输入马在棋盘中的起始位置,可以随便设(2<=x<=10,2<=y<=11
)\n");
        printf("输入的两个位置用空格分割,用ENTER键确认\n");
        scanf("%d%d",&pos1.x,&pos1.y);
        if(Visit_Recursion(chessboard,pos1))
        {
            printf("<=%d:%d\n",pos1.x,pos1.y);
            printf("逆向打印走过的路径\n");
            printf("遍历结束!\n");
        }
        else
            printf("没有满足条件的路径!\n");
        printf("还要试一下其它起始位置吗?输入Y继续,输入其它字符退出\n");
        scanf("%c",ch);
        init_chessboard(chessboard);
        if(ch=='y')continue;
        else break;
    }
}
搜索更多相关主题的帖子: include void 
2012-06-01 15:14
cjchnwq
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-6-1
收藏
得分:0 
求解答啊。。。
2012-06-01 16:22
快速回复:高手进来看啊!马的遍历补充修改下程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017137 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved