求调MLE了,感谢帮忙我一定会回关
查看原帖
求调MLE了,感谢帮忙我一定会回关
1482388
sweet_520楼主2025/1/5 20:32
// 8.problem  BFS
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,sx,sy,fx,fy;
char a[1005][1005];
int dx[4] = {1,0,-1,0};//右,下,左,上
int dy[4] = {0,1,0,-1};
//string h[1005];
bool vis[1005][1005];
struct s {
	int x;
	int y;
	int len;
};
queue <s>  que;
int main()
{
	cin >> n;
	//for (int i = 1; i <= n; i++)
	//	cin >> h[i];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];
			//a[i][j] = h[i][j - 1]-'0';//装换的时候一定要字符装换
	cin >> sx >> sy >> fx >> fy;
	s s1;
	s1.x = sx, s1.y = sy, s1.len = 0;
	que.push(s1);//将首节点入队列
	vis[sx][sy] = 1;
	while (!que.empty())
	{
		auto temp = que.front();
	    que.pop();//将队首元素出队
		if (temp.x == fx && temp.y == fy)
		{
			cout << temp.len;
			break;
		}
		for (int i = 0; i < 4; i++)
		{
			int tx = temp.x + dx[i], ty = temp.y + dy[i];
			if (tx > 0 && ty > 0 && tx <= n && ty <= n && a[tx][ty] == '0' && !vis[tx][ty])
			{
				s temp2;
				temp2.x = tx, temp2.y = ty, temp2.len=temp.len+1;
				que.push(temp2);
			}
		}
	}
	return 0;
}
2025/1/5 20:32
加载中...