DdoubleJ
2018. 8. 9. 11:48
대각선으로 갈 수 있는 만큼가고, 나은 만큼 이동한다
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 | #include <stdio.h> #include <stdlib.h> int main(void) { int T=10; scanf("%d\n", &T); for (int test_case = 1; test_case <= T; test_case++) { int w, h, n; scanf("%d%d%d", &w,&h,&n); int prevx, prevy; scanf("%d%d", &prevx,&prevy); int cnt = 0; for (int i = 0; i < n - 1; i++) { int nextx, nexty; scanf("%d%d", &nextx,&nexty); //x,y가 둘다 증가한만큼 + 나머지 x or y 더해줌 //둘다 증가하지 않으면 각각의 차이를 더해줌 if (nextx - prevx > 0 && nexty - prevy > 0) { int val = (nextx - prevx > nexty - prevy) ? nexty - prevy : nextx - prevx;//더 작은 값을 넣음 cnt += val; cnt = cnt + (nextx - prevx) - val + nexty - prevy - val; } else if (nextx - prevx < 0 && nexty - prevy < 0) {//둘다 감소할 때에도 int val = (abs(nextx - prevx) > abs(nexty - prevy)) ? abs(nexty - prevy) : abs(nextx - prevx);//더 작은 값을 넣음 cnt += val; cnt = cnt + abs(nextx - prevx) - val + abs(nexty - prevy) - val; } else { cnt = cnt + abs(nextx - prevx) + abs(nexty - prevy); } prevx = nextx; prevy = nexty; } printf("#%d %d\n", test_case, cnt); } return 0; } | cs |