-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4 mjj111 #186
4 mjj111 #186
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. ์๊ณผ ๋๋
ํธ๋ฆฌ ์ํ์ธ๋ฐ, ๋ฐฉ๋ฌธํ๋ ์ง์ ์ "๋ค์ ๋ฐฉ๋ฌธ"ํด์ผ ํ๋๊น Visited ๋ฆฌ์คํธ๋ฅผ ํ์ฅํ๋ ์์ด๋์ด๊น์ง ์ข์๋๋ฐ...
๋๋/์ ์นด์ดํ ์ 1๋ฒ๋ง ์ผ์ด๋์ผ ํ๋๊น, ๋ฐฉ๋ฌธํ๋ ์ง์ ์ ๋ค์ ๋ฐฉ๋ฌธํ ๋ ์นด์ดํ ์ ํ์ง ์๋๋ก ํ๋ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๊ณผ์ ์์ ๊ฒฐ๊ตญ ๋ชป์ฐธ๊ณ ๋ธ๋ก๊ทธ๋ฅผ ๋ด๋ฒ๋ ธ๋ค์...
์ ๊ฒฝ์ฐ์
- Visited ๋ฆฌ์คํธ๋ฅผ [ํ์ฌ ๋ ธ๋][์ ์][๋๋ ์] 3๊ฐ๋ฅผ ๋์์ ํ์ธํ๋๋ก 3์ฐจ์์ผ๋ก ๋ง๋ค์์ต๋๋ค.
- ์/๋๋ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ Info ๋ฆฌ์คํธ์ "ํ์ธํจ"์ ์๋ฏธํ๋ '2' ๊ฐ์ ๊ธฐ๋กํด, ๋ฐฑํธ๋ํน ๊ณผ์ ์์
Info[node] == 2
์ด๋ฉด ์นด์ดํ ํ์ง ์๋๋ก ํ์ต๋๋ค.
์ ์ฒด ์ฝ๋
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int solution(vector<int> Info, vector<vector<int>> Edges)
{
enum EInfo
{
Sheep = 0,
Wolf = 1,
Checked = 2
};
const int N = Info.size();
vector<vector<int>> Tree(N);
for(const auto& Edge : Edges)
{
Tree[Edge[0]].emplace_back(Edge[1]);
Tree[Edge[1]].emplace_back(Edge[0]);
}
vector<vector<vector<bool>>> Visited(N, vector(N + 1, vector(N + 1, false)));
Visited[0][1][0] = true;
Info[0] = EInfo::Checked;
int Answer = 0;
function<void(int, int, int)> Backtracking = [&](int Src, int Sheep, int Wolf)
{
Answer = max(Answer, Sheep);
for(int Dst : Tree[Src])
{
if(Info[Dst] == EInfo::Sheep && !Visited[Dst][Sheep + 1][Wolf])
{
Visited[Dst][Sheep + 1][Wolf] = true;
Info[Dst] = EInfo::Checked;
Backtracking(Dst, Sheep + 1, Wolf);
Info[Dst] = EInfo::Sheep;
Visited[Dst][Sheep + 1][Wolf] = false;
}
else if(Info[Dst] == EInfo::Wolf && Sheep > Wolf + 1 && !Visited[Dst][Sheep][Wolf + 1])
{
Visited[Dst][Sheep][Wolf + 1] = true;
Info[Dst] = EInfo::Checked;
Backtracking(Dst, Sheep, Wolf + 1);
Info[Dst] = EInfo::Wolf;
Visited[Dst][Sheep][Wolf + 1] = false;
}
else if(Info[Dst] == EInfo::Checked && !Visited[Dst][Sheep][Wolf])
{
Visited[Dst][Sheep][Wolf] = true;
Backtracking(Dst, Sheep, Wolf);
Visited[Dst][Sheep][Wolf] = false;
}
}
}; Backtracking(0, 1, 0);
return Answer;
}
2. ํ ํธ์ง
์๊ธด ๊ฒ ๋ญ๊ฐ ๋ฑ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๋ฌธ์ ๊ธธ๋ C++์ "list" ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์จ์ ๋ฑ๋
๋ฑ๋
์ฝ๋ ์งฐ๋๋ฐ ๋
๋ค ์๊ฐ ์ด๊ณผ ๋๋๋ผ๊ตฌ์...?
๋ญ์ง... ํ๋ฉด์ ์ง๋ฌธ ๊ฒ์ํ ๋ค์ ธ๋ดค๋๋ฐ ๋ฆฌ์คํธ ์ฐ์ง ๋ง๊ณ vector(Java์ ArrayList)์จ๋ผ๋ ๊ธ์ด ์๊ธธ๋ ๋ฐ๊ฟจ๋๋ฐ ์ง์ง๋ก ๋๋๋ผ๊ตฌ์...?
๋ญ๊ฐ๋ญ๊ฐํ ์ผ์ด์์ต๋๋ค...
์ ์ฒด ์ฝ๋
#include <string>
#include <vector>
using namespace std;
struct FCell
{
int Up, Index, Down;
FCell(int InUp, int InIndex, int InDown)
: Up(InUp), Index(InIndex), Down(InDown)
{
}
};
string solution(int n, int k, vector<string> InCommand) {
vector<FCell> Table;
for(int i = 0; i <= n; ++i)
{
Table.emplace_back(i - 1, i, i + 1);
}
vector<FCell> PoppedCell;
string Answer(n, 'O');
auto CommandU = [&](int X)
{
while(X--)
{
k = Table[k].Up;
}
};
auto CommandD = [&](int X)
{
while(X--)
{
k = Table[k].Down;
}
};
auto CommandC = [&]()
{
PoppedCell.emplace_back(Table[k]);
const auto& [Up, Index, Down] = Table[k];
if(0 <= Up)
{
Table[Up].Down = Down;
}
if(Down < n)
{
Table[Down].Up = Up;
}
Answer[k] = 'X';
k = (Down == n ? Up : Down);
};
auto CommandZ = [&]()
{
const auto& [Up, Index, Down] = PoppedCell.back();
PoppedCell.pop_back();
if(0 <= Up)
{
Table[Up].Down = Index;
}
if(Down < n)
{
Table[Down].Up = Index;
}
Answer[Index] = 'O';
};
for(const string& Command : InCommand)
{
switch(Command[0])
{
case 'U':
CommandU(stoi(Command.substr(2)));
break;
case 'D':
CommandD(stoi(Command.substr(2)));
break;
case 'C':
CommandC();
break;
case 'Z':
CommandZ();
break;
default:
break;
}
}
return Answer;
}
์๋ ๊ทผ๋ฐ ์ฐ์งธ Stack ํ๋๋ก ๋ค ํด๊ฒฐํ์ จ๋์...
์๊ณผ ๋๋ ๋ฌธ์ ๊ด๋ จํด์ ์ข ๊ฒ์ํด๋ณด๋๊น, ์๋ ์๋๋๋ก๋ผ๋ฉด "๋ฐฑํธ๋ํน์ ์ด์ฉํ ์์ ํ์์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ ์ ๋ฐ์ ์๋ค"๋ผ๊ณ ํ๋ค์.
๋ค์๊ณผ ๊ฐ์ ํ
์คํธ ์ผ์ด์ค๊ฐ ์กด์ฌํ๋ค๋ฉด ๋ฌด์ํ ๋ง์ ์ค๋ณต ์ผ์ด์ค๋ก ์๊ฐ ์ด๊ณผ๊ฐ ๋๋ค๊ณ ํฉ๋๋ค.
info = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
edges = [[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [2, 6], [3, 7], [3, 8], [4, 9], [4, 10], [5, 11], [5, 12], [6, 13], [6, 14], [7, 15], [7, 16]]
Return = 17
์นด์นด์ค ํ
์คํธ์ผ์ด์ค๊ฐ ๋น์ฝํด์ ๊ฐ๋ฅํ ํ์ด๋ผ๊ณ ํ๋ค์...?
์์น๋๋ก๋ฉด, ๋นํธ๋ง์คํน์ ์ด์ฉํ Flood Fill ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํ์ด์ผ ํ๋ค๊ณ ํฉ๋๋ค.
์ ํด C++ ์ฝ๋
#include <string>
#include <vector>
#include <functional>
using namespace std;
int solution(vector<int> Info, vector<vector<int>> Edges)
{
const int N = Info.size();
vector<bool> Visited(1 << N, false);
vector<vector<int>> Graph(N);
for(const auto& Edge : Edges)
{
Graph[Edge[0]].emplace_back(Edge[1]);
}
int Answer = 0;
function<void(int)> DFS = [&](int Mask)
{
if(Visited[Mask]) return;
Visited[Mask] = true;
int Sheep = 0, Wolf = 0;
for(int Node = 0; Node < N; ++Node)
{
if(Mask & (1 << Node))
{
Sheep += (Info[Node] == 0);
Wolf += (Info[Node] == 1);
}
}
if(Sheep <= Wolf) return;
Answer = max(Answer, Sheep);
for(int Parent = 0; Parent < N; ++Parent)
{
if(!(Mask & (1 << Parent))) continue;
for(int Child : Graph[Parent])
{
DFS(Mask | (1 << Child));
}
}
}; DFS(1);
return Answer;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด๋ณด๋ ค๊ณ ๋
ธ๋ ฅํ์ผ๋ ๋๋ฌด ์ด๋ ค์์ ๋ชป ํ์์ต๋๋ค...
์ ๋ ์์ฒญ ๋ณต์กํ ๋ฌธ์ ๋ผ๊ณ ์๊ฐํ์๋๋ฐ, ์ ๋ง ๊ฐ๋จํ๊ฒ ์ ํธ์
จ๋ค์๐๐ป๐๐ป
์๊ณผ ๋๋ ๋ฌธ์ ์์ ๋ค์ ๋ฐฉ๋ฌธํ๊ฒ ๋๋ ๋
ธ๋๋ ์ด๋ป๊ฒ ์ฒ๋ฆฌํด์ผ ํ ์ง ์ด๋ ค์ ๋๋ฐ, ๊ตฌํํ์ ์ฝ๋๋ฅผ ํ์ฐธ ๋ค์ฌ๋ค๋ดค๋ค์๐ฎ
๋๋ถ์ ๋ง์ด ๋ฐฐ์๊ฐ๋๋ค!!!
๐ ๋ฌธ์ ๋งํฌ
ํ๋ก๊ทธ๋๋จธ์ค - ํํธ์ง
![image](https://private-user-images.githubusercontent.com/86913355/335202318-254b65ac-40bf-454d-ad58-30c8ff095d9a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MTY0MjUsIm5iZiI6MTczOTUxNjEyNSwicGF0aCI6Ii84NjkxMzM1NS8zMzUyMDIzMTgtMjU0YjY1YWMtNDBiZi00NTRkLWFkNTgtMzBjOGZmMDk1ZDlhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDA2NTUyNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAxMGM4ZTY5ZDQzNTU3ZTMxMzM3ZDM2YmY2ZDIyNzk1YmVkZjlkZGNlNWE2ZjU1ZGMyY2YzZGMyZmUwYjM4NjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.uHHteNH8hyfW0mtJK-ZExJZYpyrVPlrVSEwrCsW0jhs)
https://school.programmers.co.kr/learn/courses/30/lessons/81303
ํ๋ก๊ทธ๋๋จธ์ค - ์๊ณผ๋๋
![image](https://private-user-images.githubusercontent.com/86913355/335202229-90a4dbc4-92f7-4be9-843d-2f8a66ef27a2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MTY0MjUsIm5iZiI6MTczOTUxNjEyNSwicGF0aCI6Ii84NjkxMzM1NS8zMzUyMDIyMjktOTBhNGRiYzQtOTJmNy00YmU5LTg0M2QtMmY4YTY2ZWYyN2EyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDA2NTUyNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI4MGM5MmI4OGE3MDMwNDNiZGJjOTk0ZjAwYWFlYTg1OTQ3ZGVjYmRiNmM0NTU5ZjFlZWQyNDNlYjMyNjQ5Y2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.EjFhZugiOQo2FIzBXOz3SiI7yWhQGhb5u1v4t8WZVok)
https://school.programmers.co.kr/learn/courses/30/lessons/92343
โ๏ธ ์์๋ ์๊ฐ
ํํธ์ง 1 ์๊ฐ 30๋ถ (๋ฌธ์ ๋ฅผ ์๋ชป ์ดํดํ๊ณ ์ ๊ทผํ์ต๋๋ค ใ )
์๊ณผ ๋๋ 50๋ถ
โจ ๊ฐ๋ตํ ์ฝ๋ ์ค๋ช
ํ๋ก๊ทธ๋๋จธ์ค - ํํธ์ง
ํํธ์ง ๋ฌธ์ ๋ ๋ช ๋ น์ด์ ๋ฐ๋ผ ํ๋ฅผ ๊ฐฑ์ ํ๋ ๊ฒ์ธ๋ฐ,
๊ฐ ๋ช ๋ น์ด์ ๋ฐ๋ผ ๋จ์ํ ์นธ ์ด๋๋ง์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
๊ฐ ๋ช ๋ น์ด๋ฅผ ์ํํ๋ฉฐ ์ฒ๋ฆฌํฉ๋๋ค.
'U x': ํ์ฌ ์ ํ๋ ํ์ x๋งํผ ์๋ก ์ด๋ (now -= value).
'D x': ํ์ฌ ์ ํ๋ ํ์ x๋งํผ ์๋๋ก ์ด๋ (now += value).
'C': ํ์ฌ ์ ํ๋ ํ์ ์ญ์ ํ๊ณ , ์คํ์ ์ ์ฅ. ํ ๊ฐ์๋ฅผ ์ค์. ํ์ฌ ์ ํ๋ ํ์ด ๋ง์ง๋ง ํ์ด์๋ค๋ฉด ํ ์นธ ์๋ก ์ด๋.
'Z': ๋ง์ง๋ง์ผ๋ก ์ญ์ ํ ํ์ ๋ณต์. ์ญ์ ๋ ํ์ด ํ์ฌ ์ ํ๋ ํ๋ณด๋ค ์์ ์์ผ๋ฉด ํ์ฌ ์ ํ๋ ํ์ ํ ์นธ ์๋๋ก ์ด๋. ํ ๊ฐ์๋ฅผ ๋๋ฆผ.
๊ฒฐ๊ณผ์ ์ผ๋ก,
StringBuilder๋ฅผ ์ฌ์ฉํ์ฌ ํ์ฌ ์ํ๋ฅผ ๋ํ๋ด๋ ๋ฌธ์์ด์ ์์ฑํฉ๋๋ค.
๋ชจ๋ ๋จ์์๋ ํ์ ๋ํด 'O'๋ฅผ ์ถ๊ฐํ๋ฉฐ ์คํ์์ ์ญ์ ๋ ํ์ ์ธ๋ฑ์ค๋ฅผ ๊บผ๋ด 'X'๋ฅผ ํด๋น ์์น์ ์ฝ์ ํฉ๋๋ค.
ํ๋ก๊ทธ๋๋จธ์ค - ์๊ณผ๋๋
์๊ณผ ๋๋์ ๋ฌธ์ ๋ ์กฐ๊ฑด์ ๋ถํฉํ์ฌ ๊ฐ์ฅ ๋ง์ ์์ ๊ตฌํ๋ ๊ฒ์ธ๋ฐ,
DFS๋ก ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
์ ํ์ ์ธ DFS์ ๋ชจ์ต์ ๊ฐ๊ณ ์์ด์.. ๋ค๋ค ์์๊ฒ ์ง๋ง..
private void dfs(int now, int sheepCnt, int wolfCnt, Set<Integer> nexts) {
dfs ๋ฉ์๋๋ ํ์ฌ ๋ ธ๋(now), ํ์ฌ๊น์ง์ ์ ์(sheepCnt), ๋๋ ์(wolfCnt), ๊ทธ๋ฆฌ๊ณ ๋ค์์ ํ์ํ ๋ ธ๋ ๋ชฉ๋ก(nexts)์ ์ธ์๋ก ๋ฐ์ต๋๋ค.
ํ์ฌ ๋ ธ๋๊ฐ ์์ธ์ง ๋๋์ธ์ง์ ๋ฐ๋ผ sheepCnt ๋๋ wolfCnt๋ฅผ ์ฆ๊ฐ์ํต๋๋ค.
๋๋ ์๊ฐ ์ ์๋ณด๋ค ๋ง์์ง๋ฉด ํด๋น ๊ฒฝ๋ก ํ์์ ์ค๋จํฉ๋๋ค.
ํ์ฌ๊น์ง์ ์ต๋ ์ ์๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค.
ํ์ฌ ํ์ ์์น๋ฅผ ์ ์ธํ ๋ค์ ํ์ ์์น๋ฅผ ๋ณต์ฌํฉ๋๋ค.
ํ์ฌ ๋ ธ๋์ ์ฐ๊ฒฐ๋ ์์ ๋ ธ๋๋ฅผ copySet์ ์ถ๊ฐํฉ๋๋ค.
copySet์ ์๋ ๊ฐ ๋ ธ๋์ ๋ํด ์ฌ๊ท์ ์ผ๋ก dfs๋ฅผ ํธ์ถํ์ฌ ํ์์ ๊ณ์ํฉ๋๋ค.
์ดํ์๋ ๋ค์ ํ์ ์์น ๋ณต์ฌ ๋ถ๋ถ์์ ํท๊ฐ๋ ค์ ๊ณ์ ํ๋ ธ์์ต๋๋ค ใ
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๋ฌธ์ ๋ฅผ ๋๋ฌด ์ด๋ ต๊ฒ ํด์ํ๋ ค ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ฌธ์ ๊ทธ๋๋ก ์ ๊ทผํ๊ณ , ์ดํ์ ์ต์ ํ๋ฅผ ํ์ด์ผ ํ๋๋ฐ.. ๋จผ์ ์ต์ ํ๋ ๋ฐฉ๋ฒ์ ๋ชจ์ํ๋ ์ค์๊ฐ ์ฆ๋ค์
์ ๋ ๋ฌธ์ ์์ ๋ผ์ ๋ฆฌ๊ฒ ๋ค์ ๋๊ผ๊ณ ๊ณ ์ณ์ผ๊ฒ ์ต๋๋ค ใ