0
点赞
收藏
分享

微信扫一扫

离开中山路(深搜or宽搜)

吓死我了_1799 2022-01-21 阅读 100
c++算法


原题连接:离开中山路

非常的简单,我是用宽搜写的

#include<bits/stdc++.h>
#define N 1005
using namespace std;
int n;
char a[N][N];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int vis[N][N];
bool f[N][N];
queue <pair <int, int> > Q;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		string asdf;
		cin >> asdf;
		for (int j = 0; j < n; j++)
			a[i + 1][j + 1] = asdf[j];		
	}
	int x, y, x1, y1;
	cin >> x >> y >> x1 >> y1;
	f[x][y] = true;
	Q.push (make_pair (x, y));
	while (Q.size ()) {
		int xx = Q.front ().first;
		int yy = Q.front ().second;
		Q.pop ();
		for (int i = 0; i < 4; i++) {
			int tx = xx + dx[i];
			int ty = yy + dy[i];
			if (f[tx][ty] || tx < 1 || ty < 1 || tx > n || ty > n || a[tx][ty] == '1') continue;
			f[tx][ty] = true;
			vis[tx][ty] = vis[xx][yy] + 1;
			Q.push (make_pair (tx, ty));
		}
	}
	cout << vis[x1][y1] << endl;
	return 0;
}
举报

相关推荐

0 条评论