c++35分求助

P2895 [USACO08FEB] Meteor Shower S

XiaoYuyu @ 2023-12-22 17:01:42

#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


by XiaoYuyu @ 2023-12-22 17:45:15

#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 };
int tspread[400][400];
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)
            {
                tspread[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  && tspread[nowx][nowy]>dis[t.first][t.second] + 1)
            {
                vis[nowx][nowy] = 1;
                dis[nowx][nowy] = dis[t.first][t.second] + 1;
                if (tspread[nowx][nowy] > 1e9)
                {
                    finalx = nowx; finaly = nowy;
                    mark = 1;
                    return;
                }
                q.push({ nowx,nowy });
            }
        }

    }
}

int main()
{
    cin >> m;
    memset(tspread, 0x3f, sizeof tspread);
    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]);
        tspread[tempstarx][tempstary] = tstar[tempstarx][tempstary];
    }
    spread();
    if (tspread[0][0] > 1e9) 
    {
        cout << 0 << endl;
        return 0;
    }
    bfs();
    if (mark == 1) cout << dis[finalx][finaly] << endl;
    else cout << "-1" << endl;
}

稍微改了一下焦土,之前有错误。但是还是35分。


|