洛谷:CF1059B Forgery

题目入口
洛谷:CF1059B Forgery_第1张图片
洛谷:CF1059B Forgery_第2张图片
思路:显然数据范围和题意来看可以直接遍历模板矩阵,找到符合条件的3*3小区域,并在新创建的矩阵上进行修改,最后再将两矩阵进行比较即可。
代码实现:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define int long long
#define lowbit(x) (x &(-x))
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double EXP = 1e-8;
const ll   MOD = 1e9 + 7;
const int  N = 1005;
int n, m;
char a[N][N], b[N][N];

signed main()
{
   IOS;
   cin >> n >> m;
   for(int i = 0; i < n; i ++)
      cin >> a[i];
   for(int i = 0; i < n; i ++)   //先初始化b数组
      for(int j = 0; j < m; j ++)
         b[i][j] = '.';
   for(int i = 1; i < n - 1; i ++){
      for(int j = 1; j < m - 1; j ++){    //外围边缘不符合条件可以不用遍历
         if(a[i - 1][j - 1] != '#' || a[i - 1][j] != '#' || a[i - 1][j + 1] != '#')
             continue;
         if(a[i][j - 1] != '#' || a[i][j + 1] != '#')
             continue;
         if(a[i + 1][j - 1] != '#' || a[i + 1][j] != '#' || a[i + 1][j + 1] != '#')
             continue;
         b[i - 1][j - 1] = '#'; b[i - 1][j] = '#', b[i - 1][j + 1] = '#';
         b[i][j - 1] = '#'; b[i][j + 1] = '#';
         b[i + 1][j - 1] = '#'; b[i + 1][j] = '#'; b[i + 1][j + 1] = '#';
      }
   }
   int ok = 0;
   for(int i = 0; i < n; i ++){
      for(int j = 0; j < m; j ++){
         if(a[i][j] != b[i][j]){
            ok = 1;
            break;
         }
      }
   }
   cout << (ok ? "NO" : "YES") << endl;
   return 0;
}

你可能感兴趣的:(比赛&训练)