这道题可以用搜索写嘛..,本蒻写了个bfs全超时.. 求大佬指教。
可以通过剪枝来ac嘛..
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#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)