# 4991. ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ
Study Repository

4991. ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ

by rlaehddnd0422

https://www.acmicpc.net/problem/4991

 

4991๋ฒˆ: ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ

๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๋”๋Ÿฌ์šด ์นธ์„ ๋ชจ๋‘ ๊นจ๋—ํ•œ ์นธ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์ด๋™ ํšŸ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์—†๋Š” ๋”๋Ÿฌ์šด ์นธ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

Point

1. ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ(o)์—์„œ ๋ชจ๋“  ๋”๋Ÿฌ์šด ์นธ(*)์œผ๋กœ ์ด๋™ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ

2. ๋”๋Ÿฌ์šด ์นธ(*)์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 10๊ฐœ

3. ๋”๋Ÿฌ์šด ์นธ์„ ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœ์„œ์— ๋”ฐ๋ผ ๋น„์šฉ์ด ๋‹ฌ๋ผ์ง„๋‹ค.

 

๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœ์„œ์— ๋”ฐ๋ผ ๋น„์šฉ์ด ๋‹ฌ๋ผ์ง„๋‹ค.

 

์“ฐ๋ ˆ๊ธฐ๊ฐ€ 3๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” 3! = 6๊ฐ€์ง€์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค.

์“ฐ๋ ˆ๊ธฐ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 10๊ฐœ์ด๋ฏ€๋กœ ์ตœ๋Œ€ 10! =~ 3,600,000 ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ๋”๋Ÿฌ์šด ์นธ(*)์˜ ์ง€์ ์„ ๋ฒกํ„ฐ์— ๋„ฃ๊ณ  next_permutation์•ˆ์—์„œ bfs๋ฅผ ๋Œ๋ฆฌ๋ฉด ์–ด๋–จ๊นŒ?

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#define FASTio ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define endl '\n' 

using namespace std;
int n,m;
int dy[]={-1,1,0,0};
int dx[]={0,0,-1,1};
int bfs(int n,int m,int y,int x,vector<vector<char>> &area,vector<pair<int,int>>&v)
{
    int sum = 0;
    int starty = y;
    int startx = x;

    for(int i=0;i<v.size();i++)
    {   
        vector<vector<int>> visited(n+1,vector<int>(m+1,0));
        vector<vector<bool>> check(n,vector<bool>(m+1,false));
        queue <pair<int,int>> q;
        
        q.push(make_pair(starty,startx));

        while(!q.empty())
        {   
            int y = q.front().first;
            int x = q.front().second;
            check[y][x] = true;

            q.pop();

            if(y==v[i].first && x==v[i].second)
            {   
                starty = y;
                startx = x;
                sum += visited[y-1][x-1];
            }

            else
            {
                for(int k=0;k<4;k++)
                {
                    int ny = y + dy[k];
                    int nx = x + dx[k];

                    if(ny>=0 && nx>=0 && ny<n && nx<m)
                    {
                        if(area[ny][nx]!='x' && check[ny][nx]==false)
                        {   
                            check[ny][nx]=true;
                            q.push(make_pair(ny,nx));
                            visited[ny][nx] = visited[y][x]+1;
                        }
                    }
                }
            }
        }

    }

    return sum;
}
int k;
int main()
{
    FASTio;

    while(1)
    {
        cin >> m >> n;
        if(n==0 && m==0) break;
        else
        {   
            vector<vector<char>> area(n+1,vector<char>(m+1));

            vector<pair<int,int>> v;
            int ans = 999999;
            int y,x;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin >> area[i][j];
                    if(area[i][j]=='o')
                    {
                        y = i;
                        x = j;
                    }
                    else if(area[i][j]=='*')
                    {
                        v.push_back(make_pair(i,j));
                    }
                }
            }
            do
            {
                ans = min(ans,bfs(n,m,y,x,area,v));

            } while (next_permutation(v.begin(),v.end()));


            if(ans==0) cout << -1 << endl;
            else cout << ans << endl;
        }
    }

    return 0;
}

 

์ด๋ ‡๊ฒŒํ•˜๋ฉด next_permutation ์•ˆ์—์„œ bfs๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(10! * NM(BFS ์ˆ˜ํ–‰์‹œ๊ฐ„))์˜ ์‹œ๊ฐ„์ด ๋ฐœ์ƒํ•˜์—ฌ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

 

๋”ฐ๋ผ์„œ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

Step 1 . dust_dist[0][1~๋”๋Ÿฌ์šด ์นธ ์ˆ˜] => ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ์—์„œ ๊ฐ ๋”๋Ÿฌ์šด์นธ์œผ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๋ฅผ BFS๋กœ ๋ฏธ๋ฆฌ ๊ตฌํ•œ๋‹ค.

Step 2. dust_dist[i][j]์— i์ง€์ ์—์„œ j์ง€์ ์œผ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๋ฅผ BFS๋ฅผ ํ†ตํ•ด ๋ฏธ๋ฆฌ ๊ตฌํ•ด ๋†“๋Š”๋‹ค.

Step 3. next_permutation์„ ํ†ตํ•ด dust_dist์— ์ €์žฅ๋œ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์ •ํ•ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ์ด ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ž์„  ์ฝ”๋“œ๊ฐ€ O(10! * NM)์ด ๊ฑธ๋ ธ๋‹ค๋ฉด ์ด ๋ฐฉ๋ฒ•์—์„œ๋Š” next_permutation์„ ๋Œ๋ฆด ๋•Œ๋งˆ๋‹ค ๋‚ด๋ถ€์—์„œ BFS๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฏธ๋ฆฌ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ด ๋‘”๋‹ค๋Š” ์ ์—์„œ O(10! + NM)์œผ๋กœ ์—„์ฒญ๋‚œ ์‹œ๊ฐ„๊ฐ์†Œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์†Œ์Šค์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#define FASTio ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define endl '\n' 

using namespace std;
int n,m;
int dy[]={-1,1,0,0};
int dx[]={0,0,-1,1};

int bfs(int n, int m, int y,int x,int desty,int destx,vector<vector<char>>&area)
{
    vector<vector<int>> visited(n+1,vector<int>(m+1,0));
    vector<vector<bool>> check(n+1,vector<bool>(m+1,false));
    check[y][x] = true;
    queue <pair<int,int>> q;
        
    q.push(make_pair(y,x));

    while(!q.empty())
    {   
        int y = q.front().first;
        int x = q.front().second;
        q.pop();

        if(y==desty && x==destx)
        {   
            break;
        }
        else
        {
            for(int k=0;k<4;k++)
            {
                int ny = y + dy[k];
                int nx = x + dx[k];

                if(ny>=0 && nx>=0 && ny<n && nx<m)
                {
                    if(area[ny][nx]!='x' && check[ny][nx]==false)
                    {   
                        check[ny][nx]=true;
                        q.push(make_pair(ny,nx));
                        visited[ny][nx] = visited[y][x]+1;
                    }
                }
            }
        }
    }

    return visited[desty][destx];
}

int main()
{
    FASTio;

    while(1)
    {
        cin >> m >> n;
        if(n==0 && m==0) break;
        else
        {   
            vector<vector<char>> area(n+1,vector<char>(m+1));
            vector<pair<int,int>> v(1);

            int ans = -1;

            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin >> area[i][j];
                    if(area[i][j]=='o')
                    {
                        v[0] = make_pair(i,j);
                    }

                    else if(area[i][j]=='*')
                    {   
                        v.push_back(make_pair(i,j));
                    }
                }
            }

            vector<vector<int>> dust_dist(v.size(),vector<int>(v.size(),0));

            // ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ -> *๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๋ชจ๋‘ ๊ณ„์‚ฐํ•ด์„œ ์ €์žฅ
            for(int i=1;i<v.size();i++)
            {
                int dist = bfs(n,m,v[0].first,v[0].second,v[i].first,v[i].second,area); 
                dust_dist[0][i] = dist;
            }
        
            // *๋“ค ๊ฐ„์˜ ๊ฑฐ๋ฆฌ
            for(int i=1;i<v.size();i++)
            {
                for(int j=1;j<v.size();j++)
                {
                    dust_dist[i][j] = bfs(n,m,v[i].first,v[i].second,v[j].first,v[j].second,area);
                }
            }

            vector<int> order(v.size()-1);

            for(int i=0;i<order.size();i++)
            {
                order[i] = i+1;
            }

            sort(order.begin(),order.end());
            
            do
            {   
                bool itcant = false;

                int sum = dust_dist[0][order[0]];


                // ์ฒซ๋ฒˆ์งธ *๋กœ ๋ชป๊ฐ€๋ฉด ๋‹ค์Œ ์กฐํ•ฉ ์‚ดํŽด๋ณด๊ธฐ
                if(sum==0)
                {
                    continue;
                }


                for(int i=0;i<order.size()-1;i++)
                {   
                    // * -> * ๋ชป๊ฐ€๋ฉด ์ข…๋ฃŒ
                    if(dust_dist[order[i]][order[i+1]]==0)
                    {
                        itcant = true;
                        break;
                    }

                    // sum์— ๋จผ์ง€๊ฑฐ๋ฆฌ ๋”ํ•ด์ฃผ๊ธฐ
                    sum += dust_dist[order[i]][order[i+1]];
                }    

                if(itcant==true)
                {
                    continue;
                }


                else
                {  
                   
                   if(ans==-1 || ans>sum)
                   {
                   ans = sum;
                   }
                }

            } while (next_permutation(order.begin(),order.end()));

            cout << ans << endl;
        }

    }


    return 0;
}

 

 

 

๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ณด๊ณ  ๋‚˜์„œ๋Š” ์‰ฌ์›Œ๋ณด์˜€๋Š”๋ฐ, ํ’€๋ฉด์„œ ์‰ฝ์ง€ ์•Š๋‹ค๋Š” ๊ฑธ ๋Š๊ผˆ๋‹ค.

์ฒ˜์Œ์—” ๊ตฌํ˜„ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์ƒ๊ฐํ•œ ๋Œ€๋กœ ๋Œ์•„๊ฐ€์ง€ ์•Š์•„ ์–ด๋ ค์› ๊ณ  ๊ตฌํ˜„ ๋์—๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์–ด๋–ป๊ฒŒ ์‹œ๊ฐ„์„ ์ค„์—ฌ์•ผ ํ• ์ง€ ๊ฐ์ด ์˜ค์ง€ ์•Š์•˜๋‹ค. ๊ตฌํ˜„ํ•˜๋Š” ์—ฐ์Šต์„ ๋งŽ์ด ํ•ด๋ณด๊ณ , ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋งŽ์ด ๊ณ ๋ฏผํ•ด๋ด์•ผ๊ฒ ๋‹ค...

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ