有辦法可以改嗎?T_T
#include "MyMaze.h"
#include <iostream>
#include <cstdlib>
//#include <time.h>
const int MAXN = 1000;
int numRows, numColumns;
int maze [MAXN][MAXN], msolv[MAXN][MAXN];
int p[MAXN], rank[MAXN];
MyMaze::MyMaze(int h, int w)
{
if(h>w){numColumns = h; numRows = w;}else
{numColumns = w; numRows = h;}
int count = 0;
for(int i = 0; i<numRows*2+1; i++)
{
for(int j = 0; j<numColumns*2+1; j++)
{
if(i%2 == 0)
{
if(j%2 == 0)
{
maze[i][j] = -5;
}else
{
maze[i][j] = -6;
}
}else
{
if(j%2 == 0)
{
maze[i][j] = -7;
}else
{
maze[i][j] = count++;
}
}
}
}
maze[0][0] = -8;
maze[0][1] = -8;
maze[1][0] = -8;
maze[numRows*2-1][numColumns*2] = -8;
maze[numRows*2][numColumns*2-1] = -8;
maze[numRows*2][numColumns*2] = -8;
}
MyMaze::~MyMaze(void)
{
}
void MyMaze::MakeMaze(int seed)
{
int id = 0;
srand(seed);
for(int i = 0; i<numRows; i++)
{
for(int j = 0; j<numColumns; j++)
{
MakeSet(id);
id = id+1;
}
}
while(true)
{
int number = 0;
int xi = 0, xj = 0;
bool flag = true;
while(flag)
{
number = (int)(numRows*numColumns*4*rand() /(float)(RAND_MAX + 1));
xi = number/(numColumns*2+1);
xj = number - (number/(numColumns*2+1)*(numColumns*2+1));
if((maze[xi][xj] == -6 || maze[xi][xj] == -7) && xi != 0 && xi != (numRows*2) &&
xj != 0 && xj != (numColumns*2))
{
flag = false;
}
}
if(xi % 2 == 0 )
{
if(Find(maze[xi-1][xj]) != Find(maze[xi+1][xj]))
{
Union(maze[xi-1][xj], maze[xi+1][xj]);
maze[xi][xj] = -8;
}
}else
{
if(Find(maze[xi][xj-1]) != Find(maze[xi][xj+1]))
{
Union(maze[xi][xj-1], maze[xi][xj+1]);
maze[xi][xj] = -8;
}
}
if((Find(0)) == (Find(numRows*numColumns-1)))
{
for(int i = 0; i<numRows*2+1; i++)
{
for(int j = 0; j<numColumns*2+1;j++)
{
if(maze[i][j] != -8 && maze[i][j]<0)
{
msolv[i][j] = 1;
}else
{
msolv[i][j] = 0;
}
}
}
return;
}
}
}
void MyMaze::PrintMaze()
{
std::cout << std::endl;
for(int i = 0; i<numRows*2+1; i++)
{
for(int j = 0; j<numColumns*2+1; j++)
{
switch(maze[i][j])
{
case -5:
std::cout << "+";
break;
case -6:
std::cout << "-";
break;
case -7:
std::cout << "|";
break;
case -8:
std::cout << " ";
break;
case 0:
std::cout << "0";
break;
case -9:
std::cout << "H";
break;
default:
std::cout << " ";
}
}
std::cout << std::endl ;
}
}
int MyMaze::SolveMaze(int x, int y, int x_to, int y_to)
{
const int size =100;
if(x_to > y_to)
{
int temp = y_to;
y_to = x_to;
x_to = temp;
}
int matrix[size][size][3];
int step;
bool added=true,result=true;
for(int i=0;i<numRows*2+1;i++)
{
for(int j=0;j<numColumns*2+1;j++)
{
if(msolv[i][j]!=0)
{
matrix[i][j][0] = -2;
}
else
{
matrix[i][j][0] = -1;
}
}
}
matrix[x_to][y_to][0]= 0;
step = 0;
while(added && matrix[x][y][0]==-1)
{
added = false;
step++;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(matrix[i][j][0]== step-1)
{
int _i,_j;
_i=i+1;_j=j;
if(_i>=0 && _j>=0 && _i<size && _j<size)
{
if(matrix[_i][_j][0]==-1 && matrix[_i][_j][0]!=-2)
{
matrix[_i][_j][0]= step;
matrix[_i][_j][1]= i;
matrix[_i][_j][2]= j;
added = true;
}
}
_i=i-1;_j=j;
if(_i>=0 && _j>=0 && _i<size && _j<size)
{
if(matrix[_i][_j][0]==-1 && matrix[_i][_j][0]!=-2)
{
matrix[_i][_j][0]= step;
matrix[_i][_j][1]= i;
matrix[_i][_j][2]= j;
added = true;
}
}
_i=i;_j=j+1;
if(_i>=0 && _j>=0 && _i<size && _j<size)
{
if(matrix[_i][_j][0]==-1 && matrix[_i][_j][0]!=-2)
{
matrix[_i][_j][0]= step;
matrix[_i][_j][1]= i;
matrix[_i][_j][2]= j;
added = true;
}
}
_i=i;_j=j-1;
if(_i>=0 && _j>=0 && _i<size && _j<size)
{
if(matrix[_i][_j][0]==-1 && matrix[_i][_j][0]!=-2)
{
matrix[_i][_j][0]= step;
matrix[_i][_j][1]= i;
matrix[_i][_j][2]= j;
added = true;
}
}
}
}
}
}
if(matrix[x][y][0]== -1)
{
result = false;
}
if(result)
{
int _i=x,_j=y;
while(matrix[_i][_j][0]!=0)
{
maze[_i][_j] = 0;
int li = matrix[_i][_j][1];
int lj = matrix[_i][_j][2];
_i=li;_j=lj;
}
}
maze[numRows*2-1][numColumns*2-1] = 0;
return 0;
}
void MyMaze::MakeSet(int x)
{
p[x] = x;
rank[x] = 0;
}
int MyMaze::Find(int x)
{
if (p[x] == x) return x;
return p[x] = Find(p[x]);
}
void MyMaze::Union(int x, int y)
{
x = Find(x);
y = Find(y);
if (rank[x] < rank[y])
p[x] = y;
else
{
p[y] = x;
if (rank[x] == rank[y])
++rank[x];
}
}
4 个解决方案
#1
整形溢出会报错?是不是除0了??
#2
number = (int)(numRows*numColumns*4*rand() /(float)(RAND_MAX + 1));
修改如下:
number=int(numRows/double(RAND_MAX+1)*numColumns*4*rand());
修改如下:
number=int(numRows/double(RAND_MAX+1)*numColumns*4*rand());
#3
感謝樓上大大回復!
我可以另外問其他錯誤訊息嗎?
error message:
MyMaze.cpp: In member function 'void MyMaze::MakeMaze(int)':
MyMaze.cpp:77:69: warning: integer overflow in expression [-Woverflow]
MyMaze.h:1:9: warning: #pragma once in main file [enabled by default]
下面是我的header file 和Main.cpp
我可以另外問其他錯誤訊息嗎?
error message:
MyMaze.cpp: In member function 'void MyMaze::MakeMaze(int)':
MyMaze.cpp:77:69: warning: integer overflow in expression [-Woverflow]
MyMaze.h:1:9: warning: #pragma once in main file [enabled by default]
下面是我的header file 和Main.cpp
//MyMaze.h
#pragma once
#ifndef MAZE_H
#define MAZE_H
#define FALSE 0
#define TRUE 1
class MyMaze
{
public:
MyMaze(int h, int w);
~MyMaze(void);
void MakeMaze(int seed);
void PrintMaze();
int SolveMaze(int x, int y, int x_to, int y_to);
private:
//disjoint set methods
void MakeSet(int x);
int Find(int x);
void Union(int x, int y);
};
#endif
//Main.cpp
#include <iostream>
//#include"Maze.h"
#include <stdlib.h>
#include "MyMaze.h"
using namespace std;
int main(int argc, char* argv[])
{
if(argc < 4)
{
cout << "Usage: Maze <height> <width> <seed>" << endl;
}
int width, height, seed;
height = atoi(argv[1]);
width = atoi(argv[2]);
seed = atoi(argv[3]);
MyMaze theMaze(height,width);
//build the maze
theMaze.MakeMaze(seed);
//print it to cout
theMaze.PrintMaze();
//find the solution and print out the successful (shortest) path and maze with solution
theMaze.SolveMaze(1, 1, width*2-1, height*2-1 );
theMaze.PrintMaze();
cin.get();
return 0;
}
#4
可以考虑先除一下,再乘
#1
整形溢出会报错?是不是除0了??
#2
number = (int)(numRows*numColumns*4*rand() /(float)(RAND_MAX + 1));
修改如下:
number=int(numRows/double(RAND_MAX+1)*numColumns*4*rand());
修改如下:
number=int(numRows/double(RAND_MAX+1)*numColumns*4*rand());
#3
感謝樓上大大回復!
我可以另外問其他錯誤訊息嗎?
error message:
MyMaze.cpp: In member function 'void MyMaze::MakeMaze(int)':
MyMaze.cpp:77:69: warning: integer overflow in expression [-Woverflow]
MyMaze.h:1:9: warning: #pragma once in main file [enabled by default]
下面是我的header file 和Main.cpp
我可以另外問其他錯誤訊息嗎?
error message:
MyMaze.cpp: In member function 'void MyMaze::MakeMaze(int)':
MyMaze.cpp:77:69: warning: integer overflow in expression [-Woverflow]
MyMaze.h:1:9: warning: #pragma once in main file [enabled by default]
下面是我的header file 和Main.cpp
//MyMaze.h
#pragma once
#ifndef MAZE_H
#define MAZE_H
#define FALSE 0
#define TRUE 1
class MyMaze
{
public:
MyMaze(int h, int w);
~MyMaze(void);
void MakeMaze(int seed);
void PrintMaze();
int SolveMaze(int x, int y, int x_to, int y_to);
private:
//disjoint set methods
void MakeSet(int x);
int Find(int x);
void Union(int x, int y);
};
#endif
//Main.cpp
#include <iostream>
//#include"Maze.h"
#include <stdlib.h>
#include "MyMaze.h"
using namespace std;
int main(int argc, char* argv[])
{
if(argc < 4)
{
cout << "Usage: Maze <height> <width> <seed>" << endl;
}
int width, height, seed;
height = atoi(argv[1]);
width = atoi(argv[2]);
seed = atoi(argv[3]);
MyMaze theMaze(height,width);
//build the maze
theMaze.MakeMaze(seed);
//print it to cout
theMaze.PrintMaze();
//find the solution and print out the successful (shortest) path and maze with solution
theMaze.SolveMaze(1, 1, width*2-1, height*2-1 );
theMaze.PrintMaze();
cin.get();
return 0;
}
#4
可以考虑先除一下,再乘