16948번 데스 나이트

동도리동·2021년 8월 13일
0

코딩테스트

목록 보기
12/76

어려운 문제만 풀다가 쉬운 문제를 푸니까 너무 좋다.
백준풀면서 처음으로 17분만에 풀었다ㅎㅎ 아직 갈길이 멀지만 예전보다는 늘은 느낌이 든다. 굿~!

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
//int map[201][201];
int dist[201][201];
int dx[6] = { -2,-2,0,0,2,2 };
int dy[6] = { -1,1,-2,2,-1,1 };
struct Loc {
	int x, y;
	Loc(int a, int b) {
		x = a;
		y = b;
	}
};
int main() {
	ios_base::sync_with_stdio(false);
	//freopen("in1.txt", "rt", stdin);
	int n;
	cin >> n;
	vector<vector<int>> map(n + 1, vector<int>(n + 1));
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			map[i][j] = 2147000000;
		}
	}
	queue<Loc> Q;
	Q.push(Loc(x1, y1));
	map[x1][y1] = 0;
	while (!Q.empty()) {
		Loc tmp = Q.front();
		Q.pop();
		if (tmp.x == x2 && tmp.y == y2) {
			cout << map[x2][y2] << '\n';
			return 0;
		}
		for (int i = 0; i < 6; i++) {
			int xx = tmp.x + dx[i];
			int yy = tmp.y + dy[i];
			if (xx >= n || xx<0 || yy >=n || yy < 0) continue;
			if (map[xx][yy] > map[tmp.x][tmp.y]+1) {
				map[xx][yy] = map[tmp.x][tmp.y] + 1;
				Q.push(Loc(xx, yy));
			}
		}
	}
	cout << -1 << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보