#include<cstdio>
#include<iostream>
using namespace std;
const int MAXN = 100;
int G[MAXN][MAXN];
int m, n;
int ex, ey;
bool DFS(int sx, int sy){
if(ex==sx && ey==sy)
return true;
int deltax[] = {0,0,1,-1};
int deltay[] = {1,-1,0,0};
for(int i=0; i<4; i++){
int newx = sx+deltax[i];
int newy = sy+deltay[i];
if(newx>=0 && newx<m && newy>=0 && newy<n && G[newx][newy]==1){
G[newx][newy] = 2;
if(true == DFS(newx, newy))
return true;
}
}
return false;
}
int main(){
int sx, sy;
cout<<"迷宫阶数:\nm=";
cin>>m;
cout<<"n=";
cin>>n;
cout<<"输入迷宫:\n";
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>G[i][j];
cout<<"输入起点:\nsx=";
cin>>sx;
cout<<"sy=";
cin>>sy;
cout<<"输入终点:\nex=";
cin>>ex;
cout<<"ey=";
cin>>ey;
if(G[sx][sy] == 0)
cout<<"No"<<endl;
else
cout<<DFS(sx,sy)<<endl;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++)
cout<<G[i][j]<<" ";
cout<<endl;
}
return 0;
}
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
struct node{
int x, y;
node(int x_, int y_):x(x_),y(y_){};
node(){};
};
int main(){
const int MAXN = 100;
int G[MAXN][MAXN];
int m, n;
node ns;
node ne;
cout<<"输入迷宫阶数:";
cin>>m>>n;
cout<<"输入迷宫:\n";
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>G[i][j];
cout<<"输入起点:";
cin>>ns.x>>ns.y;
cout<<"输入终点:";
cin>>ne.x>>ne.y;
if(G[ns.x][ns.y] == 0){
cout<<0;
return 0;
}
if(ns.x==ne.x && ns.y==ne.y){
cout<<1;
return 0;
}
queue<node> Q;
Q.push(ns);
G[ns.x][ns.y] = 2;
while(!Q.empty()){
node ntop = Q.front();
Q.pop();
int deltax[] = {0,0,1,-1};
int deltay[] = {1,-1,0,0};
for(int i=0; i<4; i++){
int newx = ntop.x+deltax[i];
int newy = ntop.y+deltay[i];
if(newx==ne.x && newy==ne.y && G[newx][newy]==1){
cout<<1<<endl;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++)
cout<<G[i][j]<<" ";
cout<<endl;
}
return 0;
}
if(newx>=0 && newx<m && newy>=0 && newy<n && G[newx][newy]==1){
G[newx][newy] = 2;
Q.push(node(newx,newy));
}
}
}
cout<<0<<endl;
return 0;
}