[๋ฐฑ์ค€] BFS,DFS 1926, 2667, 2468, 2583, 2828, 10709 ๐ŸŽฏ

seunghyunยท2023๋…„ 4์›” 3์ผ
0

Test

๋ชฉ๋ก ๋ณด๊ธฐ
6/19

1926 ๊ทธ๋ฆผ / BFS (์žฅ๊ณ )

๋ฌธ์ œ ๋งํฌ

๋ฌด๋‚œํ•œ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋ฌธ์ œ์ด๊ณ  BFS, DFS๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋‚˜๋Š” BFS๋กœ ํ’€์–ด๋ณด์•˜๋‹ค.

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

int n,m;
const int MAX = 501;
int map[MAX][MAX]={0,}; // ์ž…๋ ฅ๊ฐ’ ์ €์žฅ
int visited[MAX][MAX]={0,};
int dy[] = {-1, 0, 1, 0}; // ์‹œ๊ณ„๋ฐฉํ–ฅ
int dx[] = {0, 1, 0, -1};  // ์‹œ๊ณ„๋ฐฉํ–ฅ
queue<pair<int,int>> q; // BFS ์‚ฌ์šฉ ํ
vector<int> v; // ๊ทธ๋ฆผ ๊ฐœ์ˆ˜ ์ €์žฅ ๋ฒกํ„ฐ
int s = 1; // ๊ทธ๋ฆผ ๋„“์ด

void bfs(int y, int x){
	visited[y][x] = true;
    q.push(make_pair(y,x));
    
    while(!q.empty()){
    	y = q.front().first;
        x = q.front().second;
        q.pop();
        
        for(int i=0; i<4; i++){
        	int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny<0 || nx<0 || ny>=n || nx >=m) continue;
            if(map[ny][nx] == 1 && visited[ny][nx]==0){
            	visited[ny][nx]=true;
                s++; // ๊ทธ๋ฆผ ๋„“์ด ์นด์šดํŠธํ•ด์ฃผ๊ธฐ
                q.push(make_pair(ny,nx));
            }
        }
    }
}

// ๋ฒกํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ์œ„ํ•ด~
bool compare(int i, int j){
	return i > j;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin >> n >> m;
    for(int i=0; i<n; i++){
    	for(int j=0; j<m; j++){
        	cin >> map[i][j];
        }
    }
    
    int cnt = 0; // connected component ๊ฐœ์ˆ˜
    for(int i=0; i<n; i++){
    	for(int j=0; j<m; j++){
        	if(map[i][j]==1 && visited[i][j]==0){
            bfs(i,j);
            v.push_back(s);
            cnt++;
            s = 1; // ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๊ธฐ~
            }
        }
    }
    sort(v.begin(), v.end(), compare); //๋ฒกํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    cout << cnt << endl;
    if(cnt == 0){
    	cout << 0 << endl;
    }
    else{
    	cout << v[0] << endl;
    }
}

2667 ๋‹จ์ง€ ๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ / DFS (์žฅ๊ณ )

๋ฌธ์ œ ๋งํฌ

๋ฌด๋‚œํ•œ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ๋ฌธ์ œ์ด๊ณ  BFS, DFS๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋‚˜๋Š” DFS๋กœ ํ’€์–ด๋ณด์•˜๋‹ค.
์ง€๋„์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ 2์ฐจ์› ๋ฐฐ์—ด map , ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ visited 2์ฐจ์› ๋ฐฐ์—ด, ๊ทธ๋ฆฌ๊ณ  ๋‹ต์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ resVec ๋ฒกํ„ฐ๋ฅผ ์„ ์–ธํ–ˆ๋‹ค.

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

int map[25][25];
int visited[25][25];
vector<int> resVec;

int dy[4] = {-1, 0, 1, 0}; // ์‹œ๊ณ„๋ฐฉํ–ฅ
int dx[4] = {0, 1, 0, -1}; // ์‹œ๊ณ„๋ฐฉํ–ฅ
int n, cnt;

void dfs(int y, int x){
	for(int i = 0; i < 4; i++){
    	int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || nx < 0 || ny > =n || nx > =n) continue;
        if(visited[ny][nx] == 0 && map[ny][nx] == 1){
			// ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ง‘์ด ์žˆ๋‹ค๋ฉด
            visited[ny][nx] == 1; // ๋ฐฉ๋ฌธ ํ‘œ์‹œ
            cnt += 1;
            dfs(ny,nx); // ์žฌ๊ท€
        }
    }
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	int res = 0;
    cin >> n;
    string str;
    
    for (int i = 0; i < n; i++){
    	cin >> str;
        for(int j = 0; j <str.length(); j++){
        	visited[i][j] = 0; // ์ดˆ๊ธฐํ™”
            if(str[j] == '1'){
            	map[i][j] = 1;
            }
            else map[i][j] = 0;
        }
    }
    for (int i = 0; i < n; i++){
    	for(int j = 0; j < n; j++){
        	if(map[i][j] == 1 && visited[i][j] == 0){
            	visited[i][j] = 1;
                cnt = 1; //์ฒ˜์Œ์€ ์‹œ์ž‘์ ์„ ํฌํ•จํ•˜๋ฏ€๋กœ 1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค
                dfs(i,j); // ์‹œ์ž‘
                resVec.push_back(cnt);
                res++; // ๋‹จ์ง€ ๊ทธ๋ฃน 1๊ฐœ ํƒ์ƒ‰ ๋๋‚จ
            }
        }
    }
    
    sort(resVec.begin(), resVec.end()); //์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    cout << res << "\n"; // ๋‹จ์ง€ ๊ฐœ์ˆ˜
    
    for(int i = 0; i < resVec.size(); i++){
    	cout << resVec[i] << "\n";
    }
    return 0;
}

2468 ์•ˆ์ „์˜์—ญ

๋ฌธ์ œ๋งํฌ
์ตœ์†Œ, ์ตœ๋Œ€, ์—†๊ฑฐ๋‚˜, ์žˆ๊ฑฐ๋‚˜ ๋ฐ˜๋ก€๋ฅผ ์ฒดํฌํ•ด์ค˜์•ผ ํ•œ๋‹ค.

#include<bits/stdc++.h>
using namespace std;

int a[101][101], visited[101][101], n, ret = 1; // ๋น„๊ฐ€ ์•ˆ์˜ฌ ๋•Œ๋„ ์ƒ๊ฐํ•ด์„œ ret์„ 1๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.
int dy[4]={-1,0,1,0}, dx[4]={0,1,0,-1};

void dfs(int y, int x, int d)
{
    visited[y][x]=1;
    for(int i=0; i<4; i++)
    {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
        if(!visited[ny][nx] && a[ny][nx] > d) 
            dfs(ny, nx, d);
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cin >> a[i][j];
        }
    }
    // depth ๊ฒ€ํ† 
    for(int d=1; d<101; d++)
    {
        // ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งˆ๋‹ค ์ดˆ๊ธฐํ™”ํ•ด์ค˜์•ผ ํ•œ๋‹ค
        fill(&visited[0][0], &visited[0][0]+ 101*101, 0);
        int cnt = 0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(a[i][j] > d && !visited[i][j])
                {
                    dfs(i, j, d);
                    cnt++;
                }
            }
        }
        ret = max(ret, cnt);
    }
    cout << ret << '\n';
    return 0;
}

2583 ์˜์—ญ ๊ตฌํ•˜๊ธฐ

๋ฌธ์ œ๋งํฌ
๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์‚ฌ๊ฐํ˜•์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์˜์—ญ์„ ๋งŒ๋“ค๊ณ  ๋‚˜๋จธ์ง€ ์˜์—ญ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ตฌํ•ด ๋ฒกํ„ฐ์— ๋‹ด์•„์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

#include<bits/stdc++.h>
using namespace std;
#define y1 aaaa
int a[104][104], m,n,k,x1,x2, y1,y2, visited[104][104];
const int dy[4]={-1,0,1,0};
const int dx[4]={0,1,0,-1};
vector<int> ret;
int dfs(int y, int x) // int ๋ฐ˜ํ™˜ dfs ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค
{
    visited[y][x]=1;
    int ret = 1;
    for(int i=0; i<4; i++)
    {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny<0 || ny>= m || nx<0 || nx>=n || visited[ny][nx]==1) continue;
        if(a[ny][nx]==1) continue;
        ret += dfs(ny, nx);
    }
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> m >> n >> k;
    for(int i=0; i<k; i++)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        for(int x = x1; x < x2; x++)
        {
            for(int y = y1; y < y2; y++)
            {
                a[y][x] = 1;
            }
        }
    }
    // ๋‚˜๋จธ์ง€ ์˜์—ญ์œผ๋กœ๋ถ€ํ„ฐ connected component ์–ป์–ด์˜ค๊ธฐ
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(a[i][j] != 1 && visited[i][j]==0)
            {
                ret.push_back(dfs(i,j));    
            }
        }
    }
    sort(ret.begin(), ret.end());
    cout << ret.size() << '\n';
    for(int a : ret) cout << a << " ";
    return 0;
}

2828 ์‚ฌ๊ณผ ๋‹ด๊ธฐ ๊ฒŒ์ž„

๋ฌธ์ œ๋งํฌ

#include <bits/stdc++.h>
using namespace std;
int n,m,j,l,r,temp,ret;
int main()
{
    cin >> n >> m >> j;
    l=1;
    for(int i=0; i<j; i++)
    {
        r=l+m-1; // ์ƒˆ๋กœ ์ •์˜
        cin >> temp;
        if(temp>=l && temp<=r) continue; // ์‚ฌ๊ณผ๊ฐ€ ๋ฐ”๊ตฌ๋‹ˆ์— ๊ฑธ์น˜๋ฉด continue
        else
        {
        	// l ์ˆ˜์ •
            if(temp<l)
            {
                ret+= (l-temp);
                l=temp;
            }
            else
            {
                l+=(temp-r);
                ret+=(temp-r);
            }
        }
    }
    cout << ret << '\n';
    return 0;
}

10709 ๊ธฐ์ƒ์บ์Šคํ„ฐ

๋ฌธ์ œ๋งํฌ
๋ฌธ์ž๋ฅผ ๊ฒฐ๊ตญ intํ˜• ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ์ด๋‹ค

int a = 0, cnt = 1;
cout << a = cnt++ << endl; // a ๋Š” 1 ์ด๋‹ค. cnt ๋„ 1์ด๋‹ค
cout << cnt << endl; // cnt ๊ฐ€ ์—ฌ๊ธฐ์„œ 2๊ฐ€ ๋œ๋‹ค

int a = 0, cnt = 1;
cout << a = ++cnt << endl; // a ๋Š” 2 ์ด๋‹ค. cnt ๋„ 2์ด๋‹ค

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋„๋ก!!

#include<bits/stdc++.h>
using namespace std;
int n,m,a[104][104]; // H,W,C์ •๋ณด
string s;
int main()
{
    cin >> n >> m;
    for(int i=0; i<n; i++)
    {
        cin >> s;
        for(int j=0; j<m; j++)
        {
            if(s[j]=='.') a[i][j] = -1;
            else a[i][j] = 0;
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(a[i][j]==0){
                int cnt = 1;
                while(a[i][j+1] == -1)
                {
                    a[i][j+1]=cnt++;
                    j++;
                }
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++) cout << a[i][j] << " ";
        cout << '\n';
    }
    return 0;
}

0๊ฐœ์˜ ๋Œ“๊ธ€