SWEA::문제풀이
3349 최솟값으로 이동하기
2영재
2018. 2. 23. 12:57
3349 최솟값으로 이동하기
1. 다음 좌표에 대해 x,y 좌표 모두 증가하거나 감소하는부분 // 그렇지 않은 부분으로 나눈다.
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 31 32 33 34 35 36 | #include <iostream> #include <algorithm> using namespace std; int t, w, h, n, x, y, xx, yy, res; int main() { scanf("%d", &t); for (int tc = 1; tc <= t; tc++) { res = 0; scanf("%d%d%d%d%d", &w, &h, &n, &x, &y); n--; while (n--) { scanf("%d%d", &xx, &yy); //x,y 둘다 증가하거나 감소하는부분 if ((x < xx && y < yy) || (x > xx&&y > yy)) { if (abs(x - xx) == abs(y - yy)) { res += abs(x - xx); } else if (abs(x - xx) > abs(y - yy)) { res += abs(y - yy) + abs(abs(x - xx) - abs(y - yy)); } else { res += abs(x - xx) + abs(abs(x - xx) - abs(y - yy)); } } //그렇지 않은 부분 else { res += abs(x - xx) + abs(y - yy); } x = xx; y = yy; } printf("#%d %d\n", tc, res); } } | cs |