๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BAEKJOON][Bruteforce] 2580๋ฒˆ ์Šค๋„์ฟ  ํ’€์ด & ๋ฐ˜๋ก€

ibelieveinme 2021. 10. 11. 02:26
728x90

[๋ฌธ์ œ]

 

2580๋ฒˆ: ์Šค๋„์ฟ 

์Šค๋„์ฟ ๋Š” 18์„ธ๊ธฐ ์Šค์œ„์Šค ์ˆ˜ํ•™์ž๊ฐ€ ๋งŒ๋“  '๋ผํ‹ด ์‚ฌ๊ฐํ˜•'์ด๋ž‘ ํผ์ฆ์—์„œ ์œ ๋ž˜ํ•œ ๊ฒƒ์œผ๋กœ ํ˜„์žฌ ๋งŽ์€ ์ธ๊ธฐ๋ฅผ ๋ˆ„๋ฆฌ๊ณ  ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฐ€๋กœ, ์„ธ๋กœ ๊ฐ๊ฐ 9๊ฐœ์”ฉ ์ด 81๊ฐœ์˜ ์ž‘์€ ์นธ์œผ๋กœ ์ด๋ฃจ

www.acmicpc.net

 

[๋ฌธ์ œ์„ค๋ช…]

์Šค๋„์ฟ  ๊ฒŒ์ž„์˜ ๋‹ต์„ ์ฐพ๋Š” ๋ฌธ์ œ ~!

 

์Šค๋„์ฟ  ๊ฒŒ์ž„ ์กฐ๊ฑด์€ ์•„๋ž˜ 2๊ฐ€์ง€๋‹ค.

  1. ๊ฐ๊ฐ์˜ ๊ฐ€๋กœ์ค„๊ณผ ์„ธ๋กœ์ค„์—๋Š” 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค.
  2. ๊ตต์€ ์„ ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š” 3x3 ์ •์‚ฌ๊ฐํ˜• ์•ˆ์—๋„ 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค.

[๋ฌธ์ œ ํ’€์ด]

์ฒจ์—” ์˜ˆ์‹œ๋งŒ ๋ณด๊ณ  ๊ฐ€๋กœ, ์„ธ๋กœ, 3x3์„ ํƒ์ƒ‰ํ•ด์„œ ํ•œ์นธ๋งŒ ๋น„์–ด์„œ ๋ฐ”๋กœ ํ’€์ˆ˜ ์žˆ๋Š” ์นธ์„ ํ’€๊ณ  ๋‹ค์Œ ์นธ์„ ํ‘ธ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” ๋Œ€๋ง... ์•„๋ž˜ ๋ฐ˜๋ก€์ฒ˜๋Ÿผ ๋นˆ์นธ์ด ๋งŽ์„ ๋•Œ๋„ ํฌํ•จํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๋•Œ, ๋‹ต์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋•Œ๋Š” ์ œ์ผ ์ฒ˜์Œ ๊ตฌํ•ด์ง„๊ฑธ ๋‹ต์œผ๋กœ ์ฑ„ํƒํ•œ๋‹ค!!! exit(0); !!!

 

[ํ‹€๋ฆฐ์ฝ”๋“œ]

๋”๋ณด๊ธฐ
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

void Input();
void Stocoo();
bool FillNumber(bool *check_num, int x, int y, int index);
void Output();

int map[9][9];
vector<pair<pair<int, int>, bool>> blank;

int main() {
    Input();
    Stocoo();
    Output();
    return 0;
}

void Input() {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            cin >> map[i][j];
            if (map[i][j] == 0) {
                blank.push_back(make_pair(make_pair(i, j), false));
            }
        }
    }
}

void Stocoo() {
    for (int i = 0; i < 9; i++) {
        int x = blank[i].first.first;
        int y = blank[i].first.second;

        bool check_num[10] = { false, };
        int check = 0;

        //๊ฐ€๋กœ
        for (int col = 0; col < 9; col++) {
            if (map[x][col] != 0) {
                check_num[map[x][col]] = true;
                check++;
            }
        }
        if (check == 8) { if (FillNumber(check_num, x, y, i)) continue; }
        memset(check_num, false, sizeof(check_num));
        check = 0;

        //์„ธ๋กœ
        for (int row = 0; row < 9; row++) {
            if (map[row][y] != 0) {
                check_num[map[row][y]] = true;
                check++;
            }
        }
        if (check == 8) { if (FillNumber(check_num, x, y, i)) continue; }
        memset(check_num, false, sizeof(check_num));
        check = 0;

        //3x3
        for (int row = x / 3 * 3; row < x / 3 * 3 + 3; row++) {
            for (int col = y / 3 * 3; col < y / 3 * 3 + 3; col++) {
                if (map[row][col] != 0) {
                    check_num[map[row][col]] = true;
                    check++;
                }
            }
        }
        if (check == 8) { if (FillNumber(check_num, x, y, i)) continue; }
    }
    for (int i = 0; i < blank.size(); i++) {
        if (!blank[i].second) {
            Stocoo();
        }
    }
}

bool FillNumber(bool *check_num, int x, int y, int index) {
    for (int i = 1; i < 10; i++) {
        if (!check_num[i]) {
            map[x][y] = i;
            blank[index].second = true;
            return true;
        }
    }
    return false;
}

void Output() {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            cout << map[i][j] << " ";
        }
        cout << "\n";
    }
}

์ฆ‰, ํ’€์ด๊ณผ์ •์„ ์ ์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1) bruteforce๋กœ ๋นˆ์นธ์— ๊ฐ€๋กœ, ์„ธ๋กœ, 3x3 ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” 1~9๋ฅผ ๋ชจ๋‘ ๋„ฃ์–ด๋ณธ๋‹ค.

2) ๋งŒ์•ฝ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด ์ด์ „์— ๋„ฃ์€ ๊ฐ’์ด ๋ฌธ์ œ์ด๋ฏ€๋กœ backtracking์œผ๋กœ ๋Œ์•„๊ฐ€์„œ(return) ๋‹ค์Œ ๊ฐ’์„ ๋„ฃ์–ด๋ณธ๋‹ค.

3) ๋ชจ๋“  ์นธ์„ ์ฑ„์› ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. 

 

๊ทผ๋ฐ backtracking ์—์„œ returnํ•ด์„œ map[x][y] = 0; ๊ตฌ๋ฌธ์„ ์ ์–ด์คฌ๋Š”๋ฐ, ์ด ๊ตฌ๋ฌธ์ด ์—†์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋‹ค.

๋‚ผ ์ •์‹  ๋ง‘์„ ๋•Œ ๋‹ค์‹œ ๋ด๋ด์•ผ์ง€ ใ…Žใ…Ž...

 

[๋ฐ˜๋ก€ ์˜ˆ์ œ]

TEST CASE # 1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

 

SAMPLE ANSWER #1
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5
5 3 1 6 4 2 9 7 8
6 4 2 9 7 8 5 3 1
9 7 8 5 3 1 6 4 2

 

TEST CASE # 2
0 2 0 9 0 5 0 0 0
5 9 0 0 3 0 2 0 0
7 0 0 6 0 2 0 0 5
0 0 9 3 5 0 4 6 0
0 5 4 0 0 0 7 8 0
0 8 3 0 2 7 5 0 0
8 0 0 2 0 9 0 0 4
0 0 5 0 4 0 0 2 6
0 0 0 5 0 3 0 7 0

 

SAMPLE ANSWER # 2
3 2 1 9 7 5 6 4 8
5 9 6 8 3 4 2 1 7
7 4 8 6 1 2 9 3 5
1 7 9 3 5 8 4 6 2
2 5 4 1 9 6 7 8 3
6 8 3 4 2 7 5 9 1
8 1 7 2 6 9 3 5 4
9 3 5 7 4 1 8 2 6
4 6 2 5 8 3 1 7 9

 

[์ฝ”๋“œ]

#include <iostream>
#include <vector>
using namespace std;

void Input();
void Stocoo(int blank_index);
bool CheckNumber(int x, int y, int num);
void Output();

int map[9][9];
vector<pair<int, int>> blank_position;

int main() {
	Input();
	Stocoo(0);
	Output();
	return 0;
}

void Input() {
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 9; j++) {
			cin >> map[i][j];
			if (map[i][j] == 0) {
				blank_position.push_back(make_pair(i, j));
			}
		}
	}
}

void Stocoo(int blank_index) {
	if (blank_index == blank_position.size()) {
		Output();
		exit(0); //๋ชจ๋‘๊ฐ€ 0์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์Šค๋„์ฟ ์ฒ˜๋Ÿผ ๋‹ต์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ,
				//๊ทธ๋Ÿด ๋•Œ ์ œ์ผ ์ฒซ๋ฒˆ์งธ๋กœ ๊ตฌํ•ด์ง„ ๋‹ต์„ ์ •๋‹ต์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋”์ด์ƒ ๊ตฌํ•˜์ง€ ์•Š๋„๋ก. ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.
	}
	int x = blank_position[blank_index].first;
	int y = blank_position[blank_index].second;

	for (int num = 1; num < 10; num++) { // 1, 2, 3, 4, 5, 6, 7, 8, 9 ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด๋ณด๊ธฐ
		//์กฐ๊ฑด์— ๋งž๋Š”์ง€ ์ฒดํฌ
		if (CheckNumber(x, y, num)) {
			//์กฐ๊ฑด์— ๋งž์œผ๋ฉด ๋‹ค์Œ blank ์ฑ„์šฐ๋Ÿฌ๊ฐ€๊ธฐ
			map[x][y] = num;
			Stocoo(blank_index+1);//*****์ฃผ์˜: blank_index++๋กœ ๋„ฃ์œผ๋ฉด ์•ˆ๋“ค๊ฐ€์ง
			//๋‹ค์Œ๊ฑธ ๋„ฃ์„๊ฒŒ ์—†์–ด์„œ return ๋์„ ๋•Œ!
			//Backtracking์œผ๋กœ ๋‹ค์‹œ gogo!
			map[x][y] = 0;//???????? ์ด ๊ตฌ๋ฌธ์ด ์—†์œผ๋ฉด ์™œ ์•ˆ๋ ๊นŒ......

		}
		//์กฐ๊ฑด์— ๋งž์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ ์ˆซ์ž ๋„ฃ์–ด๋ณด๊ธฐ
	}
}

bool CheckNumber(int x, int y, int num) {
	//๊ฐ€๋กœ
	for (int col = 0; col < 9; col++) {
		if (y != col && map[x][col] == num) {
			return false;
		}
	}
	//์„ธ๋กœ
	for (int row = 0; row < 9; row++) {
		if (x != row && map[row][y] == num) {
			return false;
		}
	}
	//3x3
	for (int row = x / 3 * 3; row < x / 3 * 3 + 3; row++) {//0,1,2,3,4,5,6,7,8,9 ๊ตฌ์—ญ์œผ๋กœ ๋‚˜๋ˆ”
		for (int col = y / 3 * 3; col < y / 3 * 3 + 3; col++) {
			if (x != row && y != col && map[row][col] == num) {
				return false;
			}
		}
	}
	return true;
}

void Output() {
	cout << "\n";
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 9; j++) {
			cout << map[i][j] << " ";
		}
		cout << "\n";
	}
}
728x90