diff --git a/08/1325.c++ b/08/1325.c++ new file mode 100644 index 0000000..2e632da --- /dev/null +++ b/08/1325.c++ @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + +using namespace std; +typedef pair p; + +//1. 컴퓨터들의 신뢰관계를 vector>에 저장하여 신뢰 방향을 알아볼 수 있도록 한다. +//2. 모든 컴퓨터를 한 번씩 해킹하여 각 컴퓨터에 따라 해킹할 수 있는 컴퓨터 수를 모두 구한다. +//3. 해킹할 수 있는 컴퓨터 수는 bfs 탐색을 통해 찾는다. (queue와 해킹 여부 bool vector 사용) +//4. 정렬하여 출력한다. + +bool cmp(p& p1, p&p2){ + if(p1.first!=p2.first){ + return p1.first>p2.first; + } + return p1.second>&v, int n){ //bfs 탐색으로 해킹한다. + queue q; //해킹된 컴퓨터를 queue에 저장한다. (해당 컴퓨터를 신뢰하는 다른 컴퓨터를 찾는 용도) + vector hacked(n+1, false); //해킹 여부를 bool vector에 저장한다. + q.push(node); + hacked[node]=true; + int cnt=1; + while(!q.empty()){ + int node = q.front(); + q.pop(); + //해킹된 컴퓨터를 신뢰하는 모든 컴퓨터를 해킹한다. + for(const auto& next : v[node]){ + if(!hacked[next]){ + q.push(next); + hacked[next]=true; + cnt++; + } + } + } + return cnt; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); cout.tie(NULL); + + int n, m; + cin >> n >> m; + vector> v(n+1,vector(0)); + + //신뢰 방향이 있으므로, 신뢰 받는 컴퓨터 인덱스에 신뢰하는 컴퓨터를 value로 넣는다. + while(m--){ + int a, b; + cin >> a >> b; + v[b].push_back(a); + } + + vector

ans; + for(int i=n;i>0;i--){ //모든 컴퓨터를 한 번씩 해킹하여 전파된 컴퓨터 개수를 처음 해킹한 컴퓨터 번호와 함께 저장한다. + int h = hack(i,v,n); + ans.push_back({h,i}); + } + sort(ans.begin(),ans.end(),cmp); + for(int i=0;i +#include + +using namespace std; +typedef pair p; + +//1. 왼쪽-위부터 바둑돌이 놓아져 있는지 확인한다. +//2. 바둑돌을 발견한다면 그 바둑돌 기준으로 오른쪽, 아래, 오른쪽-아래, 왼쪽-아래에 같은 색 바둑돌이 있는지 확인한다. +//3. 같은색 바둑돌을 발견한다면 그 방향으로 정확히 5개의 바둑돌이 놓여져있는지 확인한다. +//4. 만약 5개의 바둑돌이 놓여져있다면, 처음으로 발견한 바둑돌 반대 방향에 같은 색 바둑돌이 있는지 확인한다. (6개가 아닌지 확인) + +bool dfs(int color, int cnt, int r, int c, vector>& v, int ud, int rl){ + int nr = r + ud; + int nc = c + rl; + + if(nr>=0 && nc>=0 && nr<19 && nc<19 && v[nr][nc]==color){ + cnt++; + return dfs(color, cnt, nr, nc, v, ud, rl); + } + else if(cnt==5) return true; //같은 방향으로 5번 나올시 승 + return false; +} + +void check(int color, int r, int c, vector>& v, p& ans){ + //왼쪽-위에서부터 탐색하므로 오른쪽, 아래, 오른쪽-아래, 왼쪽-아래 대각선만 봐도 된다. + int n[4]={1,0,1,-1}; + int m[4]={0,1,1,1}; + + for(int i=0;i<4;i++){ + int nr = r + n[i]; + int nc = c + m[i]; + + //연결된 같은색 바둑돌을 발견 시 같은 방향으로 dfs 탐색 + if(nr>=0 && nc>=0 && nr<19 && nc<19 && v[nr][nc]==color){ + if(dfs(color, 2, nr, nc, v, n[i],m[i])){ + //5개 이상부터는 답이 아니므로 반대 방향 같은 색인지 확인 + int pr = r-n[i]; + int pc = c-m[i]; + if(pr>=0&&pc>=0&&pr<19&&pc<19&&v[pr][pc]==color){ + continue; + } + ans = make_pair(r,c); + break; + } + } + } +} + +p find_win(vector>& v){ //왼쪽-위부터 바둑알 탐색 + p ans={-1,-1}; + for(int i=0;i<19;i++){ + for(int j=0;j<19;j++){ + if(v[i][j]==1){ + check(1,i,j,v,ans); + } + else if(v[i][j]==2){ + check(2,i,j,v,ans); + } + } + } + return ans; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); cout.tie(NULL); + + vector> v(19,vector(19,0)); + + for(int i=0;i<19;i++){ + for(int j=0;j<19;j++){ + cin >> v[i][j]; + } + } + p ans = find_win(v); + if(ans!=make_pair(-1,-1)){ + cout << v[ans.first][ans.second] << "\n"; + cout << ans.first+1 << " " << ans.second+1; + } + else cout<<0; + return 0; +} \ No newline at end of file diff --git a/08/4963.c++ b/08/4963.c++ new file mode 100644 index 0000000..e63b615 --- /dev/null +++ b/08/4963.c++ @@ -0,0 +1,71 @@ +#include +#include +#include + +using namespace std; +typedef pair p; + +const int VISITED = 2; + +void bfs(int i, int j, int w, int h, vector>& v){ + //상하좌우 대각선 한 칸씩 + int n[8]={1, -1, 0, 0,1,1,-1,-1}; + int m[8]={0, 0, 1, -1,1,-1,1,-1}; + + queue

q; + q.push({i,j}); + v[i][j]=VISITED; + + while(!q.empty()){ + int r = q.front().first; + int c = q.front().second; + q.pop(); + + for(int i=0;i<8;i++){ //기준 칸의 상하좌우, 대각선 모두 보기 + int nr = r+n[i]; + int nc= c + m[i]; + + if(nr>=0 && nc>=0 && nr> &v){ + int cnt=0; + for(int i=0;i> v; + queue

q; + + while(true){ + int w, h; + cin >> w >> h; + if(w==0 && h==0) break; //0 0 입력시 종료 + + v.assign(h, vector (w,0)); //땅 정보 + for(int i=0;i> v[i][j]; + } + } + cout << cntIsland(h,w,v)<<"\n"; + } + + return 0; +} \ No newline at end of file