P10266求指教
  • 板块P10266 高效清理
  • 楼主sll00
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/23 22:03
  • 上次更新2024/3/24 08:07:53
查看原帖
P10266求指教
1243869
sll00楼主2024/3/23 22:03

这道题可以用搜索写嘛..,本蒻写了个bfs全超时.. 求大佬指教。

传送门

可以通过剪枝来ac嘛..

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int,int>
#define mp make_pair
#define st string
#define vint vector<int>
#define vll vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Refor(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define maxn 4000
ll n,m,k;
ll arr[maxn][maxn];
ll vis[maxn][maxn];
ll xx,yy,p;
int dx[]={0,0,1,-1,1,-1,1,-1};
int dy[]={1,-1,0,0,1,-1,-1,1};
void bfs(int x,int y)
{
   queue<pair<ll,pair<ll ,ll>>>q;
   q.push(make_pair(1,make_pair(x,y)));
   while(!q.empty())
   {
       ll nx=q.front().second.first;
       ll ny=q.front().second.second;
       int ci=q.front().first;
       q.pop();
        if(ci==p)
        {
            continue;
        }
       For(i,0,7)
       {
           int tx=nx+dx[i];
           int ty=ny+dy[i];
           if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty])
           {
               vis[tx][ty]=1;
               arr[tx][ty]-=pow(p-ci,2);
               if(arr[tx][ty]<0)
               {
                   arr[tx][ty]=0;
               }
               if(ci+1<p)
                    q.push(make_pair(ci+1,make_pair(tx,ty)));
           }
       }
   }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>k;
    For(i,1,n)
    {
        For(j,1,m)
        {
            cin>>arr[i][j];
        }
    }
    while(k--)
    {
        cin>>xx>>yy>>p;
        memset(vis,0,sizeof(vis));
        arr[xx][yy]-=pow(p,2);
        if(arr[xx][yy]<0)
        {
            arr[xx][yy]=0;
        }
        vis[xx][yy]=1;
        bfs(xx,yy);
    }
    For(i,1,n)
    {
        For(j,1,m)
        {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

大佬们请讲:(orz)

2024/3/23 22:03
加载中...