首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

马踏棋盘+贪心,结果没有效果。有空的帮忙看看算法有错没。该如何处理

2012-02-27 
马踏棋盘+贪心,结果没有效果。。有空的帮忙看看算法有错没。。C/C++ code#include iostream#include stack

马踏棋盘+贪心,结果没有效果。。有空的帮忙看看算法有错没。。

C/C++ code
  #include <iostream>#include <stack>#include <vector>#include <algorithm>using namespace std;typedef struct{    int x;    int y;}Direc;//横向x,纵向y,右正,下正Direc direc[8]={{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2},{1,-2},{2,1},{2,-1}};//贪心选择数组元素类class Greed_Select{    friend class Stack_Node;    friend class Horse;public:    bool operator < (Greed_Select &p)    {        return way_out<p.way_out;    }private:    int to_x,to_y;    int way_out;//如果此跳点没有出路};class Stack_Node//栈结点类{    friend class Horse;public:    Stack_Node(int x,int y):_x(x),_y(y),_cnt(0)    {    }private:    int _x,_y;    vector<Greed_Select> _to_go;//数组中元素为上边的那个类    int _cnt;//记录该走第几条贪心选择};class Horse{public:    Horse()    {        for(int i=0;i<=8;++i)            for(int j=0;j<=8;++j)                flag[i][j]=0;    }    void begin()    {        Stack_Node *start=new Stack_Node(1,1);//从左上角开始跳        _stack.push(start);         flag[1][1]=1;        _get_greed_select(start);        while(!_stack.empty())//用栈深搜+贪心策略        {            if(_stack.size()==64)//栈中有64个点,说明整个棋盘都跳过了,找到一种路径,打印,退出.            {                cout<<"找到一种路径"<<endl;                return;            }            Stack_Node *p=_stack.top();            if(p->_cnt==8||p->_to_go[p->_cnt].way_out==999)//所有邻接跳点都遍历过,退栈.(包括8方向全部遍历过以及从当前点无法到达剩余方向的情况)            {                flag[p->_x][p->_y]=0;                _stack.pop();                delete p;            }            else//按贪心策略访问还没访问过的邻接跳点            {                int tx=p->_to_go[p->_cnt].to_x;                int ty=p->_to_go[p->_cnt].to_y;                ++p->_cnt;                Stack_Node *q=new Stack_Node(tx,ty);                flag[tx][ty]=1;                _get_greed_select(q);                _stack.push(q);            }        }        if(_stack.empty())        {            cout<<"没有路径"<<endl;        }    }    void _get_greed_select(Stack_Node *p)    {        int tx,ty,count;        for(int i=0;i<=7;++i)        {            tx=p->_x+direc[i].x;            ty=p->_y+direc[i].y;            if(test_bound(tx,ty)&&flag[tx][ty]==0)//能跳过去的邻接跳点,计算出口数量            {                count=0;                for(int j=0;j<=7;++j)                {                    if(test_bound(tx+direc[j].x,ty+direc[j].y)&&flag[tx+direc[j].x][ty+direc[j].y]==0)                    {                        ++count;                    }                }                Greed_Select temp;                temp.to_x=tx;                temp.to_y=ty;                temp.way_out=count;                p->_to_go.push_back(temp);            }            else//跳不过去的邻接跳点            {                Greed_Select temp;                temp.to_x=-1;                temp.to_y=-1;                temp.way_out=999;                p->_to_go.push_back(temp);            }        }        sort(p->_to_go.begin(),p->_to_go.end());    }    bool test_bound(int x,int y)    {        return (x>0)&(x<9)&(y>0)&(y<9);    }private:    stack<Stack_Node*> _stack;    unsigned char flag[9][9];};int main(){    Horse test;    test.begin();}


[解决办法]
坐等高手
[解决办法]
你可以优先跳靠外面的点,速度可以快很多
[解决办法]
外面就是靠近边界的,里面就是靠近中心的
[解决办法]
xuexiyixia
------解决方案--------------------


C/C++ code
Direc direc[8]={{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2},{1,-2},{2,1},{2,-1}}; 

热点排行