[2451일] A. Lights Out (성공)

코드포스 공부기록

목록 보기
18/23

A. Lights Out
time limit per test2 seconds
memory limit per test256 megabytes
Lenny is playing a game on a 3 × 3 grid of lights. In the beginning of the game all lights are switched on. Pressing any of the lights will toggle it and all side-adjacent lights. The goal of the game is to switch all the lights off. We consider the toggling as follows: if the light was switched on then it will be switched off, if it was switched off then it will be switched on.

Lenny has spent some time playing with the grid and by now he has pressed each light a certain number of times. Given the number of times each light is pressed, you have to print the current state of each light.

Input
The input consists of three rows. Each row contains three integers each between 0 to 100 inclusive. The j-th number in the i-th row is the number of times the j-th light of the i-th row of the grid is pressed.

Output
Print three lines, each containing three characters. The j-th character of the i-th line is "1" if and only if the corresponding light is switched on, otherwise it's "0".

cpp

#include<iostream>
using namespace std;
int grid[3][3], answer[3][3];
int dx[4] = { 0,0,-1,1 }, dy[4] = { -1,1,0,0 };
void cal(int y,int x, int value)
{
	if (value == 0)return;
	answer[y][x]=( answer[y][x] == 1 ? 0 : 1);
	for (int i = 0; i < 4; i++)
	{
		int ny = dy[i] + y;
		int nx = dx[i] + x;
		if (ny < 0 || ny>2 || nx < 0 || nx>2)continue;
		answer[ny][nx] = (answer[ny][nx] == 1 ? 0 : 1);
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cin >> grid[i][j];
			grid[i][j] %= 2;
			answer[i][j] = 1;
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cal(i,j,grid[i][j]);
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << answer[i][j];
		}
		cout << "\n";
	}
	return 0;
}

c#

using System;
using System.IO;
using System.Text;
using System.Threading;
class Program
{
    static BufferedStream sr = new BufferedStream(Console.OpenStandardInput());
    static StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()), Encoding.Default);
   static int[,] grid = new int[3, 3], answer = new int[3, 3];
    static void cal(int y, int x, int value)
    {
        if (value == 0) return;
        int[] dx = { 0, 0, -1, 1 };
        int[] dy = { -1, 1, 0, 0 };
        answer[y, x] = (answer[y, x]==1?0:1);
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx > 2 || ny > 2) continue;
            answer[ny, nx] = (answer[ny, nx] == 1 ? 0 : 1);
        }
    }
    static int ReadInt()
    {
        int c = sr.ReadByte();
        while (c <= 32) { if (c == -1) return -1; c = sr.ReadByte(); }
        bool neg = false;
        if (c == '-') { neg = true; c = sr.ReadByte(); }
        int val = 0;
        while (c > 32)
        {
            val = val * 10 + (c - '0');
            c = sr.ReadByte();
        }
        return neg ? -val : val;
    }
    static void Solve()
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                grid[i, j] = ReadInt() % 2;
                answer[i, j] = 1;
            }
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cal(i, j, grid[i, j]);
            }
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
               sw.Write( answer[i, j]);
            }
            sw.WriteLine();
        }
        sw.Flush();
    }
    static void Main(string[] args)
    {
        Thread thread = new Thread(Solve, 1024 * 1024 * 64);
        thread.Start();
        thread.Join();
    }
}

python3

import sys
input = sys.stdin.readline
data = sys.stdin.buffer.read()
ndata = len(data)
ii = 0
def read_int():
    global ii, ndata, data
    while ii < ndata and data[ii] <= 32:
        ii += 1
    if ii >= ndata:
        return 0
    sign = 1
    if data[ii] == 45:  # ASCII 45 == '-'
        sign = -1
        ii += 1
    num = 0
    while ii < ndata and data[ii] > 32:
        num = num * 10 + (data[ii] - 48)
        ii += 1
    return num * sign
answer=[ [1]*3 for _ in range(3) ] 
def cal (y,x,value):
    if value==0:
        return
    dx =[0,0,-1,1] 
    dy= [-1,1,0,0] 
    answer[y][x] ^=1
    for i in range(4):
        nx=x+dx[i]
        ny=y+dy[i] 
        if nx<0 or nx>2 or ny<0 or ny>2:
            continue
        answer[ny][nx] = (1 if answer[ny][nx] ==0 else 0)

for y in range(3):
    for x in range(3):
        cal(y,x, read_int() %2)
for y in range(3):
    for x in range(3):
        print(answer [y][x] ,end="")
    print()

0개의 댓글