c++35分求助
查看原帖
c++35分求助
1065928
XiaoYuyu楼主2023/12/22 17:01
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<cstring>
using namespace std;
int m;
int vis[400][400];
int dis[400][400];
int xx[] = { 0,1,0,-1 };
int yy[] = { 1,0,-1,0 };

typedef pair<int, int>PPI;
PPI star_loc[50005];
int mark = 0;
queue<PPI>q;
int tstar[400][400];
int finalx = 0, finaly = 0;
void spread()
{
	for (int i = 1; i <= m; i++)
	{
		auto hot = star_loc[i];
		int time = tstar[hot.first][hot.second];
		for (int j = 0; j <= 3; j++)
		{
			int hotx = hot.first + xx[j]; int hoty = hot.second + yy[j];
			if ( hotx >= 0 && hoty >= 0 && hotx < 301 && hoty < 301)
			{
				tstar[hotx][hoty] = min(time,tstar[hotx][hoty]);
			}
		}
	}
}

void bfs()
{
	q.push({ 0,0 });
	while (!q.empty())
	{
		auto t = q.front();
		q.pop();
		for (int i = 0; i <= 3; i++)
		{
			int nowx = t.first + xx[i]; int nowy = t.second + yy[i];
			if (vis[nowx][nowy] == 0 && nowx >= 0 && nowy >= 0  && tstar[nowx][nowy]>dis[t.first][t.second] + 1)
			{
				vis[nowx][nowy] = 1;
				dis[nowx][nowy] = dis[t.first][t.second] + 1;
				if (tstar[nowx][nowy] > 1e9)
				{
					finalx = nowx; finaly = nowy;
					mark = 1;
					return;
				}
				q.push({ nowx,nowy });
			}
		}

	}
}

int main()
{
	cin >> m;
	memset(tstar, 0x3f, sizeof tstar);
	vis[0][0] = 1;
	dis[0][0] = 0;
	for (int i = 1; i <= m; i++)
	{
		int tempstarx, tempstary;
		cin >> tempstarx >> tempstary;
		star_loc[i] = { tempstarx,tempstary };
		int tempp;
		cin >> tempp;
		tstar[tempstarx][tempstary]=min(tempp, tstar[tempstarx][tempstary]);
	}
	spread();
	bfs();
	if (mark == 1) cout << dis[finalx][finaly] << endl;
	else cout << "-1" << endl;
}

5.6.8-10WA,求大佬帮助。感觉焦土时间没啥问题,也注意到了人可以走出300

2023/12/22 17:01
加载中...