广搜全红求助
查看原帖
广搜全红求助
723993
__ryp__楼主2023/3/25 22:03
#include <iostream>
#include <queue>
using namespace std;

char map[1001][1001];
static int directions[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { 1, -1 } };
int n;

int sx, sy;
int ex, ey;

struct node { int x, y, steps; };
static inline bool
bound_check (node p)
{
  return (p.x >= 0 && p.x < n) && (p.y >= 0 && p.y < n);
}

int
bfs (void)
{
  queue<node> q;
  node p, next;

  q.push ({ sx, sy, 0 });
  while (!q.empty ())
    {
      p = q.front ();
      q.pop ();

      if (p.x == ex && p.y == ey)
	return p.steps;

      for (auto dir : directions)
	{
	  next.x = dir[0] + p.x;
	  next.y = dir[1] + p.y;
	  next.steps = p.steps + 1;

	  if (bound_check (next) && map[next.x][next.y] == '0')
	    {
	      map[next.x][next.y] = '#';
	      q.push (next);
	    }
	}
    }

  return 1;
}

int
main (void)
{
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf ("%s", map[i]);

  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  cout << bfs () << endl;
  
  return 0;
}

简明 简单的思路,本地样例过,下了第一个红点测试也过,跑测评就寄 (Too short on line 1. )

求助各位佬、

2023/3/25 22:03
加载中...