题目链接:https://atcoder.jp/contests/abc045/tasks/arc061_b
题目大意:有hw的网格初始都是白格,选n个将其染成黑色,求3x3的子网格包含黑色网格为0~9的个数。
题目思路:该题h,w范围较大故不能直接暴力求解。3x3的矩阵总和为(h-2)(w-2),对于一个黑色格可以影响的矩阵为9个,注意到n较小,所以我们最多只需要记录9*n个矩阵,用map很好的解决问题。
注意:边界问题。
细节看代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+100;
typedef pair<int,int>pa;
map<pa,int>mp;
ll ans[10];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
int h,w,n;
cin>>h>>w>>n;
for(int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
for(int i=-2;i<=0;i++)
for(int j=-2;j<=0;j++){
int aa=a+i;
int bb=b+j;
if(aa>=1&&aa<=h-2&&bb>=1&&bb<=w-2){
mp[make_pair(aa,bb)]++;
}
}
}
ans[0]=1ll*(h-2)*(w-2);
for(auto it=mp.begin();it!=mp.end();it++){
ans[it->second]++;
ans[0]--;
}
for(int i=0;i<=9;i++){
cout<<ans[i]<<endl;
}
return 0;
}
欢迎大家指错
AtCoder竞赛3x3网格染色问题解析
本文解析了AtCoder竞赛中一道关于3x3网格染色的算法题,介绍了如何通过使用map数据结构来高效解决大规模网格染色问题,避免了暴力求解的高时间复杂度。
1277

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



