代碼如下:
#include<iostream>
using namespace std;
int main()
{
//矩陣初始化
int **migong;
int h, w;
int m, n;
cout << "請輸入迷宮高度h和迷宮寬度w:" << endl;
cin >> h >> w;
migong = new int *[h];
for (int i = 0; i < h ; i++)
{
migong[i] = new int[w];
}
for (m = 0; m < h ; m++)
{
for (n = 0; n < w ; n++)
{
cin>>migong[m][n] ;
}
}
//輸出矩陣
for (m = 0; m < h; m++)
{
for (n = 0; n < w; n++)
{
cout << migong[m][n] << " ";
}
cout << endl;
}
int temp = 0;
if (h >= w)
temp = h;
else
temp = w;
int *path_row = new int;//通行路徑的行
int *path_column = new int;//通行路徑的列
for (int i = 0; i < temp; i++)
path_row[i] = path_column[i] = 0;
int row = 0;
int column = 0;
int count = 0;
do
{
column = 0;
do
{
switch (migong[row][column])
{
case 3:
cout << "入口坐標為:" << "(" << row << "," << column << ")" << endl;
}
switch (migong[row][column])
{
case 0:
path_row[count] = row;
path_column[count] = column;
cout << "(" << row << "," << column << ")" << endl;
count++;
break;
case 1:
break;
}
switch (migong[row][column])
{
case 4:
cout << "出口坐標為:" << "(" << row << "," << column << ")" << endl;
}
column += 1;
} while (column < w);
row += 1;
}while (row < h);
for (m = 0; m < h ; m++)
{
delete[] migong[m];
}
delete migong;
delete path_row;
delete path_column;
system("pause");
return 0;
}
麻煩各位大神看看是哪兒出錯了
如果結果正確卻丟出異常,顯然是delete的問題。