Codeforces Round #288 DIV2 A問題 - Pasha and Pixels

Source

Codeforces Round #288 DIV2 A問題 (500pt)
Problem description

問題概要

$N$ 行 $M$ 列のマス目があって,最初は全てのマスが白.
このゲームでは,$1$ マス選んで,そのマスを黒く塗る(既に黒いマスを選んだら何もしない)ということを繰り返すが,$2 \times 2$ の正方形の領域で全てが黒い部分ができてしまったらその時点で負け(そして終了)となる.
どのマスを選んで黒く塗るかという $K$ ターン分の予定が与えられるので,$K$ ターン以内に負けてしまうなら,何ターン目で負けるか,はたまた $K$ ターン以内に負けないかを求める問題.

解法

シミュレーション.
毎ターン,新しく塗った場所の周辺 $3 \times 3$ マスの範囲で新しく $2 \times 2$ の黒い正方形ができていないかをチェックする.
時間計算量は $O(NM+K)$ 程度.

C++のスパゲッティなコード

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar()
#define mypc(c) putchar(c)

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}

int X, Y, K;
char mp[1010][1010];

int main(){
  int i, j, k;
  int di, dj, dame = 0;
  int res = 0;

  reader(&X,&Y,&K);
  rep(k,K){
    reader(&i,&j);
    mp[i][j] = 1;
    REP(di,-1,2) REP(dj,-1,2) if(di && dj){
      if(mp[i][j] + mp[i+di][j] + mp[i][j+dj] + mp[i+di][j+dj] == 4) dame = 1;
    }
    if(dame){ res = k+1; break; }
  }
  writerLn(res);

  return 0;
}

Current time: 2024年04月16日15時56分04秒
Last modified: 2015年01月28日23時03分45秒 (by laycrs)
Tags: Competitive_Programming Codeforces CF288 CF_Div2_A
トップページに戻る

Logged in as: unknown user (not login)

ログイン: