64,676
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
#define M 5
#define N 4
int a[M][N] = {0,1,0,1,0,1,1,0,1,1,0,0,1,0,1,0,1,0,1,0};
int b[M] = {};
void genLine(int n)
{
if (n == M)
{
for (int i = 0; i < M; i++)
{
cout<<b[i]<<' ';
}
cout<<endl;
}
if (n==0)
{
for (int j = 0; j < N; j++)
{
if (a[0][j] == 1)
{
b[0] = j;
genLine(n+1);
}
}
}
if (n > 0)
{
int p = b[n-1];
if (b[n-1] > 0 && a[n][b[n-1]-1] == 1){
b[n] = b[n-1]-1;
genLine(n+1);
}
b[n-1] = p;
if (a[n][b[n-1]] == 1){
b[n] = b[n-1];
genLine(n+1);
}
b[n-1] = p;
if (b[n-1] < N && a[n][b[n-1]+1] == 1){
b[n] = b[n-1]+1;
genLine(n+1);
}
}
}
int main (int argc,char** args)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"----------------------------------"<<endl;
genLine(0);
return 0;
}
/**
* @Title 老鼠走迷宫的拓展探究
* @Author 孙琨
* @Date 2013-11-16
* @At XUST
* @All Copyright by 孙琨
*
*/
#include <iostream>
using namespace std;
int maze[9][9] = { // 初始化迷宫,英文maze为“迷宫”
{2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,2},
{2,0,2,2,0,2,2,0,2},
{2,0,2,0,0,2,0,0,2},
{2,0,2,0,2,0,2,0,2},
{2,0,0,0,0,0,2,0,2},
{2,2,0,2,2,0,2,2,2},
{2,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2}
};
int startI = 1,startJ = 1; // 入口行列坐标
int endI = 7,endJ = 7; // 出口行列坐标
void visit(int i,int j) // 自动搜寻路径
{
int m,n;
maze[i][j] = 1;
if((i == endI) && (j == endJ))
{
cout << endl << "显示路径:" << endl;
for(m=0; m<9; m++)
{
for(n=0; n<9; n++)
{
if(maze[m][n] == 2)
cout << "■";
else if(maze[m][n] == 1)
cout << "♀";
else
cout << " ";
}
cout << endl;
}
}
if(maze[i][j+1] == 0)
visit(i,j+1);
if(maze[i+1][j] == 0)
visit(i+1,j);
if(maze[i][j-1] == 0)
visit(i,j-1);
if(maze[i-1][j] == 0)
visit(i-1,j);
maze[i][j] = 0;
}
int main(void)
{
int i,j;
cout << "显示迷宫: " << endl;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if(maze[i][j] == 2)
cout << "■" ;
else
cout << " " ;
}
cout << endl;
}
visit(startI,startJ);
return 0;
}