关于这个代码
  • 板块P3395 路障
  • 楼主TODAYS
  • 当前回复7
  • 已保存回复7
  • 发布时间2025/1/1 19:25
  • 上次更新2025/1/2 11:57:29
查看原帖
关于这个代码
1295871
TODAYS楼主2025/1/1 19:25

这是我的代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1005;
bool vis[maxn][maxn];
int tim[maxn][maxn];
int sx = 1, sy = 1, ex, ey, n;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

struct node
{
	int x, y, cnt;
};
queue <node> q;

void bfs()
{
	q.push({sx, sy, 0});
	vis[sx][sy] = 1;
	while (!q.empty())
	{
		node H = q.front();
		q.pop();
		if (H.x == ex && H.y == ey)
		{
			cout << "Yes\n";
			return ;
		}
		for (int i = 0; i <= 3; i++)
		{
			int nx = H.x + dx[i], ny = H.y + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > n) continue;
			if (vis[nx][ny]) continue;
			if (tim[nx][ny] <= H.cnt + 1) continue;
			q.push({nx, ny, H.cnt + 1});
			vis[nx][ny] = 1;
		}
	}
	cout << "No\n";
	return ;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		memset(vis, 0, sizeof(vis));
		memset(tim, 1000000000, sizeof(tim));
		cin >> n;
		ex = n, ey = n;
		for (int i = 1; i <= 2 * n - 2; i++)
		{
			int x, y;
			cin >> x >> y;
			tim[x][y] = min(tim[x][y], i + 1);
		}
		bfs();
	}
	return 0;
}

这是老师的代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1005;
bool vis[maxn][maxn];
int tim[maxn][maxn];
int sx = 1, sy = 1, ex, ey, n;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

struct node
{
	int x, y, cnt;
};
queue <node> q;

void bfs()
{
	q.push({sx, sy, 0});
	vis[sx][sy] = 1;
	while (!q.empty())
	{
		node H = q.front();
		q.pop();
		if (H.x == ex && H.y == ey)
		{
			cout << "Yes\n";
			return ;
		}
		for (int i = 0; i <= 3; i++)
		{
			int nx = H.x + dx[i], ny = H.y + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > n) continue;
			if (vis[nx][ny]) continue;
			if (tim[nx][ny] <= H.cnt + 1) continue;
			q.push({nx, ny, H.cnt + 1});
			vis[nx][ny] = 1;
		}
	}
	cout << "No\n";
	return ;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		memset(vis, 0, sizeof(vis));
		memset(tim, 0x3f, sizeof(tim)); // 就这不一样
		cin >> n;
		ex = n, ey = n;
		for (int i = 1; i <= 2 * n - 2; i++)
		{
			int x, y;
			cin >> x >> y;
			tim[x][y] = min(tim[x][y], i + 1);
		}
		bfs();
	}
	return 0;
}

我清空timtim数组

memset(tim, 1000000000, sizeof(tim));

老师清空timtim数组

memset(tim, 0x3f, sizeof(tim));

我和老师就memset这不一样,我的代码全WA,老师代码AC,这是怎么回事?蒟蒻不懂。

2025/1/1 19:25
加载中...