๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2021 ์นด์นด์˜ค ์ธํ„ด์‹ญ ๋ฌธ์ œ, ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ

ibelieveinme 2021. 8. 17. 14:49
728x90

[๋ฌธ์ œ]

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ

[["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1]

programmers.co.kr

 

[๋ฌธ์ œ์กฐ๊ฑด]

  1. ๋Œ€๊ธฐ์‹ค์€ 5๊ฐœ์ด๋ฉฐ, ๊ฐ ๋Œ€๊ธฐ์‹ค์€ 5x5 ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค.
  2. ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์œ„ํ•˜์—ฌ ์‘์‹œ์ž๋“ค ๋ผ๋ฆฌ๋Š” ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2 ์ดํ•˜๋กœ ์•‰์ง€ ๋ง์•„ ์ฃผ์„ธ์š”.
  3. ๋‹จ ์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ ์‚ฌ์ด๊ฐ€ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋ง‰ํ˜€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

[๋ฌธ์ œ ํ•ด์„ค]

์‘์‹œ์ž, ์ฑ…์ƒ, ํŒŒํ‹ฐ์…˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด vector<string> ์ด ์ฃผ์–ด์ง€๊ณ ,

์‘์‹œ์ž๋“ค์ด ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๋ฅผ ์ž˜ ์œ ์ง€ํ•˜๊ณ  ์žˆ๋Š”์ง€ ํƒ์ƒ‰ํ•˜๋ฉด ๋œ๋‹ค.

 

์ด ๋•Œ, ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๋Š” ๋‘ ์ขŒํ‘œ (r1, c1), (r2, c2)๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,

๏ฝœr1-r2๏ฝœ+๏ฝœc1-c2๏ฝœ๋กœ ํŒ๋‹จ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, (0, 0), (2, 0)์˜ ์ขŒํ‘œ๋ผ๋ฉด ๏ฝœ0-2๏ฝœ+๏ฝœ0-0๏ฝœ= 2 ์ด๋ฏ€๋กœ

์ด ์ขŒํ‘œ์‚ฌ์ด์— ํŒŒํ‹ฐ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋ฐ P๊ฐ€ ์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค. (์•„๋ž˜ ์ด๋ฏธ์ง€ ์ฐธ์กฐ)

์ฆ‰, ๋‹จ์ˆœํžˆ 3x3 ํ–‰์—ด ๋ฒ”์œ„๋งŒ ํŒŒ์•…ํ•˜๋ฉด ์•ˆ๋˜๋Š” ์ด์œ ์ด๋‹ค. 

์–ผํ• ๋ณด๋ฉด, ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ,

(0, 0)๊ณผ (2, 0)์˜ P ๋Š” ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ด๊ณ  ๋นˆ ์ฑ…์ƒ์ด ์‚ฌ์ด์— ์žˆ์œผ๋ฏ€๋กœ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์€ ๊ฐ•์˜์‹ค์ด๋‹ค.

 

 

[ํ’€์ด ํ•ด์„ค ๋ฐ ์ฝ”๋“œ]

; P์˜ ์ขŒํ‘œ์—์„œ ๋งจํ—ˆํŠผ๊ฑฐ๋ฆฌ๊นŒ์ง€๋ฅผ BFS๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋˜ ๋‹ค๋ฅธ P๋ฅผ ๋งŒ๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ๋‹ค.

 

1) P์˜ ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ pair vector์— ๋„ฃ์–ด๋‘๊ณ  vector ์‚ฌ์ด์ฆˆ๋งŒํผ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.

vector<pair<int, int>> person_seats ๋ณ€์ˆ˜.

 

2) ์ฒซ๋ฒˆ์งธ person_seats ์ขŒํ‘œ๋ถ€ํ„ฐ BFS๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค. queue<pair<int, int>> q ๋ณ€์ˆ˜์™€ direction[4][2] ๋ณ€์ˆ˜๋กœ.

์ด ๋•Œ, ์žฌ๋ฐฉ๋ฌธ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ๋ฐฉ๋ฌธ์ฒดํฌ๋ณ€์ˆ˜์— ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•œ๋‹ค. ์ด ๋•Œ, X,Y ๋ฒ”์œ„๋ฅผ ๋„˜์ง€ ์•Š๋Š”์ง€, ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์€ ์•„๋‹Œ์ง€๋ฅผ ์ฒดํฌํ•˜๋ฉด์„œ ํƒ์ƒ‰ํ•œ๋‹ค.

 

3) ๋‹ค์Œ ์ขŒํ‘œ๊ฐ€ P๋ผ๋ฉด ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ 2 ์•ˆ์— ์žˆ๋Š” ์ขŒํ‘œ์ด๋ฉด์„œ P๊ฐ€ ๋˜ ์กด์žฌํ•œ๋‹ค๋Š” ๋ง์ด๋ฏ€๋กœ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์•˜๋‹ค๋Š” ๋ง์ด๋‹ค. false ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

 

4) ๋‹ค์Œ ์ขŒํ‘œ๊ฐ€ O๋ผ๋ฉด ๋นˆ์ž๋ฆฌ์ด๋ฏ€๋กœ ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ๋‹ค์‹œ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ queue์— ๋„ฃ์–ด์ฃผ๊ณ , ๋ฐฉ๋ฌธ์ฒดํฌํ•ด์ค€๋‹ค.

 

5) ํ•œ๋ฒˆ ์ƒํ•˜์ขŒ์šฐ๋กœ BFS ํƒ์ƒ‰์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๊นŠ์ด๊ฐ€ 1์ธ ๊ณณ์„ ๋‹ค ํƒ์ƒ‰ํ•˜๋Š” ๊ฒŒ ๋œ๋‹ค. ๊นŠ์ด๋Š” ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ์™€๋„ ๊ฐ™๋‹ค.

 

7) ๋‹ค์‹œํ•œ๋ฒˆ BFS ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๊ณ , ๊นŠ์ด๊ฐ€ 2์ธ ๊ณณ = ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ธ๊ณณ ๊นŒ์ง€ ๋ด์ค€๋‹ค.

 

8) ๋‹ค์‹œ BFS ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•  ๋•Œ, ๋งจํ—ˆํŠผ๊ฑฐ๋ฆฌ๊ฐ€ 2๋ผ๋ฉด, ๋‹ค์Œ ํƒ์ƒ‰์€ ๋งจํ—ˆํŠผ๊ฑฐ๋ฆฌ 3์ธ ์ขŒํ‘œ๊ฐ€ ๋œ๋‹ค. ์ฆ‰, ์ด๋ฏธ 2์ธ๊ฑฐ๋ฆฌ๊นŒ์ง€ ํƒ์ƒ‰ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ํ•ด๋‹น P์˜ ์ขŒํ‘œ๋Š” ๋”์ด์ƒ ๋ณผ ํ•„์š”๊ฐ€ ์—†๋‹ค. break๋ฌธ์œผ๋กœ ๋‹ค์Œ P์˜ ์ขŒํ‘œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ๋น ์ ธ๋‚˜์˜จ๋‹ค.

if (abs(person_seats[i].first - curX) + abs(person_seats[i].second - curY) == 2) break; ๊ฐ€ ๋ฐ”๋กœ ์ด ๋ถ€๋ถ„ !

 

9) ๋ชจ๋“  P์˜ ์ขŒํ‘œ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ–ˆ๋Š”๋ฐ return false๋ฌธ์„ ๋งŒ๋‚˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋ชจ๋“  ์ขŒํ‘œ๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ๋งŒ์กฑํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ true๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <iostream>
using namespace std;

bool checkSeat(vector<string>, vector<pair<int, int>>);

vector<int> solution(vector<vector<string>> places) {
	vector<int> answer;

	for (int i = 0; i < 5; i++) {//๊ฐ•์˜์‹ค 5๊ฐœ
		vector<pair<int, int>> person_seats;
		for (int row = 0; row < 5; row++) {//ํ•œ ๊ฐ•์˜์‹ค ๋‚ด์˜ ํ•œ ํ–‰
			for (int col = 0; col < 5; col++) {//ํ•œ ๊ฐ•์˜์‹ค ๋‚ด์˜ ํ•œ ์—ด
				if (places[i][row][col] == 'P') {
					person_seats.push_back(make_pair(row, col));
				}
			}
		}
		checkSeat(places[i], person_seats) ? answer.push_back(1) : answer.push_back(0);
	}
	return answer;
}


bool checkSeat(vector<string> classroom, vector<pair<int, int>> person_seats) {//BFS
	int direction[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; //์œ„, ์˜ค๋ฅธ์ชฝ, ์•„๋ž˜, ์™ผ์ชฝ

	for (int i = 0; i < person_seats.size(); i++) {
		int visited[5][5] = { 0, };
		queue<pair<int, int>> q;

		q.push(make_pair(person_seats[i].first, person_seats[i].second));
		visited[person_seats[i].first][person_seats[i].second] = 1;

		while (!q.empty()) {
			int curX = q.front().first;
			int curY = q.front().second;
			q.pop();

			if (abs(person_seats[i].first - curX) + abs(person_seats[i].second - curY) == 2) break;

			for (int i = 0; i < 4; i++) {
				int nextX = curX + direction[i][0];
				int nextY = curY + direction[i][1];
				if (nextX >= 0 && nextX < 5 && nextY >= 0 && nextY < 5 && visited[nextX][nextY] == 0) {
					if (classroom[nextX][nextY] == 'P') return false;
					else if (classroom[nextX][nextY] == 'O') {
						visited[nextX][nextY] = 1;
						q.push(make_pair(nextX, nextY));
					}
				}
			}
		}
	}
	return true;//๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๋ฅผ ๋‹ค ํ†ต๊ณผํ–ˆ๋‹ค๋ฉด, ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ž˜ ์ง€ํ‚จ ๊ฒƒ.
}

 

์ฐธ๊ณ ๋กœ... ํ‹€๋ฆฐ ํ’€์ด ๋ฐ ํ‹€๋ฆฐ ์ฝ”๋“œ๋Š”... ๊ฑ ์ฐธ๊ณ ๋งŒ...

๋”๋ณด๊ธฐ

 

์ฒจ์—” ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2x2 ์•ˆ์ธ์ค„ ์•Œ๊ณ ,

๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ P์˜ ์ขŒํ‘œ๋ฅผ pair vector์— ๋ชจ๋‘ ์ €์žฅํ•œ ํ›„,

vector๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ,

 

๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์•˜๋‹ค๋Š” ์กฐ๊ฑด์œผ๋กœ

 

P์˜ ์ƒํ•˜์ขŒ์šฐ์— P๊ฐ€ ์žˆ์„ ๋•Œ์™€

P ์ƒํ•˜์ขŒ์šฐ์— O๊ฐ€ ์žˆ๊ณ  ๋Œ€๊ฐ์„ ์— P๊ฐ€ ์žˆ์„ ๋•Œ๋กœ ๋‘ฌ์„œ if๋ฌธ์œผ๋กœ ๋‹จ์ˆœ๋ฌด์‹ํ•˜๊ฒŒ ์งฐ๋‹ค.

 

๊ฒฐ๊ณผ๋Š” 3, 8, 9, 12๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์‹คํŒจ.  

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ฐพ์•„๋ณด๊ณ  ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ์˜ ์ฐธ๋œ ์˜๋ฏธ๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค... ํ•˜ํ•˜...

 

๋ฌธ์ œ ์กฐ๊ฑด ์ž˜ ์ฝ์ž...

๊ตฌํ˜„์ธ์ค„ ์•Œ๊ณ  ๋‹จ์ˆœ๋ฌด์‹ํ•˜๊ฒŒ ์งฐ๋Š”๋ฐ ์•ˆ๋˜๋ฉด, BFS/DFS/DP ๋“ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ชฝ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด์ž.

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๊ฐ€ ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค!! ์ซŒ๋งŒ ๋” ์ง‘์ค‘ํ•ด์„œ ์ฝ”๋”ฉํ•˜์ž...

 

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

bool checkSeat(vector<string>, vector<pair<int,int>>);

vector<int> solution(vector<vector<string>> places) {
    vector<int> answer;

    for(int i = 0; i < 5; i++){//๊ฐ•์˜์‹ค 5๊ฐœ
        vector<pair<int,int>> person_seats;
        for(int row = 0; row < 5; row++){//ํ•œ ๊ฐ•์˜์‹ค ๋‚ด์˜ ํ•œ ํ–‰
            for(int col = 0; col < 5; col++){//ํ•œ ๊ฐ•์˜์‹ค ๋‚ด์˜ ํ•œ ์—ด
                if(places[i][row][col]=='P'){
                    person_seats.push_back(make_pair(row,col));
                }
            }
        }
        checkSeat(places[i], person_seats) ? answer.push_back(1) : answer.push_back(0);
    }
    return answer;
}


bool checkSeat(vector<string> classroom, vector<pair<int,int>> person_seats){
    int direction[5][2] = {{0,1},{1,0},{-1,0},{1,1},{1,-1}}; //์˜ค๋ฅธ์ชฝ, ์•„๋ž˜, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ๋Œ€๊ฐ์„ , ์™ผ์ชฝ ๋Œ€๊ฐ์„ 

    for(int i = 0; i < person_seats.size(); i++){
        int curX = person_seats[i].first;
        int curY = person_seats[i].second;
        for(int i = 0; i<3; i++){//์˜ค๋ฅธ์ชฝ, ์•„๋ž˜, ์™ผ์ชฝ ํ™•์ธ
            int nextX = curX + direction[i][0];
            int nextY = curY + direction[i][1];
            if(nextX < 0 || nextX >= 5 || nextY < 0 || nextY >= 5) continue;
            if(classroom[nextX][nextY] == 'P'){
                return false;
            }
            else if(classroom[nextX][nextY] == 'O'){
                for(int j = 3; j < 5; j++){//์˜ค๋ฅธ์ชฝ ๋Œ€๊ฐ์„ , ์™ผ์ชฝ ๋Œ€๊ฐ์„  ํ™•์ธ
                    nextX = curX + direction[j][0];
                    nextY = curY + direction[j][1];
                    if(nextX < 0 || nextX >= 5 || nextY < 0 || nextY >= 5) continue;
                    if(classroom[nextX][nextY] == 'P'){
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

 

 

 

 

 

 

728x90