# 2151. ๊ฑฐ์šธ์„ค์น˜
Study Repository

2151. ๊ฑฐ์šธ์„ค์น˜

by rlaehddnd0422

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

2151๋ฒˆ: ๊ฑฐ์šธ ์„ค์น˜

์ฒซ์งธ ์ค„์— ์ง‘์˜ ํฌ๊ธฐ N (2 โ‰ค N โ‰ค 50)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” N๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ง‘์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. โ€˜#โ€™๋Š” ๋ฌธ์ด ์„ค์น˜๋œ ๊ณณ์œผ๋กœ ํ•ญ์ƒ ๋‘ ๊ณณ์ด๋ฉฐ, โ€˜.โ€™์€ ์•„๋ฌด ๊ฒƒ๋„ ์—†๋Š” ๊ฒƒ์œผ๋กœ ๋น›์€

www.acmicpc.net

 
 

 

 
 

Point

  •  "#" โ–ถ๏ธŽ ๋ฌธ
  •  "!" โ–ถ๏ธŽ ๊ฑฐ์šธ ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ๊ตฌ์—ญ
  •  "*" โ–ถ๏ธŽ ๋ฒฝ
  • ๊ฑฐ์šธ์„ 45๋„ ๊ธฐ์šธ์–ด์ง„ ๋Œ€๊ฐ์„ ์œผ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค.
  • ๋ฌธ์—์„œ ๋ฌธ์„ ๋ณด๊ธฐ ์œ„ํ•ด ๊ฑฐ์šธ์„ ์„ค์น˜ํ•  ๋•Œ, ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š” ๊ฑฐ์šธ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 1๋ฒˆ์˜ ๊ฒฝ์šฐ์—๋Š”

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 1๋ฒˆ ๊ทธ๋ฆผ

๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฑฐ์šธ์„ ์„ค์น˜ํ•˜๋ฉด ๋ฌธ์—์„œ ๋ฌธ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
 

 

 

Point1 )

์šฐ์„  ๊ฑฐ์šธ๊ณผ ๋ฌธ์— ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ คํ•œ๋‹ค. 
๋ฌธ์˜ ์‹œ์ž‘๋ฒˆํ˜ธ์™€ ๋๋ฒˆํ˜ธ๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•ด๋‘๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฌธ๊ณผ ๊ฑฐ์šธ ๋ชจ๋‘ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋‹ค ( ์ฝ”๋“œ ์ฐธ์กฐ )
 

Point2)

 
dist[Start์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์šธ๋ฒˆํ˜ธ]์— 0๊ฐ’์ €์žฅ , ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฒˆํ˜ธ๋“ค push 
->  pop -> dist[popํ•œ ๊ฑฐ์šธ๋ฒˆํ˜ธ์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฒˆํ˜ธ]์— +1 ์ €์žฅ, ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฒˆํ˜ธ๋“ค push 
๋กœ์ง์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ 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 dy[]={-1,1,0,0};
int dx[]={0,0,-1,1};

int n;
vector<vector<char>> area(51,vector<char>(51));
vector<pair<int,int>> mirror;
vector<vector<int>> mirrorNum(51,vector<int>(51));
vector<vector<int>> doorNum(51,vector<int>(51));
vector<pair<int,int>> door;
int start=-1;
int dest=-1;

void bfs(int start,vector<int>&dist, vector<vector<bool>>&check)
{
    queue<int>q;
    q.push(start);
	
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        for(int i=0;i<mirror.size();i++)
        {	
        
        	// ํ˜„์žฌ ๋ฒˆํ˜ธ์—์„œ i๋ฒˆํ˜ธ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๊ณ , dist์— ๊ธฐ๋ก๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด 
            if(check[now][i] && dist[i]==-1)
            {   
                dist[i] = dist[now]+1;
                // cout << i << ' ' << dist[i] << endl;
                q.push(i);
            }
        }
    }
}
int main()
{
    FASTio;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin >> area[i][j];
			
            
            // ๋ฌธ์ธ ๊ฒฝ์šฐ -> ์‹œ์ž‘์ ๊ณผ ๋์ ๋งŒ ๊ธฐ๋กํ•œ๋‹ค๋ฉด ๊ฑฐ์šธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ทจ๊ธ‰ํ•ด๋„ ๋จ.
            if(area[i][j]=='#')
            {	
            
            	// ์‹œ์ž‘๋ฒˆํ˜ธ ๊ธฐ๋ก
                if(start==-1)
                {
                    start = mirror.size();
                }
                
                // ๋๋ฒˆํ˜ธ๊ธฐ๋ก
                else
                {
                    dest = mirror.size();
                }
                mirror.push_back({i,j});
                
                // ์ขŒํ‘œ์— ๊ฑฐ์šธ ๋ฒˆํ˜ธ ๊ธฐ๋ก
                mirrorNum[i][j] = mirror.size()-1;
            }

            else if(area[i][j]=='!')
            {	
                mirror.push_back({i,j});
                
                // ์ขŒํ‘œ์— ๊ฑฐ์šธ ๋ฒˆํ˜ธ ๊ธฐ๋ก 
                mirrorNum[i][j] = mirror.size()-1;
            }
        }
    }


    vector<vector<bool>>check(mirror.size(),vector<bool>(mirror.size(),false));
	
    
    for(int i=0;i<mirror.size();i++)
    {
        for(int k=0;k<4;k++)
        {
        	// ๊ฑฐ์šธ ๋ฒˆํ˜ธ๋ฅผ ํ†ตํ•ด์„œ ๊ฑฐ์šธ ์ขŒํ‘œ๋ฅผ ๋ฝ‘์•„๋ƒ„
            int ny = mirror[i].first + dy[k];
            int nx = mirror[i].second + dx[k];
            
            while(1)
            {	
            	// ์˜์—ญ ๋ฐ–์ธ๊ฒฝ์šฐ break
                if(!(ny>=0 && nx>=0 && nx<n && ny<n))
                {  
                    break;
                }
				
                // ๋ฒฝ์„ ๋งŒ๋‚œ ๊ฒฝ์šฐ break
                if(area[ny][nx]=='*')
                {
                    break;
                }
				
                // ๊ฑฐ์šธ์ด๋‚˜ ๋ฒฝ์„ ๋งŒ๋‚œ๊ฒฝ์šฐ
                if(area[ny][nx]=='#' || area[ny][nx]=='!')
                {	
                	// i๋ฒˆ์—์„œ mirrorNum[ny][nx] ๋ฒˆ์งธ ๊ฑฐ์šธ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•จ
                    check[i][mirrorNum[ny][nx]] = true;
                    // cout << i << ' ' << mirrorNum[ny][nx] << endl;
                }
				
                // ์กฐ๊ฑด ๋‚ด์—์„œ
                // ์œ„๋๊นŒ์ง€ ์‚ดํ•Œ-> ์•„๋ž˜๋๊นŒ์ง€์‚ดํ•Œ -> ์ขŒ๋๊นŒ์ง€ ์‚ดํ•Œ -> ์šฐ ๋๊นŒ์ง€ ์‚ดํ•Œ
                ny += dy[k];
                nx += dx[k];
            }
        }
    }
    
    vector<int> dist(mirror.size(),-1);

	
    bfs(start,dist,check);
    
    cout << dist[dest];


  
    return 0;
}

 

 
 
๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ๊ต‰์žฅํžˆ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.. ๋ฌธ์ œ ์ดํ•ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฑฐ์šธ์„ 45๋„๋กœ ์„ค์น˜ํ•œ๋‹ค๋Š” ์ ์—์„œ ๊ฑฐ์šธ์„ ์–ด๋–ป๊ฒŒ ์„ค์น˜ํ•ด์•ผ ๋˜๋Š”์ง€ ์•„์ด๋””์–ด๋„ ์•ˆ ๋– ์˜ฌ๋ผ์„œ ๋‚œ๊ฐํ–ˆ๋‹ค. ๊ฐ•์˜๋ฅผ ๋ณด๊ณ  ๊ฒจ์šฐ ๋ฌธ์ œ๋Š” ๊พธ์—ญ๊พธ์—ญ ์ดํ•ดํ–ˆ์ง€๋งŒ ํ’€์ด๋ฅผ ๋ณด๊ณ ๋‚˜์„œ๋Š” ์ด๋Ÿฐ ์•„์ด๋””์–ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋– ์˜ฌ๋ฆฌ๋Š”์ง€ ๊ฐํƒ„๋งŒ ๋‚˜์™”๋‹ค. ์•ž์œผ๋กœ๋Š” ์กฐ๊ธˆ ๋” ๊ณ ๋ฏผํ•˜๊ณ  ์ƒ๊ฐํ•ด๋ณด๊ณ  ๊ตฌํ˜„ํ•ด๋ณด๋Š” ์—ฐ์Šต์„ ํ•ด์•ผ๊ฒ ๋‹ค..
 

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

Study Repository

rlaehddnd0422

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