迷宫

来源:互联网 发布:java怎么调用数组 编辑:程序博客网 时间:2024/04/28 07:42

.h

#ifndef _STACK_H_#define _STACK_H_const int STACK_SIZE=8;typedef struct point{   int m_x;   int m_y;   bool operator!=(const struct point &diot);   bool operator==(const struct point &diot);}POINT;typedef struct stack{POINT *p_base;POINT *p_top;int   m_stack_size;}STACK;class Tower{public:Tower();~Tower();void init();void process_tower();private:bool isempty();POINT top();void push(POINT point);void pop();POINT m_point;STACK m_stack;int   m_Locus[STACK_SIZE][STACK_SIZE]; };#endif


.cpp

#include <iostream>#include "stack.h"using namespace std;bool point::operator !=(const struct point &diot){if (diot.m_x!=this->m_x && diot.m_y!=this->m_y){return true;}return false;}bool point::operator ==(const struct point &diot){if (diot.m_x==this->m_x && diot.m_y==this->m_y){return true;}return false;}Tower::Tower(){for (int k=0; k<STACK_SIZE; ++k){for (int j=0; j<STACK_SIZE; ++j){m_Locus[k][j]=0;}}/*init border */for (int i=0; i<STACK_SIZE; ++i){m_Locus[0][i]=1;m_Locus[i][0]=1;m_Locus[STACK_SIZE-1][i]=1;m_Locus[i][STACK_SIZE-1]=1;}m_Locus[2][6]=1;m_Locus[3][6]=1;m_Locus[4][6]=1;m_Locus[4][5]=1;}Tower::~Tower(){delete[] m_stack.p_base;}void Tower::init(){m_stack.p_base=new POINT[STACK_SIZE*STACK_SIZE];m_stack.p_top=m_stack.p_base;m_stack.m_stack_size=STACK_SIZE*STACK_SIZE;}bool Tower::isempty(){if (m_stack.p_base==m_stack.p_top){return true;}return false;}void Tower::push(POINT point){m_stack.p_top->m_x=point.m_x;m_stack.p_top->m_y=point.m_y;m_stack.p_top++;m_Locus[point.m_x][point.m_y]=2;}POINT Tower::top(){POINT diot;if (m_stack.p_base==m_stack.p_top){diot.m_x=0;diot.m_y=0;}diot.m_x=(m_stack.p_top-1)->m_x;diot.m_y=(m_stack.p_top-1)->m_y;return diot;}void Tower::pop(){if(!isempty())  m_stack.p_top--;}void Tower::process_tower(){POINT doit={STACK_SIZE-2,STACK_SIZE-2};POINT poi={1,1};push(poi);POINT pop_point=top();while (!isempty() && !(doit==pop_point)){if (m_Locus[pop_point.m_x-1][pop_point.m_y]==0){POINT push_point={pop_point.m_x-1,pop_point.m_y};push(push_point);}else if (m_Locus[pop_point.m_x][pop_point.m_y+1]==0){POINT push_point={pop_point.m_x,pop_point.m_y+1};push(push_point);}else if (m_Locus[pop_point.m_x+1][pop_point.m_y]==0){POINT push_point={pop_point.m_x+1,pop_point.m_y};push(push_point);}else if (m_Locus[pop_point.m_x][pop_point.m_y-1]==0){POINT push_point={pop_point.m_x,pop_point.m_y-1};push(push_point);}else{pop();}cout<<"x:"<<pop_point.m_x<<"  Y:"<<pop_point.m_y<<endl;pop_point=top();}if(pop_point==doit){cout<<"x:"<<pop_point.m_x<<"  Y:"<<pop_point.m_y<<endl;cout<<"finish!"<<endl;}else{cout<<"fail"<<endl;}}int main(int argc,const char *argv[]){Tower tower;tower.init();tower.process_tower();return 0;}


	
				
		
原创粉丝点击