Language: C++


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,sum,ex,ey;
char map[1010][1010];
int vis[1010][1010];
int d[4][2]={1,0,0,1,-1,0,0,-1};
bool judge(int x,int y)
{
	if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]!='#')
	return true;
	return false;
}
struct node
{
	int x,y,step;
}now,next;
void bfs(int sx,int sy)
{
	queue<node>q;
	memset(vis,0,sizeof(vis));
	vis[sx][sy]=1;
	now.x=sx;
	now.y=sy;
	now.step=0;
	sum=0;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			next.x=now.x+d[i][0];
			next.y=now.y+d[i][1];
			if(judge(next.x,next.y))
			{
			next.step=now.step+1;
				if(vis[next.x][next.y]==0)
				{

					vis[next.x][next.y]=1;
					if(sum<next.step)
					{
						sum=next.step;
						ex=next.x;//记录中间端点值
						ey=next.y;
					}
					q.push(next);
				}
			}
		}
	}

}
using namespace std;
int main()
{
	int t,sx,sy;

		scanf("%d%d",&m,&n);
		for(int i=0;i<m;i++)
		scanf("%s",&map[i]);
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(map[i][j]=='.')
				{
				sx=i;
				sy=j;
				break;
				}
			}
		}
		bfs(sx,sy);
		bfs(ex,ey);
		printf("%d\n", sum);

	return 0;
}


Compiler output:
a.cc: In function 'void bfs(int, int)':
a.cc:37:25: error: reference to 'next' is ambiguous
   37 |                         next.x=now.x+d[i][0];
      |                         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:38:25: error: reference to 'next' is ambiguous
   38 |                         next.y=now.y+d[i][1];
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:39:34: error: reference to 'next' is ambiguous
   39 |                         if(judge(next.x,next.y))
      |                                  ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:39:41: error: reference to 'next' is ambiguous
   39 |                         if(judge(next.x,next.y))
      |                                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:41:25: error: reference to 'next' is ambiguous
   41 |                         next.step=now.step+1;
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:42:40: error: reference to 'next' is ambiguous
   42 |                                 if(vis[next.x][next.y]==0)
      |                                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:42:48: error: reference to 'next' is ambiguous
   42 |                                 if(vis[next.x][next.y]==0)
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:45:45: error: reference to 'next' is ambiguous
   45 |                                         vis[next.x][next.y]=1;
      |                                             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:45:53: error: reference to 'next' is ambiguous
   45 |                                         vis[next.x][next.y]=1;
      |                                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:46:48: error: reference to 'next' is ambiguous
   46 |                                         if(sum<next.step)
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:48:53: error: reference to 'next' is ambiguous
   48 |                                                 sum=next.step;
      |                                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:49:52: error: reference to 'next' is ambiguous
   49 |                                                 ex=next.x;//记录中间端点值
      |                                                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:50:52: error: reference to 'next' is ambiguous
   50 |                                                 ey=next.y;
      |                                                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~
a.cc:52:48: error: reference to 'next' is ambiguous
   52 |                                         q.push(next);
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:20:6: note:                 'node next'
   20 | }now,next;
      |      ^~~~