728x90

 0. [c++] sw 8382 - 방향 전환


https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWyNQrCahHcDFAVP


 1. 풀이


이 문제에서 포인트는 맨해튼 거리의 합이 홀수와 짝수의 경우를 나누어주는 것이다.


1) 짝수의 경우에는 두 값 중 더 큰 부분에 2를 곱해준다.


2) 홀수의 경우에는 2를 곱한 값에 1을 빼준다.(첫 걸음을 더 많이 걸어야 하는 방향으로 걸어가는 것이라 생각하면 편하다.)



 2. 소스코드


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
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int main(int argc, char** argv)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int test_case;
    int T;
    cin >> T;
 
    pair<intint> dist;
    int a, b, c, d;
 
    for (test_case = 1; test_case <= T; ++test_case)
    {
        cin >> a >> b >> c >> d;
        pair<intint> dist = { abs(a - c), abs(b - d) };
 
        cout << "#" << test_case << " ";
        if((abs(dist.first + dist.second)) % 2 == 0)
            cout << max(dist.first, dist.second) * 2 << "\n";
        else
            cout << max(dist.first, dist.second) * 2 - 1 << "\n";
    }
    return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
cs


 3. 참고



질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


'<SW Expert Academy> > |C++| D4' 카테고리의 다른 글

|C++| sw 7965 - 퀴즈(빠른 제곱)  (0) 2019.07.05

+ Recent posts