cf35C
• N*M的格子,最开始有K个点 (坐标给定) 开始着火
• 每一秒着火的点会扩散到与其距离为1的其他点
• 求最后一个着火的点
• 1 ≤ n, m ≤ 2000 • 1 ≤ K ≤ 10
没什么好说的 简单的bfs
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 2025;
vector<pair<int ,int > > vt;
int mp[MAX_N][MAX_N],n,m;
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
void bfs()
{
queue<pair<pair<int,int>,int > > q;
int sz = vt.size();
for(int i = 0;i<sz;++i)
{
q.push(make_pair(make_pair(vt[i].first,vt[i].second),0));
mp[vt[i].first][vt[i].second] = 0;
}
while(!q.empty())
{
pair<pair<int ,int > ,int > now = q.front();
q.pop();
int x = now.first.first,y = now.first.second;
for(int i = 0;i<4;++i)
{
int tx = x+dir[i][0],ty = y + dir[i][1];
if(tx<=n&&tx>=1&&ty<=m&&ty>=1&&mp[tx][ty]==-1)
{
q.push(make_pair(make_pair(tx,ty),now.second+1));
mp[tx][ty] = now.second+1;
}
}
}
}
int main()
{
//ios::sync_with_stdio(false);
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int k;
scanf("%d%d%d",&n,&m,&k);
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
vt.push_back(make_pair(a,b));
}
int maxx = -1;
memset(mp,-1,sizeof(mp));
bfs();
for(int i = 1;i<=n;++i)
for(int j = 1;j<=m;++j)
maxx = max(maxx,mp[i][j]);
pll ans ;
for(int i = 1;i<=n;++i)
for(int j = 1;j<=m;++j)
if(mp[i][j]==maxx) ans = pll(i,j);
printf("%d %d\n",ans.first,ans.second);
fclose(stdin);
fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
博客介绍了Codeforces 35C问题,该问题涉及一个N*M的网格,初始有K个点起火,并按单位距离扩散。每秒火势会蔓延到相邻的点,任务是找出最后被点燃的点。问题约束为n, m≤2000,K≤10。解决方案是采用简单的广度优先搜索(BFS)算法。"
125453106,13905013,使用Dockerfile与RDS构建及管理容器,"['docker', '容器', 'AWS', 'ECR', '镜像构建']
287

被折叠的 条评论
为什么被折叠?



