백준

백준 31844 - 창고지기(C++)

공대생의 잡다한 사전 2024. 5. 26. 14:13
문제 링크입니다. https://www.acmicpc.net/problem/31844

 

박스를 옮길 수 있는 조건을 찾아서 해결하는 문제입니다.

 

박스를 옮길 수 있는 조건은 2가지입니다.

 

1. 로봇 -> 박스 -> 도착점

2. 도착점 <- 박스 <- 로봇

 

해당의 경우가 아니라면 전부 -1을 출력해주면 되고

해당 경우를 만족한다면 로봇과 도착점까지의 거리를 구해주면 됩니다.

 

 

 

자세한 것은 코드를 참고해주세요.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>

using namespace std;

string arr;
int st, fin, box;

void solve() {
	if (st < box && box < fin) {
		cout << fin - st - 1;
	}
	else if (fin < box && box < st) {
		cout << st - fin - 1;
	}
	else cout << "-1";
}

int main() {
	cin.tie(0);
	cout.tie(0);
	cin >> arr;
	for (int i = 0; i < 10; i++) {
		if (arr[i] == '@') st = i;
		if (arr[i] == '#') box = i;
		if (arr[i] == '!') fin = i;
	}
	solve();
	return 0;
}

 

 

질문 및 조언은 댓글을 남겨주세요.