WARush

SRMの結果とか、解けた問題のコードを書いていきます

Codeforces #168 Div2 A "Lights Out"

問題

3×3のマス目にランプのスイッチがある。
あるマスのスイッチを押すと、
そのマスと上下左右に隣り合うマスのランプのON/OFFが切り替わる。
全てのランプは初めOFFになっている。

各マスのスイッチの押した回数が与えられるので、
ランプがONになっていたら'0'、OFFになっていたら'1'を出力せよ。

ソース
int main() {

    int lights[3][3];
    for( int i = 0; i < 3; i++ ){
        for( int j = 0; j < 3; j++ ){
            cin >> lights[i][j];
        }
    }

    for( int i = 0; i < 3; i++ ){
        for( int j = 0; j < 3; j++ ){
            int s = lights[i][j];
            int dx[4] = { 0, 0, -1, 1 };
            int dy[4] = { -1, 1, 0, 0 };
            for( int k = 0; k < 4; k++ ){
                int tx = j + dx[k];
                int ty = i + dy[k];
                if( tx < 0 || 3 <= tx || ty < 0 || 3 <= ty ) continue;
                s += lights[ty][tx];
            }
            if( s % 2 == 0 ){
                cout << "1";
            }else{
                cout << "0";
            }
        }
        cout << endl;
    }
    
    //while(true){}
}