-
Softeer [level 3] : 동계 테스트 시점 예측 (C 언어)softeer 문제 2023. 4. 7. 15:44
https://softeer.ai/practice/info.do?idx=1&eid=411&sw_prbl_sbms_sn=172894
Softeer
연습문제를 담을 Set을 선택해주세요. 취소 확인
softeer.ai
Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139#include <stdio.h>int n, m;int arr[101][101] = { 0, };int check[101][101] = { 0, };int flag = 1;int dx[] = { -1, 1, 0, 0 };int dy[] = { 0, 0, - 1, 1 };void copy(){flag = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){arr[i][j] = check[i][j];if (check[i][j] == 1){flag = 1;}}}}void look(int a, int b){int count = 0;if (arr[a][b - 1] == 2){count ++ ;}if (arr[a + 1][b] == 2){count++;}if (arr[a - 1][b] == 2){count++;}if (arr[a][b + 1]==2){count++;}if (count >= 2){check[a][b] = 0;}}void dfs(int a, int b){for (int k = 0; k < 4; k++){int y = a + dy[k];int x = b + dx[k];if (y >= 0 && y < n && x >= 0 && x < m && arr[y][x] == 0){arr[y][x] = 2;dfs(y, x);}}}int main(){int startcheck = 0;scanf("%d %d", &n, &m);for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){scanf("%d", &arr[i][j]);check[i][j] = arr[i][j];if (arr[i][j] == 1){startcheck = 1;}}}int cnt = 0;int flag2 = 0;int flag3 = 0;if (startcheck == 0){cnt = 0;}else{while (flag == 1){cnt++;flag2 = 0;if (flag2 == 0){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (arr[i][j] == 0){dfs(i, j);flag2 = 1;flag3 = 1;break;}}if (flag3 == 1){break;}}}for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (arr[i][j] == 1){look(i, j);}}}copy();}}printf("%d", cnt);}/*7 90 0 0 0 0 0 0 0 00 1 1 0 0 0 1 1 00 1 0 1 1 1 0 1 00 1 0 0 1 0 0 1 00 1 0 1 1 1 0 1 00 1 1 0 0 0 1 1 00 0 0 0 0 0 0 0 0*/cs 문제풀이:
1. 외부공기를 2 내부공기를 0 얼음을 1로 설정하였다.
2. arr와 check 2개의 배열을 만들어 arr로 조건을 판단하고 check값을 바꾼 뒤 2중 for문을 한바퀴 돌면 copy()에 들어가 check값을 arr로 넣어준다.
밑에 주석은 문제에 나와있는 두번 째 예제이다. 결과값은 3이 나와야 한다.
'softeer 문제' 카테고리의 다른 글
Softeer [level 3] : 슈퍼컴퓨터 클러스터 (C 언어) (0) 2023.06.02 Softeer [level 3] : 스마트 물류 (C 언어) (0) 2023.04.04 Softeer [level 3] : 출퇴근길 (C++ 언어) (0) 2023.04.03 Softeer [level 3] 징검다리 (C 언어) (0) 2023.04.01 Softeer [level 3] : 택배 마스터 광우 (C 언어) (0) 2023.03.31