python

프로그래머스/Level1

[Level 1] 소수 찾기 (C++/Python)

C++) #include #include #include using namespace std; bool visited[1000003]; int solution(int n) { int answer = 0; for(int i=2; i

백준 2/그래프

[백준 4179] 불! (C++/Python)

C++) #include #include #include #define MAX_NUM 1002 using namespace std; string arr[MAX_NUM]; int visited[MAX_NUM][MAX_NUM]; queue jq; queue fq; int dx[4] = { 1,0,-1,0 }; int dy[4] = { 0,1,0,-1 }; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int r, c; cin >> r >> c; for (int i = 0; i > arr[i]; for (int j = 0; j < c; j++) { if (arr[i][j] == 'J') { jq.push..

백준 2/그래프

[백준 2206] 벽 부수고 이동하기 (C++/Python)

1. 조건 - 0 : 이동 가능 / 1 : 이동 불가능 - (1,1) → (N,M) 으로 이동 - 한개까지 벽 부시기 가능 - (1,1), (N,M)은 항상 0 2. 입력 - 가로 : M, 세로 : N 3. 출력 - 최단거리 출력하되 불가능하면 -1 4. 경계값 반례 1 1 1 // 1 1 1 0 // 1 3 4 0111 1000 0000 // 6 3 4 0111 0000 0000 // 6 5. 풀이 - 0층은 벽을 안 부쉈을 때, 1층은 벽을 한개 부쉈을 때로 가정해 [2][MAX_NUM][MAX_NUM] 배열을 만든다. - (0,0)은 무조건 0이므로 0층 [0][0]에서 탐색을 시작한다. - (N-1, M-1)까지 도달할 수 있는 조건은 3가지이다. ㄴ 1) 벽을 한번도 안 부쉈고 가야할 곳도 벽이..

백준 2/그래프

[백준 5427] 불 (C++/Python)

1. 조건 - 동서남북 - 벽에 불 못 번진다. - 상근이는 벽을 통과할 수 없다. - 불이 옮겨진 칸 또는 붙으려는 칸으로 이동할 수 없다. ㄴ 불을 먼저 번지게 한 이유 2. 입력 - 1 = w) continue; // 상근이가 이미 간 곳이라면 굳이 갈 필요가 없다 -> sang[nx][ny] == 0 넣은 이유 if ((arr[nx][ny] == '.' || arr[nx][ny] == '@') && (fire[nx][ny] == 0 && sang[nx][ny] == 0)) { fire[nx][ny] = fire[x][y] + 1; fq.push({ nx,ny }); } } } // 상근이 탈출중 int sqSize = sq.size(); for (int i = 0; i < sqSize; i++) ..

백준 2/그래프

[백준 2644] 촌수계산 (C++/Python)

C++) #include #include #include #include #define MAX_NUM 101 using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, a, b, m, x, y; cin >> n; cin >> a >> b; vector v(n + 1); cin >> m; for (int i = 0; i > x >> y; v[x].push_back(y); v[y].push_back(x); } int visited[MAX_NUM]; fill(visited, visited + MAX_NUM, -1); queue q; visited[a] = 0; q.p..

백준 2/그래프

[백준 10026] 적록색약 (C++/Python)

C++) #include #include #include #define MAX_NUM 102 using namespace std; int dx[4] = { 1,0,-1,0 }; int dy[4] = { 0,1,0,-1 }; string arr[MAX_NUM]; bool visited[MAX_NUM][MAX_NUM]; // 적록색약 int RGB(int n, bool flag) { queue q; int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (!visited[i][j]) { visited[i][j] = true; q.push( {i,j} ); cnt++; while (!q.empty()) { int x = q.fro..

백준 2/그래프

[백준 2468] 안전영역 (C++/Python)

C++) #include #include #include #include #include #define MAX_NUM 102 using namespace std; int dx[4] = { 1,0,-1,0 }; int dy[4] = { 0,1,0,-1 }; int arr[MAX_NUM][MAX_NUM]; bool visited[MAX_NUM][MAX_NUM]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int maxHeight = -987654321; for (int i = 0; i > arr[i][j]; maxH..

백준 2/그래프

[백준 2583] 영역 구하기 (C++/Python)

C++) #include #include #include #include using namespace std; int arr[102][102]; int visited[102][102]; int dx[4] = { 1,0,-1,0 }; int dy[4] = { 0,1,0,-1 }; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int m, n, k; cin >> n >> m >> k; int x1, y1, x2, y2; while (k--) { cin >> y1 >> x1 >> y2 >> x2; for (int i = x1; i < x2; i++) { for (int j = y1; j < y2; j++) { arr[i][j] = 1..

백준 2/그래프

[백준 1697] 숨바꼭질 (C++/Python)

C++) #include #include #include #define MAX_SIZE 100002 using namespace std; queue q; int ans[MAX_SIZE]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; q.push(n); while (!q.empty()) { int cur = q.front(); q.pop(); if (cur == k) break; if (cur - 1 >= 0 && ans[cur - 1] == 0) { ans[cur - 1] = ans[cur] + 1; q.push(cur - 1); } if (cur + 1

백준 2/그래프

[백준 2606] 바이러스 (C++/Python)

C++) #include #include #include #include #define MAX_NUM 101 using namespace std; bool visited[MAX_NUM]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, x, y; cin >> n; cin >> m; vector v(n+1); for (int i = 0; i > x >> y; v[x].push_back(y); v[y].push_back(x); } queue q; visited[1] = true; q.push(1); int ans = 0; while (!q.empty()) { int cur = q.fro..

핑구ovo
'python' 태그의 글 목록 (6 Page)