19238번 스타트 택시

동도리동·2021년 10월 18일
0

코딩테스트

목록 보기
65/76

예외처리가 좀 어려웠다..

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
int ans = -1;
struct Loc {
	int dis, x, y;
	Loc(int a, int b, int c) {
		x = a;
		y = b;
		dis = c;
	}
	bool operator<(const Loc& b) const {
		if (dis != b.dis) return dis > b.dis;
		else if (x != b.x) return x > b.x;
		return y > b.y;
	}
};
struct taxi {
	int x, y, oil;
};
int map[22][22];
int ch[22][22];
int n, m;
taxi jun;
vector<pair<int, int> > son;
vector<pair<int, int> > dest;
bool go(int a, int b, int k) {
	vector<vector<int> > ch(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			ch[i][j] = -1;
		}
	}
	queue<pair<int, int> > Q;
	Q.push(make_pair(a, b));
	ch[a][b] = 0;
	while (!Q.empty()) {
		int x, y;
		tie(x, y) = Q.front(); Q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx >= n || xx < 0 || yy >= n || yy < 0) continue;
			if (map[xx][yy] == 1) continue;
			if (ch[xx][yy] == -1) {
				ch[xx][yy] = ch[x][y] + 1;
				Q.push(make_pair(xx, yy));
			}
			if (xx == dest[k].first && yy == dest[k].second) {
				jun.oil -= ch[xx][yy];
				jun.x = xx;
				jun.y = yy;
				if (jun.oil < 0) {
					return false;
				}
				else {
					jun.oil += 2 * ch[xx][yy];
					return true;
				}
			}
		}
	}
	return false;
}
int main() {
	//freopen("in1.txt", "rt", stdin);
	cin >> n>>m >>jun.oil;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> map[i][j];
		}
	}
	cin >> jun.x >> jun.y;
	jun.x--;
	jun.y--;
	for (int i = 0; i < m; i++) {
		int x, y;
		cin >> x >> y;
		x--; y--;
		son.push_back(make_pair(x, y)); 
		map[x][y] = 2;
		//cout << x << " " << y << " 승객 첫 위치" << '\n';
		cin >> x >> y;
		x--; y--;
		dest.push_back(make_pair(x, y));
	}
	priority_queue<Loc> Q;
	Q.push(Loc(jun.x, jun.y,0));
	bool ok = false;
	for (int x1 = 0; x1 < n; x1++) {
		for (int y1 = 0; y1 < n; y1++) {
			ch[x1][y1] = -1;
		}
	}
	ch[jun.x][jun.y] = 0;
	while (!Q.empty()) {
		ok = false;
		Loc tmp = Q.top();
		Q.pop();
		if (map[tmp.x][tmp.y] == 2) {
			map[tmp.x][tmp.y] = 0;
			//cout << xx << " " << yy<<" find 승객" <<'\n';
			for (int k = 0; k < m; k++) {
				if (son[k].first == tmp.x && son[k].second == tmp.y) {
					jun.oil -= ch[tmp.x][tmp.y];
					//cout << jun.oil << '\n';
					if (jun.oil <= 0) {
						cout << -1 << '\n';
						return 0;
					}
					if (go(tmp.x, tmp.y, k) == false) {
						cout << -1 << '\n';
						return 0;
					}
					ok = true;
					while (!Q.empty()) Q.pop();
					Q.push(Loc(jun.x, jun.y, 0));
					for (int x1 = 0; x1 < n; x1++) {
						for (int y1 = 0; y1 < n; y1++) {
							ch[x1][y1] = -1;
						}
					}
					ch[jun.x][jun.y] = 0;
					//continue;
				}
			}
			if (ok) continue;
		}
		for (int i = 0; i < 4; 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] == 1) continue;
			if (ch[xx][yy] == -1) {
				ch[xx][yy] = tmp.dis + 1;
				Q.push(Loc(xx, yy, tmp.dis + 1));
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (map[i][j] == 2) {
				cout << -1 << '\n';
				return 0;
			}
		}
	}
	cout << jun.oil << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보