๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1455_๋’ค์ง‘๊ธฐ II

๋ฟŒ์•ผ._. 2024. 4. 16. 12:08

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1455)

< ๋’ค์ง‘๊ธฐ II >

 

๋ฌธ์ œ ํ’€์ด 

 

๋’ท๋ฉด์ธ ๋™์ „ ์ค‘์—์„œ (0,0)์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋™์ „์œ„์น˜๋ถ€ํ„ฐ ๋’ค์ง‘๋Š”๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class _1455_ { // ๋’ค์ง‘๊ธฐ II
	static boolean arr[][];
	static int x, y;

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		arr = new boolean[n][m];

		int result = 0;

		x = -1;
		y = -1;
		for (int i = 0; i < n; i++) {
			String str = bf.readLine();
			for (int j = 0; j < m; j++) {
				if (str.charAt(j) == '1') {
					arr[i][j] = true;
					x = i;
					y = j;
				}
			}
		}

		while (x != -1 && y != -1) {
			result += 1;
			convert(x,y);
			check();
		}
		System.out.println(result);
	}

	private static void check() {
		x = -1;
		y = -1;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[0].length; j++) {
				if (arr[i][j]) {
					x = i;
					y = j;
				}
			}
		}

	}

	private static void convert(int x, int y) {
		for (int i = 0; i <= x; i++) {
			for (int j = 0; j <= y; j++) {
				arr[i][j] = !arr[i][j];
			}
		}
	}
}
๋ณ€์ˆ˜)
n, m : ๋™์ „ ํฌ๊ธฐ
arr : ๋™์ „์˜ ์•ž๋ฉด, ๋’ท๋ฉด ์—ฌ๋ถ€
result : ๋™์ „์„ ๋’ค์ง‘๋Š” ํšŸ์ˆ˜
x, y: ๋’ค์ง‘์„ ๋™์ „์˜ ์œ„์น˜

 

๋™์ „์˜ ํฌ๊ธฐ n, m์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋™์ „์˜ ํฌ๊ธฐ๋งŒํผ ๋™์ „์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ๋’ท๋ฉด์ผ ๊ฒฝ์šฐ true๋กœ ์ €์žฅํ•˜๊ณ  ์•ž๋ฉด์ผ ๊ฒฝ์šฐ false๋กœ ์ €์žฅํ•œ๋‹ค. ๋’ท๋ฉด์ผ ๊ฒฝ์šฐ ์•ž๋ฉด์œผ๋กœ ๋’ค์ง‘์–ด์•ผ ํ•˜๋ฏ€๋กœ (0,0)์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋’ท๋ฉด ๋™์ „ ์œ„์น˜๋ฅผ ๊ตฌํ•ด x, y์— ์ €์žฅํ•œ๋‹ค. ๋’ท๋ฉด์ธ ๋™์ „์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) result+1

2) convertํ•จ์ˆ˜ ํ˜ธ์ถœ

3) check ํ•จ์ˆ˜ ํ˜ธ์ถœ

 

์ตœ์ข… ๋™์ „์„ ๋’ค์ง‘๋Š” ํšŸ์ˆ˜์ธ result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

check()

x์™€ y๋ฅผ -1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋’ท๋ฉด์ธ ๋™์ „์ผ ๊ฒฝ์šฐ ์œ„์น˜๋ฅผ x์™€ y์— ์ €์žฅํ•œ๋‹ค.

 

convert(int x, int y)

(0,0)๋ถ€ํ„ฐ (x, y) ์œ„์น˜๊นŒ์ง€ ๋™์ „์„ ๋’ค์ง‘๋Š”๋‹ค.