[백준] C++ 2468번:안전영역!
·
알고리즘
생각할 것1. 모든 경우에 가장 많은 안전구역이 생기는 때를 찾으려면 -> for (min ... max) 순회하여 BFS 한다. #include using namespace std;int dy[4] = {1, 0, -1, 0};int dx[4] = {0, 1, 0, -1};int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector> v (n, vector(n, -1)); int min = 0, max = 0; for (int y = 0; y > v[y][x]; if (v[y][x] max) { max = v[y][x]; } ..
[백준] C++ 5014번:스타트링크!
·
알고리즘
생각할 것1. 벡터 선언 범위2. 인덱스 시작이 0인지 1인지 확인 #include using namespace std;int main(){ ios::sync_with_stdio(0); cin.tie(0); int f, s, g, u, d; cin >> f >> s >> g >> u >> d; vector v (f + 1, -1); queue q; if (s == g) { cout 0 && ss 0 && ss
[백준] C++ 2667번:단지번호붙이기!
·
알고리즘
생각할 것1. 1개씩 받는 입력법2. 면적 측정 #include using namespace std;int dy[4] = {1, 0, -1, 0};int dx[4] = {0, 1, 0, -1};int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector> v (n, (vector (n, -1))); vector> vis (n, (vector (n, 0))); for (int y = 0; y > ch; v[y][x] = ch - '0'; } } vector areas; int cnt = 0; queue> q; for (int y = 0; y ..
[백준] C++ 2583번:영역 구하기!
·
알고리즘
생각할 것1. 시작 정점과 끝 정점을 for문의 시작과 끝으로 설정2. 방문 표시 하기 #include using namespace std;int dy[4] = {1, 0, -1, 0};int dx[4] = {0, 1, 0, -1};int main(){ ios::sync_with_stdio(0); cin.tie(0); int mx, my, sq; cin >> mx >> my >> sq; vector> v (my, (vector (mx, 0))); vector> vis (my, (vector (mx, 0))); for (int i = 0; i > sy >> sx; cin >> ey >> ex; for (int n = sy; n area..
[백준] C++ 5427번: 불!
·
알고리즘
생각할 점1. 불이 붙으려는 공간으로 이동할 수 없다 -> 불을 먼저 이동 시키고 상근이를 이동시킨다.2. 불 확산조건, 상근이 이동 조건 고려3. 탈출 판정 #include using namespace std;int dy[4] = {1, 0, -1, 0};int dx[4] = {0, 1, 0, -1};int main(){ ios::sync_with_stdio(0); cin.tie(0); int tk; cin >> tk; for (int r = 0; r > nx >> ny; vector> v (ny, vector (nx)); queue> q; tuple tu; vector> dist (ny, vector (nx, -1)); ..
[백준] C++ 7562번:나이트의 이동!
·
알고리즘
생각할 점1. 다양한 이동 범위 -> dx, dy 배열에 추가2. 몇 번째 이동인지? -> v 벡터에 (다음 이동할 영역 값 ) = (현재 영역 값) + 1 #include using namespace std;int dy[8] = {2, 1, -1, -2, -2, -1, 1, 2};int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1};int main(){ ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (int r = 0; r > n; vector> v (n, vector(n, -1)); queue> q; int y, x; cin >> y >> x; ..
[백준] C++ 7569번:토마토!
·
알고리즘
#include using namespace std;int dx[6] = {1, 0, -1, 0, 0, 0};int dy[6] = {0, 1, 0, -1, 0, 0};int dz[6] = {0, 0, 0, 0, 1, -1};int main(){ ios::sync_with_stdio(0); cin.tie(0); int x, y, z; cin >> x >> y >> z; vector>> v (z, vector>(y, vector(x, -1))); vector>> vis (z, vector>(y, vector(x, 0))); //1: 익은토마토, 0: 익지 않은 토마토, -1: 토마토 없음 for (int zz = 0; zz > v[zz][yy][xx]..
[백준] C++ 10026번:적록색약
·
알고리즘
기존 BFS에서 1로 탐색 지점을 표시했다면 이번에는 RGB중 1개 이므로영역이 같은지는 아래와 같이 판단한다if (v[cur.first][cur.second] == v[yy][xx]) //if(현재영역 값 == 너비탐색하는영역 값) 이 문제에서는 적록색약 사람의 관점도 요구하기 때문에 for로 G를 R로 바꾸어 BFS를 재활용한다. #include using namespace std;int dx[4] = {1, 0 , -1, 0};int dy[4] = {0, -1, 0, 1};int n;void bfs(vector> v) { vector> vis (n, vector(n, 0)); queue> q; int cnt = 0; for (int y = 0; y -1 && yy -1 &&..