亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

沒下源碼,自己琢磨出來了,有百度左右手算法,算法這東西真難XD...

#include <iostream>

#include <Windows.h>

#include <string>

#include <process.h>

#include <Windows.h>


using namespace std;


/* https://zhidao.baidu.com/question/1755742141667975828.html?

設定初始面向 East ( 如果是右手抹墻則是 West)


1如果當前格為第一排,則說明當前格為終點,結束。

2根據當前格的數據,找到下一步需要面向的方向, 方法是,如果當前方向上有墻,則順時針(右手抹墻則逆時針)轉身,重復這一步驟直到面向的方向上可以行走

3沿當前方向走一步

4逆時針(右手抹墻則順時針)轉身一次,使當前面對方向為第3步之后的左手(或右手)方向, 然后回到步驟1

*/


//墻體和路常量

#define WOLD ?"■"

#define ROLD ?" ?"


//迷宮大小常量

const int X = 8;

const int Y = 8;


//計步器

int count = 0;


//方向枚舉

enum direction{

N = 0,

S = 1,

W = 2,

E = 3

};


//地圖類

class MazeMap {

public:

MazeMap() {}

void coutMap(string map[X][Y]) { //構造地圖

for (int i = 0; i < X; i++) {

for (int j = 0; j < Y; j++) {

cout << map[i][j] ;

}

cout << endl;

}

}

};


//人類類

class Human {

public:

Human() :x(6), y(6), man("☆"), direction(W){} //默認人初始化為第一個位置

Human(string _man) :x(6), y(6), man(_man), direction(W){} //可以設置人的標識符

//橫縱坐標

int x;

int y;

//人物標識

string man;

//方向

int direction;

// 移動方法

void humanMove(string map[X][Y], Human *man) {

MazeMap *mazeMap = new MazeMap;

map[man->x][man->y] = man->man;

mazeMap->coutMap(map);

cout << "READY?(Q:y/n)" << endl;

char flag = 'n';

cin >> flag;

//單向尋路原則 ?右手抹墻,初始朝向為W

if (flag == 'y') {

do

{

turnBack(map, man);

move(map, man);

Sleep(1000);

system("cls");

mazeMap->coutMap(map);

} while (finsh(man));

cout << "YOU WIN!!!" << endl;

}

else {

cout << "YOU ARE A LOSE!!!" << endl;

}


delete mazeMap;

mazeMap = NULL;

}


//確定方向 如果方向上有墻就逆時針轉一下

void turnBack(string map[X][Y],Human *man) {

static int cache = 0;

if (man->direction == N) {

if (map[man->x - 1][man->y] == WOLD) {

man->direction = W;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (man->direction == S) {

if (map[man->x+1][man->y] == WOLD) {

man->direction = E;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (man->direction == W) {

if (map[man->x][man->y-1] == WOLD) {

man->direction = S;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (man->direction == E) {

if (map[man->x][man->y+1] == WOLD) {

man->direction = N;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (cache == 4) {

cache = 0;

return;

}

}

//移動一格 ?后順時針調轉方向

void move(string map[X][Y], Human *man) {

if (man->direction == N) {

man->direction = E;

map[man->x - 1][man->y] = man->man;

map[man->x][man->y] = ROLD;

man->x -= 1;

}else if (man->direction == S) {

man->direction = W;

map[man->x + 1][man->y] = man->man;

map[man->x][man->y] = ROLD;

man->x += 1;

}else if (man->direction == W) {

man->direction = N;

map[man->x][man->y - 1] = man->man;

map[man->x][man->y] = ROLD;

man->y -= 1;

}else if(man->direction == E) {

man->direction = S;

map[man->x][man->y + 1] = man->man;

map[man->x][man->y] = ROLD;

man->y += 1;

}

return;

}

//判斷是否完成

bool finsh(Human *man) {

if (man->x == 0)

return false;

return true;

}


};



int main(void) {

string map[X][Y] = { {WOLD,ROLD,WOLD,WOLD,WOLD,WOLD,WOLD,WOLD},

{WOLD,ROLD,ROLD,ROLD,ROLD,ROLD,WOLD,WOLD},

{WOLD,WOLD,WOLD,WOLD,WOLD,ROLD,ROLD,WOLD},

{WOLD,ROLD,ROLD,ROLD,ROLD,ROLD,WOLD,WOLD},

{WOLD,WOLD,ROLD,WOLD,ROLD,ROLD,WOLD,WOLD},

{WOLD,WOLD,ROLD,ROLD,ROLD,WOLD,WOLD,WOLD},

{WOLD,WOLD,WOLD,WOLD,ROLD,ROLD,ROLD,WOLD},

{WOLD,WOLD,WOLD,WOLD,WOLD,WOLD,WOLD,WOLD} };

Human *man = new Human("⊙");

man->humanMove(map,man);


delete man;

man = NULL;

return 0;

}



正在回答

4 回答

//確定方向 如果方向上有墻就逆時針轉一下

void turnBack(string map[X][Y], Human *man) {

static int cache = 0;

if (man->direction == N) {

if (map[man->x - 1][man->y] == WALL) {

man->direction = W;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (man->direction == S) {

if (map[man->x + 1][man->y] == WALL) {

man->direction = E;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (man->direction == W) {

if (map[man->x][man->y - 1] == WALL) {

man->direction = S;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (man->direction == E) {

if (map[man->x][man->y + 1] == WALL) {

man->direction = N;

cache++;

turnBack(map, man);

}

cache = 0;

return;

}

if (cache == 4) {

cache = 0;

return;

}

}

可以問一下,這個cache的作用是什么嗎?沒看懂???

1 回復 有任何疑惑可以回復我~
#1

puikiri 提問者

man轉向后的當前方向, //方向枚舉 enum direction{ N = 0, S = 1, W = 2, E = 3 };
2019-02-18 回復 有任何疑惑可以回復我~
#2

puikiri 提問者

覺得不對勁,回頭看了下好像沒什么用 - - ,我去掉了運行也沒錯 - -
2019-02-18 回復 有任何疑惑可以回復我~
#3

puikiri 提問者

想起來了,這是判斷,如果出生點四處都是墻則強制返回的 = =
2019-02-18 回復 有任何疑惑可以回復我~
#4

puikiri 提問者

應該把 if (cache == 4) { cache = 0; return; } 放在這句static int cache = 0;后面 = =,吐血,我的源碼改了上傳的沒改- -,難怪看半天總覺得不對- -
2019-02-18 回復 有任何疑惑可以回復我~
#5

puikiri 提問者 回復 puikiri 提問者

感謝提出寶貴意見,之前沒注意到,出生點四處是墻應該強制退出 = = ,
2019-02-18 回復 有任何疑惑可以回復我~
查看2條回復

process.h這個頭文件是干嘛的呀大佬?

0 回復 有任何疑惑可以回復我~

感謝大佬

0 回復 有任何疑惑可以回復我~

聽說算法和數據結構是很重要的XD...,歡迎大家指出不足XD...

0 回復 有任何疑惑可以回復我~
#1

qq_阿斯蒂芬_0

你這個代碼 給新手看最好 void humanMove(string map[X][Y], Human *man) *man改為 *pMan 還有 turnBake(map, pMan); turnBake 改為 turnLeft
2019-01-04 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
C++遠征之封裝篇(下)
  • 參與學習       70902    人
  • 解答問題       534    個

封裝--面向對象三大特征之一,通過案例讓C++所學知識融會貫通

進入課程

沒下源碼,自己琢磨出來了,有百度左右手算法,算法這東西真難XD...

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號