题解 | #迷宫问题#
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
#include <iostream>
#include <vector>
using namespace std;
bool flag;
void dfs(vector<vector<int>>&MAP,vector<pair<int,int>>&points,int i,int j,int n,int m)
{
if(flag)
return;
if(i>=n||j>=m||i<0||j<0)
return;
if(MAP[i][j]==1)
return;
pair<int,int> temp;
temp.first=i;
temp.second=j;
points.push_back(temp);
MAP[i][j] = 1;
if(i == n-1&&j == m-1)
{
for(auto t:points)
{
cout<<'('<<t.first<<','<<t.second<<')'<<endl;
}
flag = true;
return;
}
dfs(MAP,points,i+1,j,n,m);
dfs(MAP,points,i,j+1,n,m);
dfs(MAP,points,i-1,j,n,m);
dfs(MAP,points,i,j-1,n,m);
points.pop_back();
}
int main() {
int n,m;
cin>>n>>m;
vector<vector<int>> MAP;
for(int i = 0; i < n; i++)
{
vector<int>temp(m);
MAP.push_back(temp);
}
for(int i = 0; i<n;i++)
for(int t = 0; t < m;t++)
{
cin>>MAP[i][t];
}
flag = false;
vector<pair<int,int>> path;
dfs(MAP,path,0,0,n,m);
}
// 64 位输出请用 printf("%lld")

