BFS,AC了,但是我的dfs过不了QWQ
AC代码:
#include <iostream>
#include <queue>
using namespace std;
struct Node
{
int x, y;
int step=0; //当前步数
};
int n, m;
int a, b, c, d;
int minstep = 0x3f3f3f3f;
int g[102][102] = { 0 };
int book[102][102] = { 0 };
int mx[4] = { 0,0,1,-1 };
int my[4] = { 1,-1,0,0 };
void bfs()
{
queue<Node>q;
q.push({ a,b,0 });
while (!q.empty())
{
Node cur = q.front();
q.pop();
if (cur.x == c && cur.y == d)
minstep = min(minstep, cur.step);
for (int i = 0; i < 4; i++)
{
Node nex;
nex.x = cur.x + mx[i];
nex.y = cur.y + my[i];
nex.step = cur.step + 1;
if (g[nex.x][nex.y] == 0 || book[nex.x][nex.y] == 1) continue;
q.push(nex);
book[nex.x][nex.y] = 1;
}
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
cin >> a >> b >> c >> d;
bfs();
if (minstep!=0x3f3f3f3f) cout << minstep;
else cout << -1;
return 0;
}