Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
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
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 13228번 The REAL Manhattan distance 본문

YJ/C++

[백준/BOJ/C++] 13228번 The REAL Manhattan distance

Team DAON 2021. 11. 5. 14:36

[문제]

If you remember the preliminary round, we already measured distances in Manhattan. Besides being a grid, Manhattan also has very tall buildings scattered all over the island.

Suppose I want to go to from my apartment that is at position x=3, y=4, floor=3 to my friends apartment, which is x=3, y=5, floor=2. To compute the distance between two points we have to take into account the distance from the my apartment down to the street, the distance to my friend’s building and finally the distance from the street up to my friend’s apartment.

Given two points, please compute the distance in meters we need to walk. We will assume the distance between floors is 1. You should know already that in Manhattan, the street distance between two points is equal to the distance between the x coordinates plus the distance between the y coordinates.

Distance from (3,4,3) to (3,5,2) is 3 (going down the first building) + 0 (moving among x) + 1 (moving among y) + 2 (going up the second building). Result=3+0+1+2=6. We spent more time inside buildings that actually walking on the streets!

[입력]

The first line will contain the number of test cases T. Each of the next T lines will contain 2 points. You have to compute the distance between these two points. Each test cases is a line with 6 integer numbers x1, y1, floor1, x2, y2, floor2. See the examples below. (1 ≤ T ≤ 100, 0 ≤ x, y, floor ≤ 10000)

[출력]

For each test case a single line with the distance between the two points.

[Source Code]

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int test;
	cin >> test;

	for(int i = 0; i < test; i++){
		int x1, y1, f1, x2, y2, f2;
		cin >> x1 >> y1 >> f1
			>> x2 >> y2 >> f2;

		int result = f1 + f2 + abs(x1 - x2) + abs(y1 - y2);
		cout << result << "\n";
	}
}

[결과 화면]