※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 13221번 문제인 Manhattan이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/13221 

 

13221번: Manhattan

The island of Manhattan in New York has a grid-like network of streets, where taxis have to travel in a rectilinear fashion along the north, south, east and west cardinal directions. The distance from one intersection to another is often called the taxicab

www.acmicpc.net

택시를 하나하나 조사해보며, 현재 위치에서 (맨해튼 거리 기준으로) 더 가까운 위치의 택시를 발견할 때마다 그 거리와 택시의 위치를 저장해나가는 것으로 문제를 해결하자.

 

이는 반복문과 조건문을 적절히 이용해 구현할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int x, y; cin >> x >> y;
	int dist = 1000000007;
	int ansx, ansy;
	int T; cin >> T;
	while (T--) {
		int xx, yy; cin >> xx >> yy;
		int temp = abs(xx - x) + abs(yy - y);
		if (temp < dist) {
			dist = temp;
			ansx = xx, ansy = yy;
		}
	}

	cout << ansx << ' ' << ansy;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 25755 // C++] 거울반사  (0) 2022.11.23
[BOJ 15474 // C++] 鉛筆 (Pencils)  (0) 2022.11.23
[BOJ 21573 // C++] Кондиционер  (0) 2022.11.22
[BOJ 2712 // C++] 미국 스타일  (0) 2022.11.22
[BOJ 20492 // C++] 세금  (0) 2022.11.22

+ Recent posts