Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 문제풀이
- uclidean algorithm
- Vector
- Expanding Polytope Algorithm
- 내적
- 리눅스
- 분할축 이론
- 충돌 알고리즘
- C
- SOH
- 수학
- AABB
- 유니티
- C++
- Graham Scan
- 다이나믹 프로그래밍
- 벡터
- GJK
- PS
- ubuntu
- 외적
- Unity
- dp
- 보로노이다이어그램
- linux
- 백준
- 알고리즘
- Doubly Connected Edge List
- c#
- 우분투
Archives
- Today
- Total
마이 플밍 블로그
[C++] 백준 1261 - 알고스팟 본문
1261번: 알고스팟
첫째 줄에 미로의 크기를 나타내는 가로 크기 M, 세로 크기 N (1 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 미로의 상태를 나타내는 숫자 0과 1이 주어진다. 0은 빈 방을 의미하고, 1은 벽을 의미
www.acmicpc.net
풀이
BFS로 풀고 flag로 벽을 부신 횟수를 넣으면 된다.
코드
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int board[101][101];
int flag[101][101];
int n, m;
int minAnswer = 1000000000;
bool InMap(int x,int y){
if(0 <= x && x < m && 0 <= y && y < n)
return true;
return false;
}
void BFS(){
queue<pair<int,pair<int,int>>> s;
s.push(make_pair(1,make_pair(0,0)));
while(!s.empty()){
int c = s.front().first;
int x = s.front().second.first;
int y = s.front().second.second;
s.pop();
if(!InMap(x,y))
continue;
if(minAnswer <= c)
continue;
if(flag[y][x] != 0 && flag[y][x] <= c)
continue;
//cout << x << " " << y << " " << c << endl;
if(x == m-1 && y == n-1){
minAnswer = min(c, minAnswer);
flag[y][x] = minAnswer;
continue;
}
flag[y][x] = c;
for(int i =0;i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(!InMap(nx,ny))
continue;
if(board[y][x] == 1)
s.push(make_pair(c+1,make_pair(nx,ny)));
else
s.push(make_pair(c,make_pair(nx,ny)));
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> m >> n;
for(int y = 0;y < n; y++){
string a;
cin >> a;
for(int i = 0; i < a.length(); i++){
board[y][i] = ((int)a[i]) - ((int)'0');
}
}
BFS();
cout << minAnswer-1;
return 0;
}
'문제풀이 > 백준' 카테고리의 다른 글
[C++] 백준 18223 - 민준이와 마산 그리고 건우 (1) | 2023.10.09 |
---|---|
[C++] 백준 1504 - 특정한 최단 경로 (1) | 2023.10.09 |
[C++] 백준 11779 - 최소비용 구하기 2 (1) | 2023.10.08 |
[C++] 백준 2234 - 성곽 (1) | 2023.10.07 |
[C++] 백준 14442 - 벽 부수고 이동하기 2 (1) | 2023.10.07 |