听灌多
  • 板块灌水区
  • 楼主AIcat_136
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/7/28 17:07
  • 上次更新2024/7/28 17:17:36
查看原帖
听灌多
1285350
AIcat_136楼主2024/7/28 17:07

50pts测评记录

#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, fx, fy, ans;
char a[5005][5005]; 
bool vis[5005][5005]; 
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct node
{
	int x, y, step;
};
int bfs(int x, int y) 
{
    queue<node> q;
    q.push(node{x, y, 0});
	vis[x][y] = 1;
    while(!q.empty())
    {
    	node t;
    	t = q.front();
    	q.pop();
    	if(t.x == fx && t.y == fy)
			return t.step;
    	int tx, ty;
    	for(int i = 0; i < 3; i++)
    	{
    		tx = t.x + dx[i];
    		ty = t.y + dy[i];
    		if(tx >= 1 && tx <= n && ty >= 1 && ty <= n)
    		{
    			if(vis[tx][ty] == 0 && a[tx][ty] == '0')
    			{
    				q.push(node{tx, ty, t.step + 1});
					vis[tx][ty] = 1;
				}
			}
		}
	}
	return 2147483647;
}
int main() 
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
    	for(int j = 1; j <= n; j++)
    	{
    		cin >> a[i][j];
		}
	}
	cin >> sx >> sy >> fx >> fy;
	vis[sx][sy] = 1;
	ans = bfs(sx, sy);
	cout << ans; 
	return 0;
}

思路为正常bfs思路

2024/7/28 17:07
加载中...