超垃圾代码
花了4个多小时才编出一个bfs解迷宫的程序,真是衰到了,到底是实力问题~ 下面总结一下各垃圾点。
首先,是队列的实现。就因为==写成=耽误了1个小时,差点想放弃。以后再也不自己做队列了。
第2,是用1~4代表上下左右,即(1,0) (0,1) (-1,0) (0,-1),是用switch吗?偏不,咱得想个转换公式呀,那样多丑。又盯着屏幕1个多小时... 诞生了getdxy()。
第3,设计问题,既然maze占了100b,队列又占了400b,咱还有脸再开一个数组存放路径么?所以咱把路径信息还放到maze里,到时候从终点往回推就行了。这么难,又耽误1个多小时。
第4,最后调试的时候路径明明标对了,可是程序就是不知道拐弯。最后删了几行调试代码它就会拐弯了~
所以饿决定:一天只编一个程序!!
#include <cstdlib>
#include <iostream>
using namespace std;
enum {WAY, WALL = 5};
const int side = 10;
char maze[side][side];
const int qlen = side * side + 1;
int queue[qlen];
int head, tail;
void init_maze ()
{
for (int i = 0; i < side; ++i)
maze[5][i] = WALL;
maze[5][5] = WAY;
}
bool addq (int elem)
{
int t = tail + 1;
if (t >= qlen)
t = 0;
if (t == head)
return false; // if queue full
queue[tail] = elem;
tail = t;
return true;
}
bool outq (int &elem)
{
if (head == tail)
return false; // if empty
elem = queue[head++];
if (head >= qlen)
head = 0;
return true;
}
// 1
// 2 3
// 4
inline void getdxy (int dir, int &dx, int &dy)
{
dx = dir/2 - 1;
dy = dir%2 - (dir <= 2);
}
int showresult ()
{
int x = side - 1, y = side - 1;
int dx, dy;
int n = 0;
while (x != 0 || y != 0) {
getdxy ((int)maze[x][y], dx, dy);
maze[x][y] = '*';
x += dx;
y += dy;
n++;
}
for (int i = 0; i < side; ++i) {
for (int j = 0; j < side; ++j) {
int m = maze[i][j];
if (m == '*')
cout << '*';
else if (m == WALL)
cout << '#';
else
cout << '0';
cout << ' ';
}
cout << '\n';
}
return n;
}
int runmaze ()
// return routin lenth
{
init_maze ();
addq (0);
maze[0][0] = '*';
int pos;
while (outq (pos) == true) {
int x = pos/side, y = pos%side;
for (int i = 1; i <= 4; ++i) {
int dx, dy;
getdxy (i, dx, dy);
int x2 = x + dx;
int y2 = y + dy;
if (x2 < side && x2 >= 0 && y2 < side && y2 >= 0 &&
maze[x2][y2] == WAY) {
maze[x2][y2] = 5 - i; // un-direction
if (x2 == side-1 && y2 == side - 1)
return showresult ();
if (addq (x2*side + y2) == false)
return -1;
}
}
}
}
int main(int argc, char *argv[])
{
runmaze ();
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
enum {WAY, WALL = 5};
const int side = 10;
char maze[side][side];
const int qlen = side * side + 1;
int queue[qlen];
int head, tail;
void init_maze ()
{
for (int i = 0; i < side; ++i)
maze[5][i] = WALL;
maze[5][5] = WAY;
}
bool addq (int elem)
{
int t = tail + 1;
if (t >= qlen)
t = 0;
if (t == head)
return false; // if queue full
queue[tail] = elem;
tail = t;
return true;
}
bool outq (int &elem)
{
if (head == tail)
return false; // if empty
elem = queue[head++];
if (head >= qlen)
head = 0;
return true;
}
// 1
// 2 3
// 4
inline void getdxy (int dir, int &dx, int &dy)
{
dx = dir/2 - 1;
dy = dir%2 - (dir <= 2);
}
int showresult ()
{
int x = side - 1, y = side - 1;
int dx, dy;
int n = 0;
while (x != 0 || y != 0) {
getdxy ((int)maze[x][y], dx, dy);
maze[x][y] = '*';
x += dx;
y += dy;
n++;
}
for (int i = 0; i < side; ++i) {
for (int j = 0; j < side; ++j) {
int m = maze[i][j];
if (m == '*')
cout << '*';
else if (m == WALL)
cout << '#';
else
cout << '0';
cout << ' ';
}
cout << '\n';
}
return n;
}
int runmaze ()
// return routin lenth
{
init_maze ();
addq (0);
maze[0][0] = '*';
int pos;
while (outq (pos) == true) {
int x = pos/side, y = pos%side;
for (int i = 1; i <= 4; ++i) {
int dx, dy;
getdxy (i, dx, dy);
int x2 = x + dx;
int y2 = y + dy;
if (x2 < side && x2 >= 0 && y2 < side && y2 >= 0 &&
maze[x2][y2] == WAY) {
maze[x2][y2] = 5 - i; // un-direction
if (x2 == side-1 && y2 == side - 1)
return showresult ();
if (addq (x2*side + y2) == false)
return -1;
}
}
}
}
int main(int argc, char *argv[])
{
runmaze ();
system("PAUSE");
return EXIT_SUCCESS;
}