免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1548 | 回复: 4
打印 上一主题 下一主题

[C++] 急 求救 c++ 马踏棋盘 运行出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-10 14:06 |只看该作者 |倒序浏览
#include<iostream>
#include<assert.h>
using namespace std;
const int maxSize = 64;

struct Pos{        //位置
        int x , y ;
};



struct Horse{
        int ord;
        Pos seat;
        int di;
        
};


int Bord[8][8];    //棋盘  0为未走过 1为走过


int movex[8]={-2,-1,1,2,2,1,-1,-2};   //x轴的移动数组
int movey[8]={1,2,2,1,-1,-2,-2,-1};  //y轴的移动数组
   



class Stack
{
public:
        Stack(int sz = 64);
        ~Stack(){}
        void Push ( const Horse  &x);//入栈
        bool Pop  ( Horse & x);//退栈
        bool getTop ( Horse & x);//取栈顶元素,不删除
        bool IsEmpty()const{return(top-elements>=maxSize)?true:false;}
        bool IsFull()const{return(top==elements)?true:false;}
private:
        Horse *elements;  //存放栈中元素的栈数组
        Horse *top;      //栈顶指针
        int maxSize;  //栈最大可容纳元素个数

};



Stack::Stack(int sz ){
        elements = new Horse[maxSize];
        assert(elements != NULL);
        top=elements;
        maxSize = sz;
};



void Stack::  Push  (   const Horse & x){
        if(IsFull()!=true)
        {*top = x;
        top++;}
        else
                std::cout<<"栈已满"<<std::endl;
};




bool Stack::  Pop (  Horse &x){
        if(IsEmpty()==true) return false;
        x= *top;
        top--;
        return true;
};




bool Stack::getTop(Horse &x){
        if(IsEmpty()==true) return false;
        x = *top;
        return true;
};



void FootPrint(Pos curpos)      //走过的标记
{
        Bord[curpos.x][curpos.y]=1;
}



bool Pass(Pos curpos)           //判断改位置是否以走过
{
        if((Bord[curpos.x][curpos.y]==0)
                &(curpos.x>=0)&(curpos.y>=0)
                &(curpos.x<=7)&(curpos.y<=7))
                return true ;
        else return false ;
}



int ways_out(int x, int y)      //计算改点的出口
{
        int i , count = 0 , tx ,ty ;
        if(x<0||y<0||x>=8||y>=8||Bord[x][y]>0)
                return -1;    //-1表示该节点非法或是已经走过
        for(i=0;i<8;++i)
        {
                tx = x + movex;
                ty = y + movey;
                if(tx<0||ty<0||tx>=8||ty>=8 )   //该节点非法或是已经走过
                        continue ;                 //直接进入下个循环
                if(Bord[tx][ty]==0)
                        ++count;

        }
        return count;
}



void ways_out_sort(int x ,int y)    //按孙节点从少到多排序
{
        int temp ;
        int i , j ;
        for(i=0;i<8;++i)
        {
                for(j=i;j<8;++j)
                        if(ways_out(x + movex,y + movey)>ways_out(x + movex[j],y + movey[j]))
                        {
                                temp = movex;
                                movex = movex[j];
                                movex[j] = temp ;
                                temp = movey;
                                movey = movey[j];
                                movey[j] = temp;

                        }
        }
}



Pos NextPos(Pos now , int c)     //当前位置的下个出口是ways_out最少的那个
{
        Pos next ;
        ways_out_sort(now.x,now.y);
        next.x = now.x + movex[c-1];
        next.y = now.y + movey[c-1];
        return next;
}



bool Horse_Move(Pos star , Stack & s)    //马遍历棋盘的函数
{
        Horse a_horse ; Pos curpos  ; int  curstep ;
        curpos = star ;
        curstep = 1 ;
        do{
                if(Pass(curpos)){               //当前位置可以通过
                        FootPrint(curpos);         //已走标志
                        a_horse.ord = curstep ;
                        a_horse.seat = curpos ;
                        a_horse.di = 1;
                        s.Push(a_horse);
                        if(s.IsFull()) return true ;
                        curpos = NextPos(curpos , 1);
                        curstep++ ;

                }
                else {
                        if(s.IsEmpty()==false){
                                s.Pop(a_horse);
                                while((a_horse.di==8 )&( s.IsEmpty()==false)){
                                        Bord[a_horse.seat.x][a_horse.seat.y]=0;
                                        s.Pop(a_horse);                           //留下不能通过的标记 ,退回一步
                                }
                                if(a_horse.di< 8 )  {
                                        a_horse.di++ ;s.Push(a_horse);
                                        curpos = NextPos(a_horse.seat,a_horse.di);
                                }
                        }
                }
        }
        while(s.IsEmpty()==false);
        
                return false ;
        
}




void Print(Stack s)        //输出函数
{
        int i ,j ;int a[8][8]={0};
        int m = 0 ;
        Horse a_horse;
        do{
                s.Pop(a_horse);
                a[a_horse.seat.x][a_horse.seat.y]=64-m;
                ++m;
        }while(s.IsEmpty()==false);
        for(i=0;i<8;++i){
                for(j=0;j<8;++j)
                        cout<<a[j]<<" ";
                cout<<endl;

        }
}

int main()
{
        Stack s ;
        int i , j ;
        Pos start ;
        start.x = 4 ;
        start.y = 4 ;
        for(i=0;i<8;++i)
                for(j=0;j<8;++j)
                        Bord[j] = 0;
        Horse_Move(start , s );
        Print(s);
        return 0;
        system("pause");

}


[ 本帖最后由 h2ojie 于 2008-7-10 14:11 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-07-10 15:35 |只看该作者
自己解决了

#include<iostream>
using namespace std;


const int maxSize = 64;


struct Pos{        //位置
        int x , y ;
};



struct Horse{
        int ord;
        Pos seat;
        int di;
       
};


int Bord[8][8];    //棋盘  0为未走过 1为走过


int movex[8]={-2,-1,1,2,2,1,-1,-2};   //x轴的移动数组
int movey[8]={1,2,2,1,-1,-2,-2,-1};  //y轴的移动数组
   



class Stack
{
public:
        Stack();
        ~Stack(){delete[]elements;}
        void Push(const Horse  &x);//入栈
        bool Pop( Horse & x);//退栈
        bool getTop(Horse & x);//取栈顶元素,不删除
        bool IsEmpty()const{return(top==-1)?true:false;}
        bool IsFull()const{return(top==maxSize-1)?true:false;}
private:
        Horse *elements;  //存放栈中元素的栈数组
        int top;      //栈顶指针
        int maxSize;  //栈最大可容纳元素个数

};



Stack::Stack(){
        top=-1;

        maxSize=64;
        elements = new Horse[maxSize];


};



void Stack::Push(const Horse & x){
        if(IsFull()!=true)
                elements[++top] = x;
       
        else
                cout<<"栈已满"<<endl;
};




bool Stack::Pop( Horse &x){
        if(IsEmpty()==true) return false;
        x = elements[top--];
        return true;
};




bool Stack::getTop(Horse &x){
        if(IsEmpty()==true) return false;
        x=elements[top];
        return true;
};



void FootPrint(Pos curpos)      //走过的标记
{
        Bord[curpos.x][curpos.y]=1;
}



bool Pass(Pos curpos)           //判断改位置是否以走过
{
        if((Bord[curpos.x][curpos.y]==0)
                &(curpos.x>=0)&(curpos.y>=0)
                &(curpos.x<=7)&(curpos.y<=7))
                return true ;
        else return false ;
}



int ways_out(int x, int y)      //计算改点的出口
{
        int i , count = 0 , tx ,ty ;
        if(x<0||y<0||x>=8||y>=8||Bord[x][y]>0)
                return -1;    //-1表示该节点非法或是已经走过
        for(i=0;i<8;++i)
        {
                tx = x + movex;
                ty = y + movey;
                if(tx<0||ty<0||tx>=8||ty>=8)   //该节点非法或是已经走过
                        continue ;                 //直接进入下个循环
                if(Bord[tx][ty]==0)
                        ++count;

        }
        return count;
}



void ways_out_sort(int x ,int y)    //按孙节点从少到多排序
{
        int temp ;
        int i , j ;
        for(i=0;i<8;++i)
        {
                for(j=i;j<8;++j)
                        if(ways_out(x + movex,y + movey)>ways_out(x + movex[j],y + movey[j]))
                        {
                                temp = movex;
                                movex = movex[j];
                                movex[j] = temp ;
                                temp = movey;
                                movey = movey[j];
                                movey[j] = temp;

                        }
        }
}



Pos NextPos(Pos now , int c)     //当前位置的下个出口是ways_out最少的那个
{
        Pos next ;
        ways_out_sort(now.x,now.y);
        next.x = now.x + movex[c-1];
        next.y = now.y + movey[c-1];
        return next;
}



bool Horse_Move(Pos star , Stack & s)    //马遍历棋盘的函数
{
        Horse a_horse ; Pos curpos  ; int  curstep ;
        curpos = star ;
        curstep = 1 ;
        do{
                if(Pass(curpos)){               //当前位置可以通过
                        FootPrint(curpos);         //已走标志
                        a_horse.ord = curstep ;
                        a_horse.seat = curpos ;
                        a_horse.di = 1;
                        s.Push(a_horse);
                        if(s.IsFull()) return true ;
                        curpos = NextPos(curpos , 1);
                        curstep++ ;

                }
                else {
                        if(s.IsEmpty()==false){
                                s.Pop(a_horse);
                                while((a_horse.di==8 )&( s.IsEmpty()==false)){
                                        Bord[a_horse.seat.x][a_horse.seat.y]=0;
                                        s.Pop(a_horse);                           //留下不能通过的标记 ,退回一步
                                }
                                if(a_horse.di<8){
                                        a_horse.di++ ;s.Push(a_horse);
                                        curpos = NextPos(a_horse.seat,a_horse.di);
                                }
                        }
                }
        }
        while(s.IsEmpty()==false);
       
                return false ;
       
}




void Print(Stack &s)        //输出函数
{
        int i ,j ;int a[8][8]={0};
        int m = 0 ;
        Horse a_horse;
        do{
                s.Pop(a_horse);
                a[a_horse.seat.x][a_horse.seat.y]=64-m;
                ++m;
        }while(s.IsEmpty()==false);
        for(i=0;i<8;++i){
                for(j=0;j<8;++j)
                        cout<<a[j]<<" ";
                cout<<endl;

        }
}

int main()
{
        Stack s ;
        int i , j ;
        int a , b ;
        cout<<"请输入初始位置:\n"<<"(x=0~7,y=0~7)"<<endl;
        cout<<"x = ";
        cin>>a;
        cout<<"y = ";
        cin>>b;
        Pos start ;
        start.x = a ;
        start.y = b ;
        for(i=0;i<8;++i)
                for(j=0;j<8;++j)
                        Bord[j] = 0;
        Horse_Move(start , s );
        Print(s);
        return 0;
        system("pause");

}

[ 本帖最后由 h2ojie 于 2008-7-11 17:01 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-07-10 15:55 |只看该作者
代码贴出来好多笑脸

论坛徽章:
0
4 [报告]
发表于 2008-07-10 15:59 |只看该作者
:)

论坛徽章:
0
5 [报告]
发表于 2008-07-10 16:00 |只看该作者
左边有一个禁用smiles的选项
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP