有一个 m×n 格的迷宫(表示有 m 行、n列),其中有可走的也有不可走的,如果用 1表示可以走,0 表示不可以走,文件读入这 m×n个数据和起始点、结束点。该迷宫由0和1组成,如果一条路都不可行,则输出相应信息(用 −1 表示无路)。
优先顺序:左上右下。数据保证随机生成。
第一行是两个数 m,n(1 所有可行的路径,描述一个点时用 (x,y)(x,y)(x,y) 的形式,除开始点外,其他的都要用 如果没有一条可行的路则输出 −1-1−1。 输入 #1 输出 #1 代码 #include { if(x1+Hang[i]>0&&x1+Hang[i]<=m)&&(y1+Lie[i]>0&&y1+Lie[i]<=n)输出格式
->
表示方向。输入输出样例
5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
#include
#include
#include
#include
using namespace std;
int A[20][20],B[20][20];
int K[500][2];
int n,m,x2,y2,j=0,y=0;
void hanshu(int x1,int y1)
{
B[x1][y1]=1;
int i;
int Hang[4]={0,-1,0,1};
int Lie[4]={-1,0,1,0};
if(x1==x2&&y1==y2)
{
for(i=0;i
printf("(%d,%d)\n",x2,y2);
y=1;
return;
}
for(i=0;i<4;i++)
{
if(A[x1+Hang[i]][y1+Lie[i]]==1&&B[x1+Hang[i]][y1+Lie[i]]==0)
{
K[j][0]=x1;K[j][1]=y1;
B[x1+Hang[i]][y1+Lie[i]]=1;
j++;
hanshu(x1+Hang[i],y1+Lie[i]);
B[x1+Hang[i]][y1+Lie[i]]=0;
j--;
}
}
}
return;
}
int main()
{
int i,j,x1,y1;
cin>>m>>n;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cin>>A[i][j];
}
}
cin>>x1>>y1;
cin>>x2>>y2;
hanshu(x1,y1);
if(y==0)
printf("-1");
return 0;
}