一些改变的工作代码...... [英] Some working code for a change...

查看:80
本文介绍了一些改变的工作代码......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些人可能还记得,在不久前,我开始对一个奇怪的3D迷宫进行一次或两次b / b
。我现在有一个工作代码,我想要分享b
,听取意见,建议等等,但首先让我解释一下它的全部内容。


整个迷宫是一个立方体,它包含x ^ 3立方

房间。

迷宫是无限的/有限的,它没有边框,但仍然有一个

大小。

fx。如果labytrint的大小是2 ^ 3并且你发现自己处于

坐标3,3,3你真的在坐标处找到你的自我

1, 1,1.

每个房间有6个墙/可能的门,但是,相邻的房间共用一个

墙,所以我们只需要跟踪每个房间的三面墙。

墙可以设置或未设置,如果未设置,当然现在有了你的

可以自由移动到下一个房间。

网格图案遵循一个简单的房间:你必须,在任何时候都能够

来找到从一个房间到另一个房间的路径。

这个棘手的问题解决了通过设置迷宫中的所有墙壁和

随机设置一个起始位置,然后随机选择一个方向。

如果那个方向的房间没有被访问过然后,踩下

墙并移动到那里重新开始,否则再试一次。如果你不能继续移动

,则回到最后一个位置并从那里开始。当你被困在起始位置时,

网格已准备就绪。


剩下的唯一问题应该是:使用它的原因是什么?

好​​吧,我可以想到很多选项,但是一开始就可以在一个labyrint中使用一个

鼠标;-)


最后我要感谢两个人:

" Kai-Uwe Bux"

帮助我了解数据结构。

有人我记不起名字了

为了提供网格生成的想法。


当然我要感谢其他人切割总计n00b

一些渣。 ;-)


现在代码:


@code start

#include< iostream>

#include< vector>

#include< bitset>

#include< ctime>

const unsigned short Size = 4; // 2 ^ N

const unsigned short Log2 = 2; // N,log2(大小)

std :: bitset<尺寸*尺寸*尺寸> YZ;

std :: bitset<尺寸*尺寸*尺寸> XZ;

std :: bitset<尺寸*尺寸*尺寸> XY;

std :: bitset<尺寸*尺寸*尺寸> V;

struct Stack {

unsigned short z:Log2;

unsigned short y:Log2;

unsigned短x:Log2;

bool dir [6];

};

struct {

union {

struct {

unsigned short z:Log2;

unsigned short y:Log2;

unsigned short x:Log2 ;

};

unsigned long long id:Log2 * 3;

};

} pos;

短rand_int(短i){

返回rand()%i;

}

void new_grid() {

std :: vector< Stack> stack(1);

bool loop = true;

YZ.set();

XZ.set();

XY.set();

V.set();

V.flip();

srand(time( 0));

pos.x = rand_int(6);

pos.y = rand_int(6);

pos.z = rand_int(6);

V [pos.id] = 1;

stack.back()。x = pos.x;

stack.back()。y = pos.y;

stack.back()。z = pos.z;

stack.resize(stack.size()+ 1);

while(stack.size()> 0){

for(int choice = rand_int(6),loop = true;

loop == true;

choice = rand_int(6)){

stack.back()。dir [choice] = 1;

if(choice == 0){

pos.x ++;

if(V [pos.id] == 1)pos.x - ;

else {

loop = false;

pos.x--;

YZ [pos.id] = 0;

pos.x ++;

}

}

else if(choice == 1){

pos.y ++;

if(V [pos.id] == 1)pos.y--;

else {

loop = false;
pos.y--;

XZ [pos.id] = 0;

pos.y ++;

}

}

else if(choice == 2){

pos.z ++;

if(V [pos.id] == 1)pos.z--;

else {

loop = false;

pos.z-- ;

XY [pos.id] = 0;

pos.z ++;

}

}

else if(choice == 3){

pos.x--;

if(V [pos.id] == 1)pos .x ++;

else {

loop = false;

YZ [pos.id] = 0;

}

}

else if(choice == 4){

pos.y--;

if (V [pos.id] == 1)pos.y ++;

else {

loop = false;

XZ [pos.id ] = 0;

}

}

else if(choice == 5){

pos.z - ;

if(V [pos.id] == 1)pos.z ++;

else {

loop = false;

XY [pos.id] = 0;

}

}

if(loop == true& &

stack.back()。dir [0] + stack.back()。dir [1] + stack.back()。d ir [2] +

stack.back()。dir [3] + stack.back()。dir [4] + stack.back()。dir [5]

== 6){

loop = false;

stack.pop_back();

pos.x = stack.back( ).x;

pos.y = stack.back()。y;

pos.z = stack.back()。z;

}

else if(loop == false){

V [pos.id] = 1;

stack.back() .x = pos.x;

stack.back()。y = pos.y;

stack.back()。z = pos.z;

stack.resize(stack.size()+ 1);

}

}

////临时进度概述///////////

/ ** / system(" cls"); / ** /

/ ** / std :: cout<< YZ <<的std :: ENDL; / ** /

/ ** / std :: cout<< XZ<<的std :: ENDL; / ** /

/ ** / std :: cout<< XY<<的std :: ENDL; / ** /

/ ** / std :: cout<< V; / ** /

////////////////////////////////////// /

}

}


int main(){

while(0 == 0 )

new_grid();

}

@code end


现在,这段代码是ofcouse远未完成,但这确实有效,

这是进步,应该庆祝。


现在最重要的事情是:

1)有些不合情理的联盟。

2)调整大小(size()+ 1)

3)Log2,大小

4)最后一个if循环巫婆看起来并不好;-)

5)网格生成代码确实需要一些优化。

6 )你应该感兴趣的任何其他问题。


NB。

这段代码确实需要优化和改变但是避免编程

不超过一个月,还有很多我不知道/理解,

所以请给我一些意见。


Best

Zacariaz

Some might remember that i, not so long ago, started a treath or two
about a weird 3d labyrinth. I now have a working code, that i want to
share, hear comments, advice, ect., but first let me explain what its
all about.

The whole labyrinth is a cubic in its self and it contains x^3 cubic
rooms.
The labyrinth is infinite/finite, it has no borders, but still have a
size.
fx. if the size of the labytrint is 2^3 and you find yourself at
coordinate 3,3,3 you''re really finding your self at the coordinate
1,1,1.
Each room has 6 wall/possible doors, however, adjacent rooms shares a
wall, so we only need to keep track of three wall per room.
A wall can be set or unset, if unset, ofcourse there is nowall and your
free to move to the next room.
The grid pattern follow one simple room: "you must, at all time be able
to find a path from one room to another."
This tricky problem is solved by setting all walls in the labyrinth and
setting a start position at random, then picking a direction at random.
If the room in that direction have''nt been visited yet, brake down the
wall and move there and start over, else try again. If you cant move
any further, pop back to last position and start over from there. The
grid is ready when you are stuck at the start position.

The only question left should be: "what to use it for?"
Well, i can think of many options, but for a start lets just this of a
mouse in a labyrint ;-)

Last i would like to thank two people:
"Kai-Uwe Bux"
For helping me alot with the datastructure.
"someone i cant remember the name of"
For suplying the idea for the grid generation.

And ofcourse i want to thank everyone else for cutting a total n00b
some slag. ;-)

Now for the code:

@code start
#include <iostream>
#include <vector>
#include <bitset>
#include <ctime>
const unsigned short Size = 4; // 2^N
const unsigned short Log2 = 2; // N, log2(Size)
std::bitset<Size*Size*Size> YZ;
std::bitset<Size*Size*Size> XZ;
std::bitset<Size*Size*Size> XY;
std::bitset<Size*Size*Size> V;
struct Stack {
unsigned short z:Log2;
unsigned short y:Log2;
unsigned short x:Log2;
bool dir[6];
};
struct {
union {
struct {
unsigned short z:Log2;
unsigned short y:Log2;
unsigned short x:Log2;
};
unsigned long long id:Log2*3;
};
} pos;
short rand_int(short i) {
return rand() % i;
}
void new_grid() {
std::vector<Stack> stack(1);
bool loop = true;
YZ.set();
XZ.set();
XY.set();
V.set();
V.flip();
srand(time(0));
pos.x = rand_int(6);
pos.y = rand_int(6);
pos.z = rand_int(6);
V[pos.id] = 1;
stack.back().x = pos.x;
stack.back().y = pos.y;
stack.back().z = pos.z;
stack.resize(stack.size()+1);
while (stack.size() > 0) {
for (int choice = rand_int(6), loop = true;
loop == true;
choice = rand_int(6)) {
stack.back().dir[choice] = 1;
if (choice == 0) {
pos.x++;
if (V[pos.id] == 1) pos.x--;
else {
loop = false;
pos.x--;
YZ[pos.id] = 0;
pos.x++;
}
}
else if (choice == 1) {
pos.y++;
if (V[pos.id] == 1) pos.y--;
else {
loop = false;
pos.y--;
XZ[pos.id] = 0;
pos.y++;
}
}
else if (choice == 2) {
pos.z++;
if (V[pos.id] == 1) pos.z--;
else {
loop = false;
pos.z--;
XY[pos.id] = 0;
pos.z++;
}
}
else if (choice == 3) {
pos.x--;
if (V[pos.id] == 1) pos.x++;
else {
loop = false;
YZ[pos.id] = 0;
}
}
else if (choice == 4) {
pos.y--;
if (V[pos.id] == 1) pos.y++;
else {
loop = false;
XZ[pos.id] = 0;
}
}
else if (choice == 5) {
pos.z--;
if (V[pos.id] == 1) pos.z++;
else {
loop = false;
XY[pos.id] = 0;
}
}
if (loop == true &&
stack.back().dir[0]+stack.back().dir[1]+stack.back().dir[2]+
stack.back().dir[3]+stack.back().dir[4]+stack.back().dir[5]
== 6) {
loop = false;
stack.pop_back();
pos.x = stack.back().x;
pos.y = stack.back().y;
pos.z = stack.back().z;
}
else if (loop == false) {
V[pos.id] = 1;
stack.back().x = pos.x;
stack.back().y = pos.y;
stack.back().z = pos.z;
stack.resize(stack.size()+1);
}
}
//// temp progress overview ///////////
/**/ system("cls"); /**/
/**/ std::cout << YZ << std::endl; /**/
/**/ std::cout << XZ << std::endl; /**/
/**/ std::cout << XY << std::endl; /**/
/**/ std::cout << V; /**/
///////////////////////////////////////
}
}

int main() {
while (0==0)
new_grid();
}
@code end

Now, this code is ofcouse far from finished, but this actually work,
that is progress and should be celebrated.

Now the most important things to discuss would be:
1) the somewhat inapropiate union.
2) the resize(size()+1)
3) the Log2, Size
4) the last if loop witch doesnt look good at all ;-)
5) the grid generation code that is does indeed need some optimizing.
6) any other issue that you should find interesting.

NB.
This code does indeed need optimizing and altering but haven programed
for no longer than a month, theres still alot i dont know/understand,
so please give me some input.

Best
Zacariaz

推荐答案



< fe ********** @ hotmail.com>在消息中写道

新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ...

:有些人可能还记得我,不久前,我开始了一两片生活:关于一个奇怪的3D迷宫。我现在有一个工作代码,我想要b $ b:分享,听取意见,建议等等,但首先让我解释一下它的所有内容。:所有关于。< br $> b $ b:

:整个迷宫是一个立方体,它包含x ^ 3立方

:房间。

:迷宫是无限的/有限的,它没有边框,但仍然有一个

:大小。

:fx。如果labytrint的大小是2 ^ 3你发现自己在
:坐标3,3,3你真的在坐标处找到自己

: 1,1,1。

:每个房间有6个墙/可能的门,但是,相邻的房间共用一个

:墙,所以我们只需跟踪三个每个房间的墙壁。

:墙壁可以设置或未设置,如果未设置,当然有现在和你的

:免费移动到下一个房间。

:网格模式遵循一个简单的房间:你必须在任何时候都能够

:找到从一个房间到另一个房间的路径。

:这个棘手的问题是通过设置迷宫中的所有墙来解决的,并且

:随机设置一个起始位置,然后随机选择一个方向。

:如果那个方向的房间还没有被访问过,踩下

:墙并移动到那里重新开始,否则再试一次。如果你不能移动

:再进一步,回到最后一个位置并从那里重新开始。当你被困在起始位置时,

:网格已经准备就绪。



:剩下的唯一问题应该是:什么使用它??

:嗯,我可以想到很多选项,但是一开始只需要一个

:鼠标在labyrint中;-)



:最后我要感谢两个人:

:" Kai-Uwe Bux"

:为了帮助我完成数据结构。

:有人记不起名字

:为了提供网格生成的想法。



:当然我要感谢其他人削减总额n00b

:一些渣。 ;-)



:现在代码:



:@code start

:#include< iostream>

:#include< vector>

:#include< bitset>

:#include< ctime>

:const unsigned short Size = 4; // 2 ^ N

:const unsigned short Log2 = 2; // N,log2(大小)

:std :: bitset<尺寸*尺寸*尺寸> YZ;

:std :: bitset<尺寸*尺寸*尺寸> XZ;

:std :: bitset<尺寸*尺寸*尺寸> XY;

:std :: bitset<尺寸*尺寸*尺寸> V;

:struct Stack {

:unsigned short z:Log2;

:unsigned short y:Log2;

:unsigned short x:Log2;

:bool dir [6];

:};

:struct {

:union {

:struct {

注意:匿名结构成员不是C ++的标准/便携式功能/>

:unsigned short z:Log2;

:unsigned short y:Log2;

:unsigned short x:Log2;

:};

:unsigned long long id:Log2 * 3;

:};

你确实在玩这里消防。

此外,使用位字段可能是性能上的悲观而不是优化的b $ b。你检查了吗?


:} pos;

:短rand_int(短i){

:返回rand()% i;

使用

rand()返回的值的高位通常提供更好的''随机性'',

但这可能不是这里有一个大问题...


:}

:void new_grid(){

:std :: vector< Stack> ; stack(1);

:bool loop = true;

注意:在C ++中,它被视为最佳实践

声明本地使用前的变量,

尽可能晚。


:YZ.set();

:XZ.set( );

:XY.set();

:V.set();

:V.flip();

注意:V.reset()相当于set()+ flip()


:srand(time(0));

:pos.x = rand_int(6);

:pos.y = rand_int(6);

:pos.z = rand_int(6);

''6''不是你想要的值范围

for pos! - > ''尺寸''而不是


:V [pos.id] = 1;

如果这适用于您的平台,则由

纯粹的机会。

使用direcly会好得多:

V [pos.x + Size *(pos.y + Size * pos.z) ]

(可能有一个内联函数

来写V [makeIndex(x,y,z)])

你可以摆脱它''pos''变量

一共(替换为:

unsigned short posx,posy,posz;)


:stack.back()。x = pos.x;

:stack.back()。y = pos.y;

:stack.back()。z = pos.z;

:stack.resize(stack.size()+ 1);

你需要做的是先创建一个
$ b $'元素类型''Stack''(实际上是一个堆栈项),

然后调用:

Stack item;

item。 x = pos.x;

item.y = pos.y;

item.z = pos.z;

stack.push_back( item);

更好的是:在struct Stack中添加一个构造函数

x,y,z位置作为参数。


:while(stack.size()> 0){

或:while(!stack.empty())//更干净的IMO


:for(int choice = rand_int(6),循环= true;

:loop == true;

:choice = rand_int(6)){

摆脱这个内循环(参见保证''选择''作为第一个陈述:

int choice = rand_int(6);

另外:

bool loop = true; //如果需要的话...


:stack.back()。dir [choice] = 1;

你应该首先检查一下dir [choice]是零,

否则你可以跳过所有其余的:

if(stack.back()。dir [choice]!= 0)继续;


而不是if-s链,你真的应该使用:b
$ b开关(选择){

案例0:...

案例1:......



}

:如果(选择== 0){

:pos.x ++;

:if(V [pos.id] == 1)pos.x--;

:else {

:loop = false;

:pos.x--;

:YZ [pos.id] = 0;

:pos.x ++;


:}

:}

:else if(choice == 1){

:pos.y ++;

:if(V [pos.id] == 1)pos.y - ;

:else {

:loop = false;

:pos.y--;

:XZ [pos。 id] = 0;

:pos.y ++;

:}

:}

:否则if(choice == 2){

:pos.z ++;

:if(V [pos.id] == 1)pos.z--;

:else {

:loop = false;

:pos.z--;

:XY [pos.id] = 0;

:pos.z ++;

:}

:}

:否则if(choice == 3){

:pos.x--;

:if(V [pos.id] == 1)pos.x ++;

:else {

:loop = false;

:YZ [pos.id] = 0;

:}

:}

:否则如果(选择== 4){

:pos.y--;

:if(V [pos.id] == 1)pos.y ++;

:else {

:loop = false;

:XZ [pos.id] = 0;

:}

:}

:否则if(choice == 5){

:pos.z - ;

:if(V [pos.id] == 1)pos.z ++;

:else {

:loop = false;

:XY [pos.id] = 0;

:}

:}

:if(loop == true& &

:stack.back()。dir [0] + stack.back()。dir [1] + stack.back()。dir [2] +

:stack.back()。dir [3] + stack.back()。dir [4] + stack.back()。dir [5]

:== 6){

:loop = false;

:stack.pop_back();

:pos.x = stack.back()。x;

:pos.y = stack.back()。y;

:pos.z = stack.back()。z;

:}

:else if(loop == false){

:V [pos.id] = 1;

:stack.back() .x = pos.x;

:stack.back()。y = pos.y;

:stack.back()。z = pos.z;

:stack.resize(stack.size()+ 1);

:}

if(!循环){

V.set(makeInd [posx,posy,posz]);

stack.push_back(StackItem(posx,posy,posz))

{

if if(...所有路线都清楚......){

stack.pop_back();

}

//不需要额外的内循环,

//所有都发生在堆栈中


:}

:////临时进度概述///////////

:/ ** / system(" cls"); / ** /

:/ ** / std :: cout<< YZ <<的std :: ENDL; / ** /

:/ ** / std :: cout<< XZ<<的std :: ENDL; / ** /

:/ ** / std :: cout<< XY<<的std :: ENDL; / ** /

:/ ** / std :: cout<< V; / ** /

:///////////////////////////////////// //

:}

:}



:int main(){

:while(0 == 0)

:new_grid();

:}

:@code end



:现在,这段代码远远没有完成,但这确实有效,

:这是进步,应该庆祝。



:现在讨论的最重要的事情是:

:1)有些不合情理的联盟。

它'不,不。使用返回

索引的成员函数,或单独的索引制作函数。

位域是不必要的悲观化。


:2)resize(size()+ 1)

使用push_back,为你的堆栈元素添加一个构造函数。


:3)Log2 ,大小

如果你摆脱所有的位域,Log2是不必要的。


:4)最后一个if循环女巫看起来并不好;-)

摆脱它。

另外请记住,你可以继续使用; / break;


:5)网格生成代码确实需要进行一些优化。

请参阅代码中的注释以使更浅的''错误命中''。

一个bool dir [6]数组的Insead,你可以使用

一些算术:

unsigned char dirFlags;

首先用0初始化它(清除所有位)。

然后:

bool dirWasVisited = 0!=(dirFlags&(1<<选择));

设置为访问:

dirFlags | =(1<< choice);

检查是否所有访问过:

bool allVisited =(dirFlags ==(1<< 6)-1);


:6)你应该感兴趣的任何其他问题。

在最坏的情况下,你可以重新开始(即将物品添加到堆栈中)和pow(尺寸,3)一样深,这可能是

是性能问题,可能不是你想要的。

可能有其他方法......


:NB。

:这段代码确实需要优化zing和改变但是没有编程

:不超过一个月,还有很多我不知道/理解,

:所以请给我一些意见。

对于刚入门的人来说一点都不差;)

我希望这会有所帮助,

Ivan

-
http://ivan.vecerina.com/contact/?subject = NG_POST < - 电子邮件联系表格

<fe**********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
: Some might remember that i, not so long ago, started a treath or two
: about a weird 3d labyrinth. I now have a working code, that i want to
: share, hear comments, advice, ect., but first let me explain what its
: all about.
:
: The whole labyrinth is a cubic in its self and it contains x^3 cubic
: rooms.
: The labyrinth is infinite/finite, it has no borders, but still have a
: size.
: fx. if the size of the labytrint is 2^3 and you find yourself at
: coordinate 3,3,3 you''re really finding your self at the coordinate
: 1,1,1.
: Each room has 6 wall/possible doors, however, adjacent rooms shares a
: wall, so we only need to keep track of three wall per room.
: A wall can be set or unset, if unset, ofcourse there is nowall and your
: free to move to the next room.
: The grid pattern follow one simple room: "you must, at all time be able
: to find a path from one room to another."
: This tricky problem is solved by setting all walls in the labyrinth and
: setting a start position at random, then picking a direction at random.
: If the room in that direction have''nt been visited yet, brake down the
: wall and move there and start over, else try again. If you cant move
: any further, pop back to last position and start over from there. The
: grid is ready when you are stuck at the start position.
:
: The only question left should be: "what to use it for?"
: Well, i can think of many options, but for a start lets just this of a
: mouse in a labyrint ;-)
:
: Last i would like to thank two people:
: "Kai-Uwe Bux"
: For helping me alot with the datastructure.
: "someone i cant remember the name of"
: For suplying the idea for the grid generation.
:
: And ofcourse i want to thank everyone else for cutting a total n00b
: some slag. ;-)
:
: Now for the code:
:
: @code start
: #include <iostream>
: #include <vector>
: #include <bitset>
: #include <ctime>
: const unsigned short Size = 4; // 2^N
: const unsigned short Log2 = 2; // N, log2(Size)
: std::bitset<Size*Size*Size> YZ;
: std::bitset<Size*Size*Size> XZ;
: std::bitset<Size*Size*Size> XY;
: std::bitset<Size*Size*Size> V;
: struct Stack {
: unsigned short z:Log2;
: unsigned short y:Log2;
: unsigned short x:Log2;
: bool dir[6];
: };
: struct {
: union {
: struct {
NB: anonymous struct members are not a
standard/portable feature of C++

: unsigned short z:Log2;
: unsigned short y:Log2;
: unsigned short x:Log2;
: };
: unsigned long long id:Log2*3;
: };
You are indeed playing with fire here.
Furthermore, using bit-fields might be
a performance pessimization rather than
an optimization. Have you checked ?

: } pos;
: short rand_int(short i) {
: return rand() % i;
Using upper bits of the value returned by
rand() usually provides better ''randomness'',
but this might not a big deal here...

: }
: void new_grid() {
: std::vector<Stack> stack(1);
: bool loop = true;
NB: in C++, it is seen as a best practice to
declare local variables just before use,
as late as possible.

: YZ.set();
: XZ.set();
: XY.set();
: V.set();
: V.flip();
NB: V.reset() is equivalent to set() + flip()

: srand(time(0));
: pos.x = rand_int(6);
: pos.y = rand_int(6);
: pos.z = rand_int(6);
''6'' is not the range of values you want
for pos ! --> ''Size'' instead

: V[pos.id] = 1;
If this works on your platform, it is by
pure chance.
It is much better to use direcly:
V[ pos.x + Size*(pos.y+Size*pos.z) ]
( possibly have an inline function
to write V[ makeIndex(x,y,z)] )
And you could get rid of the ''pos'' variable
altogether (replace with:
unsigned short posx, posy, posz; )

: stack.back().x = pos.x;
: stack.back().y = pos.y;
: stack.back().z = pos.z;
: stack.resize(stack.size()+1);
What you need to do is first create an
element of type ''Stack'' (really a stack item),
and then call:
Stack item;
item.x = pos.x;
item.y = pos.y;
item.z = pos.z;
stack.push_back( item );
Even better: add a constructor to struct Stack
that takes the x,y,z position as a parameter.

: while (stack.size() > 0) {
or: while( ! stack.empty() ) // cleaner IMO

: for (int choice = rand_int(6), loop = true;
: loop == true;
: choice = rand_int(6)) {
Get rid of this inner loop (see below).
Keep ''choice'' as a first statement:
int choice = rand_int(6);
Also:
bool loop = true; // if needed...

: stack.back().dir[choice] = 1;
You should first check that dir[choice] is zero,
otherwise you can skip all the rest:
if( stack.back().dir[choice]!=0 ) continue;

Instead of the chain of if-s, you really
should use:
switch( choice ) {
case 0: ...
case 1: ...
etc.
}
: if (choice == 0) {
: pos.x++;
: if (V[pos.id] == 1) pos.x--;
: else {
: loop = false;
: pos.x--;
: YZ[pos.id] = 0;
: pos.x++;

: }
: }
: else if (choice == 1) {
: pos.y++;
: if (V[pos.id] == 1) pos.y--;
: else {
: loop = false;
: pos.y--;
: XZ[pos.id] = 0;
: pos.y++;
: }
: }
: else if (choice == 2) {
: pos.z++;
: if (V[pos.id] == 1) pos.z--;
: else {
: loop = false;
: pos.z--;
: XY[pos.id] = 0;
: pos.z++;
: }
: }
: else if (choice == 3) {
: pos.x--;
: if (V[pos.id] == 1) pos.x++;
: else {
: loop = false;
: YZ[pos.id] = 0;
: }
: }
: else if (choice == 4) {
: pos.y--;
: if (V[pos.id] == 1) pos.y++;
: else {
: loop = false;
: XZ[pos.id] = 0;
: }
: }
: else if (choice == 5) {
: pos.z--;
: if (V[pos.id] == 1) pos.z++;
: else {
: loop = false;
: XY[pos.id] = 0;
: }
: }
: if (loop == true &&
: stack.back().dir[0]+stack.back().dir[1]+stack.back().dir[2]+
: stack.back().dir[3]+stack.back().dir[4]+stack.back().dir[5]
: == 6) {
: loop = false;
: stack.pop_back();
: pos.x = stack.back().x;
: pos.y = stack.back().y;
: pos.z = stack.back().z;
: }
: else if (loop == false) {
: V[pos.id] = 1;
: stack.back().x = pos.x;
: stack.back().y = pos.y;
: stack.back().z = pos.z;
: stack.resize(stack.size()+1);
: }
if( ! loop ) {
V.set( makeInd[posx,posy,posz] );
stack.push_back( StackItem(posx,posy,posz) )
{
else if( ... all directions are clear ... ) {
stack.pop_back();
}
// no need for the additional inner loop,
// all happens through the stack

: }
: //// temp progress overview ///////////
: /**/ system("cls"); /**/
: /**/ std::cout << YZ << std::endl; /**/
: /**/ std::cout << XZ << std::endl; /**/
: /**/ std::cout << XY << std::endl; /**/
: /**/ std::cout << V; /**/
: ///////////////////////////////////////
: }
: }
:
: int main() {
: while (0==0)
: new_grid();
: }
: @code end
:
: Now, this code is ofcouse far from finished, but this actually work,
: that is progress and should be celebrated.
:
: Now the most important things to discuss would be:
: 1) the somewhat inapropiate union.
It''s a no-no. Use a member function that returns an
index, or a separate index-making function.
The bitfields are an unnecessary pessimization.

: 2) the resize(size()+1)
Use push_back, add a constructor for your stack element.

: 3) the Log2, Size
Log2 is unnecessary if you get rid of all the bitfields.

: 4) the last if loop witch doesnt look good at all ;-)
Get rid of it.
Also keep in mind that you can use continue; / break;

: 5) the grid generation code that is does indeed need some optimizing.
See notes in the code to make shallower ''mis-hits''.
Insead of a bool dir[6] array, you could use
some bit arithmetic:
unsigned char dirFlags;
First initialize it with 0 (all bits cleared).
Then:
bool dirWasVisited = 0!=( dirFlags&(1<<choice) );
To set as visited:
dirFlags |= (1<<choice);
To check if all was visited:
bool allVisited = ( dirFlags == (1<<6)-1 );

: 6) any other issue that you should find interesting.
In the worst case, you could recurce (i.e. add items
to the stack) as deeply as pow(Size,3), which could
be a performance problem, and might not be what you want.
There could be other approaches...

: NB.
: This code does indeed need optimizing and altering but haven programed
: for no longer than a month, theres still alot i dont know/understand,
: so please give me some input.
Not bad at all for someone who just got started ;)
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form




Ivan Vecerina skrev:

Ivan Vecerina skrev:
< fe ********** @ hotmail.com>在消息中写道
新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ......
:有些人可能还记得我,不久前,开始了一两次生病:关于一个奇怪的3D迷宫。我现在有一个工作代码,我想要分享,听取意见,建议等等,但首先让我解释一下它的全部内容。

:整个迷宫是一个立方体,它包含x ^ 3立方
:房间。
:迷宫是无限/有限的,它没有边框,但仍然有一个
:尺寸。
:fx。如果labytrint的大小是2 ^ 3并且你发现自己在
:坐标3,3,3你真的在坐标处找到你的自己
:1,1,1。
:每个房间有6个墙/可能的门,但是,相邻的房间共用一个墙:所以我们只需要跟踪每个房间的三面墙。
:墙可以设置或取消设置如果没有设置,当然有现在和你的
:自由移动到下一个房间。
:网格图案遵循一个简单的房间:你必须,在任何时候都能够
:找到从一个房间到另一个房间的路径。
:通过设置迷宫中的所有墙壁来解决这个棘手的问题:
:随机设置一个起始位置,然后随机选择一个方向。
:如果那个方向的房间还没有被访问过,请将
:墙制动并移动到那里重新开始,否则再试一次。如果你不能移动
:再进一步,回到最后一个位置并从那里重新开始。当你被困在起始位置时,
:网格已准备就绪。

:唯一的问题应该是:使用它的原因是什么?
:好吧,我可以想到很多选择,但是一开始就让我们这样做一个
:鼠标在一个labyrint ;-)

:最后我要感谢两个人:
:Kai-Uwe Bux
:为了帮助我完成数据结构。
:有些人我不记得
的名字:为了提供这个想法网格生成。

:当然我要感谢其他人一共削减n00b
:一些渣。 ;-)

:现在代码:

:@code start
:#include< iostream>
:#include< ; vector>
:#include< bitset>
:#include< ctime>
:const unsigned short Size = 4; // 2 ^ N
:const unsigned short Log2 = 2; // N,log2(大小)
:std :: bitset<尺寸*尺寸*尺寸> YZ;
:std :: bitset<尺寸*尺寸*尺寸> XZ;
:std :: bitset<尺寸*尺寸*尺寸> XY;
:std :: bitset<尺寸*尺寸*尺寸> V;
:struct Stack {
:unsigned short z:Log2;
:unsigned short y:Log2;
:unsigned short x:Log2;
:bool dir [6];
:};
:struct {
:union {
:struct {
注意:匿名结构成员不是
标准/ C ++的便携功能


好​​吧,看过它使用了很多,所以我没有看到它作为一个问题,它

确实有解决方案。 br />
:unsigned short z:Log2;
:unsigned short y:Log2;
:unsigned short x:Log2;
:};
:unsigned long long id:Log2 * 3;
:};
你确实在这里玩火。
此外,使用位字段可能是性能上的悲观而不是
优化。你检查过吗?


在这个位域为2的情况下,让我解释一下我为什么要做的事情。

吧。

@code start

pos.x = 3;

pos.x ++;

// pos x现在应该包含值0

@code结束


我知道它现在是解决这个问题的明智方法,但是我的

意见最简单的。

否则我每次想要签收时都要检查号码或

判决它。

:} pos;
:短rand_int(短i) {
:返回rand()%i;
使用
rand()返回的值的高位通常会提供更好的随机性,
但这可能不是这里很重要...


i你不觉得这个问题。

:}
:void new_grid(){
:std :: vector< Stack> stack(1);
:bool loop = true;
注意:在C ++中,它被视为在使用之前声明局部变量的最佳实践,
可能。


好​​的,你每天都学习; ;-)
:YZ.set();
:XZ.set();
:XY.set();
:V.set() ;
:V.flip();
注意:V.reset()相当于set()+ flip()


ofcourse。

:srand(时间(0));
:pos.x = rand_int(6);
:pos.y = rand_int(6);
:pos.z = rand_int(6);
''6''不是你想要的值的范围
pos! - > ''尺寸''而不是


a小错字,感谢注意;-)

:V [pos.id] = 1;
如果这可以在你的平台上运行,它纯粹是偶然的。
直接使用它会好得多:
V [pos.x + Size *(pos.y + Size * pos.z )]
(可能有一个内联函数
来写V [makeIndex(x,y,z)])
你可以摆脱''pos''变量完全(替换为:
unsigned short posx,posy,posz;)


雅,我已经知道了这一点,并在感觉不舒服时改变它,但是/>
i需要一个针对位域问题的解决方案才能得到''pos''

一共。

:stack.back()。x = pos.x;
:stack.back()。y = pos.y;
:stack.back()。z = pos.z;
:stack.resize(stack.size ()+1);
你需要做的是首先创建一个''Stack'类型的元素(实际上是一个堆栈项目),
然后调用:
堆叠项目;
item.x = pos.x;
item.y = pos.y;
item.z = pos.z;
stack.push_back(item);
更好:添加一个构造函数struct Stack
将x,y,z位置作为参数。


这里我只是困惑,我已经听到了表达

" constructor"但它不是在课堂上使用的东西,而且它是如何解决问题的?b / b
:while(stack.size()> 0){
或:while(!stack.empty())//更干净IMO


确实......
:for(int choice = rand_int(6),loop = true;
:loop == true;
:choice = rand_int(6)){
摆脱这个内循环(见下文)。
保持''选择'作为第一个声明:
int choice = rand_int(6);
另外:
bool loop = true; //如果需要的话...


i已经尝试了,但我一直都在失败......
:stack.back()。dir [choice] = 1;
你应该首先检查dir [choice]是否为零,否则你可以跳过所有其余的:
if(stack.back()。dir [choice]!= 0)继续;


我在这里有些困惑...

而不是if-s链,你真的应该使用:
switch (选择){
案例0:......
案例1:...

}


已经完成; - )

:if(choice == 0){
:pos.x ++;
:if(V [pos.id] == 1)pos.x-- ;
:else {
:loop = false;
:pos.x--;
:YZ [pos.id] = 0;
:pos.x ++ ;

:}
:}
:if if(choice == 1){
:pos.y ++;
:if(V [pos .id] == 1)pos.y--;
:else {
:loop = false;
:pos.y--;
:XZ [pos.id ] = 0;
:pos.y ++;
:}
:}
:else if(choice == 2){
:pos.z ++;
:if(V [pos.id] == 1)pos.z--;
:else {
:loop = false;
:pos.z - ;
: XY [pos.id] = 0;
:pos.z ++;
:}
:}
:if if(choice == 3){
:pos .x--;
:if(V [pos.id] == 1)pos.x ++;
:else {
:loop = false;
:YZ [pos .id] = 0;
:}
:}
:if if(choice == 4){
:pos.y--;
:if( V [pos.id] == 1)pos.y ++;
:else {
:loop = false;
:XZ [pos.id] = 0;
:}
:}
:if if(choice == 5){
:pos.z--;
:if(V [pos.id] == 1)pos。 z ++;
:else {
:loop = false;
:XY [pos.id] = 0;
:}
:}
:if (loop == true&&
:stack.back()。dir [0] + stack.back()。dir [1] + stack.back()。dir [2] +
:stack.back()。dir [3] + stack.back()。dir [4] + stack.back()。dir [5]
:== 6){
:loop = false;
:stac k.pop_back();
:pos.x = stack.back()。x;
:pos.y = stack.back()。y;
:pos.z = stack .back()。z;
:}
:else if(loop == false){
:V [pos.id] = 1;
:stack.back( ).x = pos.x;
:stack.back()。y = pos.y;
:stack.back()。z = pos.z;
:stack.resize (stack.size()+ 1);
:}
if(!循环){
V.set(makeInd [posx,posy,posz]);
stack.push_back(StackItem(posx,posy,posz))
{
如果( ......所有方向都很明确......){
stack.pop_back();
}
//不需要额外的内循环,
//全部通过堆栈

:}
:////临时进度概述///////////
:/ ** / system(" cls" ); / ** /
:/ ** / std :: cout<< YZ <<的std :: ENDL; / ** /
:/ ** / std :: cout<< XZ<<的std :: ENDL; / ** /
:/ ** / std :: cout<< XY<<的std :: ENDL; / ** /
:/ ** / std :: cout<< V; / ** /
:///////////////////////////////////////
:}
:}

:int main(){
:while(0 == 0)
:new_grid();
:}
:@code end

:现在,这段代码远远没有完成,但这实际上是有效的,
:这是进步,应该庆祝。

:现在讨论的最重要的事情是:
:1)有点不合情理的联盟。
这是不 - 不。使用返回
索引的成员函数或单独的索引制作函数。
位域是不必要的悲观化。


有些东西在上面说了...

:2)调整大小(size()+ 1)
使用push_back,添加一个构造函数你的堆栈元素。


这样做了,但我最终得到的是现在的两倍,不要问我

为什么,从来没有像现在这样糟糕的想法时间。

:3)Log2,大小
Log2是不必要的,如果你摆脱所有的位域。


但是直到我找到解决上述问题的++ / - 问题...

:4)最后一个if循环巫婆看起来不太好在所有;-)
摆脱它。
还要记住,你可以使用继续; / break;


再次相当困惑......

:5)网格生成代码确实需要进行一些优化。
请参阅代码中的注释更糟糕的''误击''。
一个bool dir [6]数组的内置,你可以使用
一些算术:
unsigned char dirFlags;
首先初始化它为0(所有位清除)。
然后:
bool dirWasVisited = 0!=(dirFlags&(1<< choice));
设置为访问:
dirFlags |= (1<<choice);
To check if all was visited:
bool allVisited = ( dirFlags == (1<<6)-1 );


ya....

: 6) any other issue that you should find interesting.
In the worst case, you could recurce (i.e. add items
to the stack) as deeply as pow(Size,3), which could
be a performance problem, and might not be what you want.
There could be other approaches...


Im listening...

: NB.
: This code does indeed need optimizing and altering but haven programed
: for no longer than a month, theres still alot i dont know/understand,
: so please give me some input.
Not bad at all for someone who just got started ;)


While thank you ;-)

I hope this helps,
Ivan
<fe**********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
: Some might remember that i, not so long ago, started a treath or two
: about a weird 3d labyrinth. I now have a working code, that i want to
: share, hear comments, advice, ect., but first let me explain what its
: all about.
:
: The whole labyrinth is a cubic in its self and it contains x^3 cubic
: rooms.
: The labyrinth is infinite/finite, it has no borders, but still have a
: size.
: fx. if the size of the labytrint is 2^3 and you find yourself at
: coordinate 3,3,3 you''re really finding your self at the coordinate
: 1,1,1.
: Each room has 6 wall/possible doors, however, adjacent rooms shares a
: wall, so we only need to keep track of three wall per room.
: A wall can be set or unset, if unset, ofcourse there is nowall and your
: free to move to the next room.
: The grid pattern follow one simple room: "you must, at all time be able
: to find a path from one room to another."
: This tricky problem is solved by setting all walls in the labyrinth and
: setting a start position at random, then picking a direction at random.
: If the room in that direction have''nt been visited yet, brake down the
: wall and move there and start over, else try again. If you cant move
: any further, pop back to last position and start over from there. The
: grid is ready when you are stuck at the start position.
:
: The only question left should be: "what to use it for?"
: Well, i can think of many options, but for a start lets just this of a
: mouse in a labyrint ;-)
:
: Last i would like to thank two people:
: "Kai-Uwe Bux"
: For helping me alot with the datastructure.
: "someone i cant remember the name of"
: For suplying the idea for the grid generation.
:
: And ofcourse i want to thank everyone else for cutting a total n00b
: some slag. ;-)
:
: Now for the code:
:
: @code start
: #include <iostream>
: #include <vector>
: #include <bitset>
: #include <ctime>
: const unsigned short Size = 4; // 2^N
: const unsigned short Log2 = 2; // N, log2(Size)
: std::bitset<Size*Size*Size> YZ;
: std::bitset<Size*Size*Size> XZ;
: std::bitset<Size*Size*Size> XY;
: std::bitset<Size*Size*Size> V;
: struct Stack {
: unsigned short z:Log2;
: unsigned short y:Log2;
: unsigned short x:Log2;
: bool dir[6];
: };
: struct {
: union {
: struct {
NB: anonymous struct members are not a
standard/portable feature of C++
ok, have seen it used alot, so i didnt see this of it as a problem, it
does however have solution.

: unsigned short z:Log2;
: unsigned short y:Log2;
: unsigned short x:Log2;
: };
: unsigned long long id:Log2*3;
: };
You are indeed playing with fire here.
Furthermore, using bit-fields might be
a performance pessimization rather than
an optimization. Have you checked ?
In this case where the bitfield is 2, let me explain why i have done
it.
@code start
pos.x = 3;
pos.x++;
// pos x should contain the value 0 now.
@code end

I know its now the wises way to go around that problem, but i my
oppinion the easyest.
Otherwise i have to check the number everytime i want to increement or
decreement it.

: } pos;
: short rand_int(short i) {
: return rand() % i;
Using upper bits of the value returned by
rand() usually provides better ''randomness'',
but this might not a big deal here...
i asure you this is not a problem.

: }
: void new_grid() {
: std::vector<Stack> stack(1);
: bool loop = true;
NB: in C++, it is seen as a best practice to
declare local variables just before use,
as late as possible.
Ok, you learn every day; ;-)

: YZ.set();
: XZ.set();
: XY.set();
: V.set();
: V.flip();
NB: V.reset() is equivalent to set() + flip()
ofcourse.

: srand(time(0));
: pos.x = rand_int(6);
: pos.y = rand_int(6);
: pos.z = rand_int(6);
''6'' is not the range of values you want
for pos ! --> ''Size'' instead
a little typo, thanks for noticing ;-)

: V[pos.id] = 1;
If this works on your platform, it is by
pure chance.
It is much better to use direcly:
V[ pos.x + Size*(pos.y+Size*pos.z) ]
( possibly have an inline function
to write V[ makeIndex(x,y,z)] )
And you could get rid of the ''pos'' variable
altogether (replace with:
unsigned short posx, posy, posz; )
Ya, i allready know this, and ill change it when feel like it, however
i need a solution for bitfield problem before i can get rit of ''pos''
alltogether.

: stack.back().x = pos.x;
: stack.back().y = pos.y;
: stack.back().z = pos.z;
: stack.resize(stack.size()+1);
What you need to do is first create an
element of type ''Stack'' (really a stack item),
and then call:
Stack item;
item.x = pos.x;
item.y = pos.y;
item.z = pos.z;
stack.push_back( item );
Even better: add a constructor to struct Stack
that takes the x,y,z position as a parameter.
Here im just confused, i have ofcourse hear the expression
"constructor" but isnt it something to use in classes and how cant it
solve the problem?

: while (stack.size() > 0) {
or: while( ! stack.empty() ) // cleaner IMO
Indeed...
: for (int choice = rand_int(6), loop = true;
: loop == true;
: choice = rand_int(6)) {
Get rid of this inner loop (see below).
Keep ''choice'' as a first statement:
int choice = rand_int(6);
Also:
bool loop = true; // if needed...
i have tryed, but i keep failing...
: stack.back().dir[choice] = 1;
You should first check that dir[choice] is zero,
otherwise you can skip all the rest:
if( stack.back().dir[choice]!=0 ) continue;
Im somewhat confused here...

Instead of the chain of if-s, you really
should use:
switch( choice ) {
case 0: ...
case 1: ...
etc.
}
allready done ;-)
: if (choice == 0) {
: pos.x++;
: if (V[pos.id] == 1) pos.x--;
: else {
: loop = false;
: pos.x--;
: YZ[pos.id] = 0;
: pos.x++;

: }
: }
: else if (choice == 1) {
: pos.y++;
: if (V[pos.id] == 1) pos.y--;
: else {
: loop = false;
: pos.y--;
: XZ[pos.id] = 0;
: pos.y++;
: }
: }
: else if (choice == 2) {
: pos.z++;
: if (V[pos.id] == 1) pos.z--;
: else {
: loop = false;
: pos.z--;
: XY[pos.id] = 0;
: pos.z++;
: }
: }
: else if (choice == 3) {
: pos.x--;
: if (V[pos.id] == 1) pos.x++;
: else {
: loop = false;
: YZ[pos.id] = 0;
: }
: }
: else if (choice == 4) {
: pos.y--;
: if (V[pos.id] == 1) pos.y++;
: else {
: loop = false;
: XZ[pos.id] = 0;
: }
: }
: else if (choice == 5) {
: pos.z--;
: if (V[pos.id] == 1) pos.z++;
: else {
: loop = false;
: XY[pos.id] = 0;
: }
: }
: if (loop == true &&
: stack.back().dir[0]+stack.back().dir[1]+stack.back().dir[2]+
: stack.back().dir[3]+stack.back().dir[4]+stack.back().dir[5]
: == 6) {
: loop = false;
: stack.pop_back();
: pos.x = stack.back().x;
: pos.y = stack.back().y;
: pos.z = stack.back().z;
: }
: else if (loop == false) {
: V[pos.id] = 1;
: stack.back().x = pos.x;
: stack.back().y = pos.y;
: stack.back().z = pos.z;
: stack.resize(stack.size()+1);
: }
if( ! loop ) {
V.set( makeInd[posx,posy,posz] );
stack.push_back( StackItem(posx,posy,posz) )
{
else if( ... all directions are clear ... ) {
stack.pop_back();
}
// no need for the additional inner loop,
// all happens through the stack

: }
: //// temp progress overview ///////////
: /**/ system("cls"); /**/
: /**/ std::cout << YZ << std::endl; /**/
: /**/ std::cout << XZ << std::endl; /**/
: /**/ std::cout << XY << std::endl; /**/
: /**/ std::cout << V; /**/
: ///////////////////////////////////////
: }
: }
:
: int main() {
: while (0==0)
: new_grid();
: }
: @code end
:
: Now, this code is ofcouse far from finished, but this actually work,
: that is progress and should be celebrated.
:
: Now the most important things to discuss would be:
: 1) the somewhat inapropiate union.
It''s a no-no. Use a member function that returns an
index, or a separate index-making function.
The bitfields are an unnecessary pessimization.
Some stuff ecplained above...

: 2) the resize(size()+1)
Use push_back, add a constructor for your stack element.
Did that, but i ended up with twice the lengt it is now, dont ask me
why, never the less it seemed like a bad idea at the time.

: 3) the Log2, Size
Log2 is unnecessary if you get rid of all the bitfields.
But until i find a solution for the ++/-- problem explained above...

: 4) the last if loop witch doesnt look good at all ;-)
Get rid of it.
Also keep in mind that you can use continue; / break;
Again pretty confused...

: 5) the grid generation code that is does indeed need some optimizing.
See notes in the code to make shallower ''mis-hits''.
Insead of a bool dir[6] array, you could use
some bit arithmetic:
unsigned char dirFlags;
First initialize it with 0 (all bits cleared).
Then:
bool dirWasVisited = 0!=( dirFlags&(1<<choice) );
To set as visited:
dirFlags |= (1<<choice);
To check if all was visited:
bool allVisited = ( dirFlags == (1<<6)-1 );
ya....

: 6) any other issue that you should find interesting.
In the worst case, you could recurce (i.e. add items
to the stack) as deeply as pow(Size,3), which could
be a performance problem, and might not be what you want.
There could be other approaches...
Im listening...

: NB.
: This code does indeed need optimizing and altering but haven programed
: for no longer than a month, theres still alot i dont know/understand,
: so please give me some input.
Not bad at all for someone who just got started ;)
While thank you ;-)

I hope this helps,
Ivan




This must have taken a great deal of time to go trough, so i just want

to say thank you ;-)


Best

Zacariaz



This must have taken a great deal of time to go trough, so i just want
to say thank you ;-)

Best
Zacariaz


Code altered a bit.


@code start

#include <iostream& gt;

#include <vector>

#include <bitset>

#include <ctime>

const unsigned short Size = 4; // 2^N

const unsigned short Log2 = 2; // N, log2(Size)

std::bitset<Size*Size*Size> YZ;

std::bitset<Size*Size*Size> XZ;

std::bitset<Size*Size*Size> XY;

std::bitset<Size*Size*Size> V;

struct Stack {

unsigned short z:Log2;

unsigned short y:Log2;

unsigned short x:Log2;

bool dir[6];

};

struct {

union {

struct {

unsigned short z:Log2;

unsigned short y:Log2;

unsigned short x:Log2;

};

unsigned long long id:Log2*3;

};

} pos;

short rand_int(short i) {

return rand() % i;

}

void new_grid() {

std::vector<Stack> stack(1);

bool loop = true;

YZ.set();

XZ.set();

XY.set();

V.reset();

srand(time(0));

pos.x = rand_int(Size);

pos.y = rand_int(Size);

pos.z = rand_int(Size);

V[pos.id] = 1;

stack.back().x = pos.x;

stack.back().y = pos.y;

stack.back().z = pos.z;

stack.resize(stack.size()+1);

while (!stack.empty()) {

for (int choice = rand_int(6), loop = true;

loop; choice = rand_int(6)) {

stack.back().dir[choice] = 1;

switch(choice) {

case 0:

pos.x++;

if (V[pos.id] == 1) pos.x--;

else {

loop = false;

pos.x--;

YZ[pos.id] = 0;

pos.x++;

}

break;

case 1:

pos.y++;

if (V[pos.id] == 1) pos.y--;

else {

loop = false;

pos.y--;
$b$ b XZ[pos.id] = 0;

pos.y++;

}

break;

case 2:

pos.z++;

if (V[pos.id] == 1) pos.z--;

else {

loop = false;

pos.z--;

XY[pos.id] = 0;

pos.z++;

}

break;

case 3:

pos.x--;

if (V[pos.id] == 1) pos.x++;

else {

loop = false;

YZ[pos.id] = 0;

}

break;

case 4:

pos.y--;

if (V[pos.id] == 1) pos.y++;

else {

loop = false;

XZ[pos.id] = 0;

}

break;

case 5:

pos.z--;

if (V[pos.id] == 1) pos.z++;

else {

loop = false;

XY[pos.id] = 0;

}

break;

}

if (!loop) {

V[pos.id] = 1;

stack.back().x = pos.x;

stack.back().y = pos.y;

stack.back().z = pos.z;

stack.resize(stack.size()+1);

}

else if (stack.back().dir[0] == 1 && stack.back().dir[1] == 1 &&

stack.back().dir[2] == 1 && stack.back().dir[3] == 1 &&

stack.back().dir[4] == 1 && stack.back().dir[5] == 1) {

loop = false;

stack.pop_back();

pos.x = stack.back().x;

pos.y = stack.back().y;

pos.z = stack.back().z;

}

}

//// temp progress overview ///////////

/**/ system("cls"); /**/

/**/ std::cout << YZ <<的std :: ENDL; /**/

/**/ std::cout << XZ <<的std :: ENDL; /**/

/**/ std::cout << XY <<的std :: ENDL; /**/

/**/ std::cout << V; /**/

///////////////////////////////////////

}

}


int main() {

while (true)

new_grid();

}

@code end

Code altered a bit.

@code start
#include <iostream>
#include <vector>
#include <bitset>
#include <ctime>
const unsigned short Size = 4; // 2^N
const unsigned short Log2 = 2; // N, log2(Size)
std::bitset<Size*Size*Size> YZ;
std::bitset<Size*Size*Size> XZ;
std::bitset<Size*Size*Size> XY;
std::bitset<Size*Size*Size> V;
struct Stack {
unsigned short z:Log2;
unsigned short y:Log2;
unsigned short x:Log2;
bool dir[6];
};
struct {
union {
struct {
unsigned short z:Log2;
unsigned short y:Log2;
unsigned short x:Log2;
};
unsigned long long id:Log2*3;
};
} pos;
short rand_int(short i) {
return rand() % i;
}
void new_grid() {
std::vector<Stack> stack(1);
bool loop = true;
YZ.set();
XZ.set();
XY.set();
V.reset();
srand(time(0));
pos.x = rand_int(Size);
pos.y = rand_int(Size);
pos.z = rand_int(Size);
V[pos.id] = 1;
stack.back().x = pos.x;
stack.back().y = pos.y;
stack.back().z = pos.z;
stack.resize(stack.size()+1);
while (!stack.empty()) {
for (int choice = rand_int(6), loop = true;
loop; choice = rand_int(6)) {
stack.back().dir[choice] = 1;
switch(choice) {
case 0:
pos.x++;
if (V[pos.id] == 1) pos.x--;
else {
loop = false;
pos.x--;
YZ[pos.id] = 0;
pos.x++;
}
break;
case 1:
pos.y++;
if (V[pos.id] == 1) pos.y--;
else {
loop = false;
pos.y--;
XZ[pos.id] = 0;
pos.y++;
}
break;
case 2:
pos.z++;
if (V[pos.id] == 1) pos.z--;
else {
loop = false;
pos.z--;
XY[pos.id] = 0;
pos.z++;
}
break;
case 3:
pos.x--;
if (V[pos.id] == 1) pos.x++;
else {
loop = false;
YZ[pos.id] = 0;
}
break;
case 4:
pos.y--;
if (V[pos.id] == 1) pos.y++;
else {
loop = false;
XZ[pos.id] = 0;
}
break;
case 5:
pos.z--;
if (V[pos.id] == 1) pos.z++;
else {
loop = false;
XY[pos.id] = 0;
}
break;
}
if (!loop) {
V[pos.id] = 1;
stack.back().x = pos.x;
stack.back().y = pos.y;
stack.back().z = pos.z;
stack.resize(stack.size()+1);
}
else if (stack.back().dir[0] == 1 && stack.back().dir[1] == 1 &&
stack.back().dir[2] == 1 && stack.back().dir[3] == 1 &&
stack.back().dir[4] == 1 && stack.back().dir[5] == 1) {
loop = false;
stack.pop_back();
pos.x = stack.back().x;
pos.y = stack.back().y;
pos.z = stack.back().z;
}
}
//// temp progress overview ///////////
/**/ system("cls"); /**/
/**/ std::cout << YZ << std::endl; /**/
/**/ std::cout << XZ << std::endl; /**/
/**/ std::cout << XY << std::endl; /**/
/**/ std::cout << V; /**/
///////////////////////////////////////
}
}

int main() {
while (true)
new_grid();
}
@code end


这篇关于一些改变的工作代码......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆