广搜全WA,谁能救救孩子啊......
查看原帖
广搜全WA,谁能救救孩子啊......
807181
PoorGhost楼主2023/4/15 13:52
#include <iostream>
#include <string>
using namespace std;
int s[1010][1010] = { 0 }, book[1010][1010] = { 0 };
int n, xx0, yy0, x2, y2;
struct point
{
	int a;
	int b;
	int id;
}q[1000005];
void bfs(int x,int y)
{
	int tx = 0, ty = 0;
	int next[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
	//向四个方向移动
	int head = 1, tail = 1;
	q[tail].a = x;
	q[tail].b = y;
	q[tail].id = 0;
	tail++;
	int flag = 0;
	while (head < tail)
	{
		for (int i = 0; i < 4; i++)
		{
			tx = q[head].a + next[i][0];
			ty = q[head].b + next[i][1];
			if (tx<1 || tx>n || ty<1 || ty>n)
				continue;
			if (book[tx][ty] == 0 && s[tx][ty] == 0)
			{
				book[tx][ty] = 1;
				q[tail].a = tx;
				q[tail].b = ty;
				q[tail].id = q[head].id + 1;
				tail++;
			}
			if (tx == x2 && ty == y2)
			{
				flag == 1;
				break;
			}
		}
		if (flag == 1)
			break;
		head++;
	}
	cout << q[tail - 1].id << endl;
	return;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		string str;
		cin >> str;
		for (int j = 1; j <= n; j++)
		{
			s[i][j] = str[j - 1] - '0';
		}
	}
	cin >> xx0 >> yy0 >> x2 >> y2;
	book[xx0][yy0] = 1;
	bfs(xx0,yy0);
	return 0;
}

2023/4/15 13:52
加载中...