티스토리 뷰
#. Problem
* The copyright in this matter is in Inflearn
#. Resolution Process
1. Read and understand problem
2. Redefine the problem + abstract
- 블록의 정면과 오른쪽 측면의 높이 정보를 가지고 블럭의 최대 개수 구하기
3. Create solution plan (select Algorithm, Data structure)
4. Prove the plan (check performance time and usage memory)
5. Carry out the plan
6. Look back on the plan and find a way to improve it
#. Solve
1. 입력이
4
2 0 3 1
1 1 2 3
으로 주어졌을 때,
우선 N * N 크기의 2차원 배열을 만들어두고,
앞 면으로 보았을 때 최대의 블록 개수를 쌓아보자.
2 0 3 1
2 0 3 1
2 0 3 1
2 0 3 1
이 상태에서 오른쪽으로 보았을 때 최대의 블록 개수를 다시 쌓아보자
2 0 3 1 (3, 뒤에 3이 있으니 pass)
2 0 3 1 (2, 2보다 큰 층은 2층으로 줄여주자)
2 0 3 1 (1, 1보다 큰 층은 1층으로 줄여주자)
2 0 3 1 (1, 1보다 큰 층은 1층으로 줄여주자)
2 0 3 1
2 0 2 1
1 0 1 1
1 0 1 1
마무리로 이차원배열에 있는 모든 원소를 더해주면 되겠다!
이 과정을 코드로 옮겨보자!
#. Code
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; int block[10][10]; int main(void) { freopen("input.txt", "rt", stdin); int n, i, j, tmp, sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &tmp); for (j = 0; j < n; j++) block[j][i] = tmp; } for (i = n - 1; i >= 0; i--) { scanf("%d", &tmp); for (j = n - 1; j >= 0; j--) { if (block[i][j] > tmp) block[i][j] = tmp; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) sum += block[i][j]; } printf("%d\n", sum); return 0; } | cs |
입력 받자마자 바로 처리해버리기 위해 나름 노력해보았다. ㅎ_ㅎ
#. Other code
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 31 32 33 34 35 | #include<stdio.h> int a[11][11], b[11]; int main(){ int n, i, j, sum=0; scanf("%d", &n); for(i=1; i<=n; i++) scanf("%d", &b[i]); for(i=1; i<=n; i++){ for(j=1; j<=n; j++){ a[j][i]=b[i]; } } for(i=n; i>=1; i--) scanf("%d", &b[i]); for(i=1; i<=n; i++){ for(j=1; j<=n; j++){ if(a[i][j]>b[i]) a[i][j]=b[i]; } } for(i=1; i<=n; i++){ for(j=1; j<=n; j++){ sum+=a[i][j]; } } printf("%d\n", sum); return 0; } | cs |
강사님과 로직이 똑같으면 뭔가 100점 맞은 것 처럼 기분이 좋다.
Yeah~~!
#. Result
- Input --------------------------------------------------------
4
2 0 3 1
1 1 2 3
------------------------------------------------------------------
- Output --------------------------------------------------------
17
------------------------------------------------------------------
'PS > Problem_Solving' 카테고리의 다른 글
[Inflearn] 올바른 괄호(stack) (0) | 2020.04.29 |
---|---|
[Inflearn] K진수 출력 (0) | 2020.04.29 |
[Inflearn] 각 행의 평균과 가장 가까운 값 (0) | 2020.04.28 |
[Inflearn] 봉우리 (0) | 2020.04.28 |
[Inflearn] 멀티태스킹 (0) | 2020.04.28 |