Giter Club home page Giter Club logo

find_way_in_the_maze's People

find_way_in_the_maze's Issues

find_way_in_the_maze

#include
#include
#include
#include <stdlib.h>
#include <windows.h>
#define stack_width 9
#define stack_length 9
using namespace std;

// 0 原点
// 1 道路
// 2 墙
// 3 终点
// 6 路径

const int Pixmap[stack_length][stack_width]={
{2,2,2,2,2,2,2,2,2},
{2,0,1,2,1,1,2,1,2},
{2,1,1,2,1,1,2,1,2},
{2,1,1,1,1,2,1,1,2},
{2,1,2,2,2,1,1,1,2},
{2,1,1,1,2,1,1,1,2},
{2,1,2,1,1,2,1,1,2},
{2,2,1,1,1,1,1,3,2},
{2,2,2,2,2,2,2,2,2}
};

int Tmpmap[stack_length][stack_width];

class pos{
private:
int x;
int y;
int direction;//上一步的移动方向
public:
pos(){x=0;y=0;direction=0;};
pos(int xpos, int ypos);
~pos(){};
bool compare(pos test_pos);
void copy_pos(pos& tmp_pos);
void setDir(int i);
void setX(int xpos);
void setY(int ypos);
int getDir();
int getX();
int getY();
void up();//移动
void down();
void left();
void right();
int movepos_up();//判断
int movepos_down();
int movepos_left();
int movepos_right();
};

void pos::up(){
x = x - 1;
y = y;
}

void pos::down(){
x = x + 1;
y = y;
}

void pos::left(){
x = x;
y = y - 1;
}

void pos::right(){
x = x;
y = y + 1;
}

int pos::movepos_up(){
int xtmp = x - 1;
int ytmp = y;
if(Tmpmap[xtmp][ytmp] == 2){
return 0;
}else if(Tmpmap[xtmp][ytmp] == 1){
return 1;
}
}

int pos::movepos_down(){
int xtmp = x + 1;
int ytmp = y;
if(Tmpmap[xtmp][ytmp] == 2){
return 0;
}else if(Tmpmap[xtmp][ytmp] == 1){
return 2;
}
}

int pos::movepos_left(){
int xtmp = x;
int ytmp = y - 1;
if(Tmpmap[xtmp][ytmp] == 2){
return 0;
}else if(Tmpmap[xtmp][ytmp] == 1){
return 3;
}
}

int pos::movepos_right(){
int xtmp = x;
int ytmp = y + 1;
if(Tmpmap[xtmp][ytmp] == 2){
return 0;
}else if(Tmpmap[xtmp][ytmp] == 1){
return 4;
}
}

void pos::setDir(int i){
direction = i;
}

void pos::setX(int xpos){
x = xpos;
}

void pos::setY(int ypos){
y = ypos;
}

int pos::getDir(){
return direction;
}

int pos::getX(){
return x;
}

int pos::getY(){
return y;
}

//这里是为了判断是否有cur_pos有和end_pos相等
bool pos::compare(pos test_pos){
if(x == test_pos.getX() && y == test_pos.getY()){
return false;
}else{
return true;
}
}

pos::pos(int xpos, int ypos){
x = xpos;
y = ypos;
}

void pos::copy_pos(pos& tmp_pos){
x = tmp_pos.getX();
y = tmp_pos.getY();
direction = tmp_pos.getDir();
}

void print_map(const int the_map[9][9]){
cout<<endl<<endl;
for(int i =0; i < 9; i++){
cout<<" ";
for(int j = 0; j < 9; j++){
switch(the_map[i][j]){
case 0:
cout<<"$";
break;
case 1:
cout<<" ";
break;
case 2:
cout<<"#";
break;
case 3:
cout<<"@";
break;
case 5:
cout<<"+";
break;
case 6:
cout<<"*";
break;
default:
break;
}
}
cout<<endl;
}
}

void move(const int Pixmap[][9]){
//遍历寻找起点终点
pos start_pos(0,0);
pos end_pos(0,0);
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(Pixmap[i][j]==0){
start_pos.setX(i);
start_pos.setY(j);
}else if(Pixmap[i][j]==3){
end_pos.setX(i);
end_pos.setY(j);
}
}
}

pos cur_pos(0,0);
cur_pos.copy_pos(start_pos);
stack<pos> pos_record;
for(int i = 0; i < 9; i++){
    for(int j = 0; j < 9; j++){
        Tmpmap[i][j] = Pixmap[i][j];
    }
}
print_map(Tmpmap);
while(cur_pos.compare(end_pos)){
    if(!cur_pos.compare(start_pos)){
        if(cur_pos.movepos_up()==0 && cur_pos.movepos_down()==0 && cur_pos.movepos_left()==0 && cur_pos.movepos_right()==0){
            cout<<"迷宫无解"<<endl;
            exit(0);
        }else if(cur_pos.movepos_up()){
            cur_pos.up();
            Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
            pos_record.push(cur_pos);
            cur_pos.setDir(1);//储存上一步移动方向
        }else if(cur_pos.movepos_down()){
            cur_pos.down();
            Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
            pos_record.push(cur_pos);
            cur_pos.setDir(2);
        }else if(cur_pos.movepos_left()){
            cur_pos.left();
            Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
            pos_record.push(cur_pos);
            cur_pos.setDir(3);
        }else if(cur_pos.movepos_right()){
            cur_pos.right();
            Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
            pos_record.push(cur_pos);
            cur_pos.setDir(4);
        }
    }else{
        if(cur_pos.getDir() == 1){
            if(cur_pos.movepos_up()==0 && cur_pos.movepos_left()==0 && cur_pos.movepos_right()==0){
                pos tmp = pos_record.top();
                for(int i = 0; i < 9; i++){
                    for(int j = 0; j < 9; j++){
                        if(Tmpmap[i][j] == 5){
                            Tmpmap[i][j] = 1;
                        }
                    }
                }
                Tmpmap[tmp.getX()][tmp.getY()] = 2;
                cur_pos.setX(start_pos.getX());
                cur_pos.setY(start_pos.getY());
                cur_pos.setDir(start_pos.getDir());
                while(!pos_record.empty()){
                    pos_record.pop();
                }
            }else if(cur_pos.movepos_left()){
                cur_pos.left();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(3);
            }else if(cur_pos.movepos_right()){
                cur_pos.right();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(4);
            }else if(cur_pos.movepos_up()){
                cur_pos.up();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(1);
            }
        }else if(cur_pos.getDir() == 2){
            if(cur_pos.movepos_down()==0 && cur_pos.movepos_left()==0 && cur_pos.movepos_right()==0){
                pos tmp = pos_record.top();
                for(int i = 0; i < 9; i++){
                    for(int j = 0; j < 9; j++){
                        if(Tmpmap[i][j] == 5){
                            Tmpmap[i][j] = 1;
                        }
                    }
                }
                Tmpmap[tmp.getX()][tmp.getY()] = 2;
                cur_pos.setX(start_pos.getX());
                cur_pos.setY(start_pos.getY());
                cur_pos.setDir(start_pos.getDir());
                while(!pos_record.empty()){
                    pos_record.pop();
                }
            }else if(cur_pos.movepos_down()){
                cur_pos.down();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(2);
            }else if(cur_pos.movepos_left()){
                cur_pos.left();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(3);
            }else if(cur_pos.movepos_right()){
                cur_pos.right();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(4);
            }
        }else if(cur_pos.getDir() == 3){
            if(cur_pos.movepos_up()==0 && cur_pos.movepos_down()==0 && cur_pos.movepos_left()==0){
                pos tmp = pos_record.top();
                for(int i = 0; i < 9; i++){
                    for(int j = 0; j < 9; j++){
                        if(Tmpmap[i][j] == 5){
                            Tmpmap[i][j] = 1;
                        }
                    }
                }
                Tmpmap[tmp.getX()][tmp.getY()] = 2;
                cur_pos.setX(start_pos.getX());
                cur_pos.setY(start_pos.getY());
                cur_pos.setDir(start_pos.getDir());
                while(!pos_record.empty()){
                    pos_record.pop();
                }
            }else if(cur_pos.movepos_down()){
                cur_pos.down();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(2);
            }else if(cur_pos.movepos_left()){
                cur_pos.left();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(3);
            }else if(cur_pos.movepos_up()){
                cur_pos.up();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(1);
            }
        }else if(cur_pos.getDir() == 4){
            if(cur_pos.movepos_up()==0 && cur_pos.movepos_down()==0 && cur_pos.movepos_right()==0){
                pos tmp = pos_record.top();
                for(int i = 0; i < 9; i++){
                    for(int j = 0; j < 9; j++){
                        if(Tmpmap[i][j] == 5){
                            Tmpmap[i][j] = 1;
                        }
                    }
                }
                Tmpmap[tmp.getX()][tmp.getY()] = 2;
                cur_pos.setX(start_pos.getX());
                cur_pos.setY(start_pos.getY());
                cur_pos.setDir(start_pos.getDir());
                while(!pos_record.empty()){
                    pos_record.pop();
                }
            }else if(cur_pos.movepos_down()){
                cur_pos.down();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(2);
            }else if(cur_pos.movepos_right()){
                cur_pos.right();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(4);
            }else if(cur_pos.movepos_up()){
                cur_pos.up();
                Tmpmap[cur_pos.getX()][cur_pos.getY()] = 5;
                pos_record.push(cur_pos);
                cur_pos.setDir(1);//储存上一步移动方向
            }
        }
    }
    system("cls");
    print_map(Tmpmap);
    Sleep(200);

    if(!cur_pos.compare(end_pos)){
        break;
    }
}
system("cls");
cout<<"现在开始倒推正确路线";
Sleep(1000);

while(!pos_record.empty()){
    pos tmp = pos_record.top();
    Tmpmap[tmp.getX()][tmp.getY()] = 6;

    system("cls");
    print_map(Tmpmap);

    pos_record.pop();
}

}

int main(){
move(Pixmap);
return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.