[백준] 1103 게임💫

0

백준

목록 보기
257/271
post-thumbnail
post-custom-banner

[백준] 1103 게임

틀린 풀이

  • 1%에서 틀렸습니다
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;

int N, M;
vector<string> board;

bool inRange(int r, int c) {
	if ((r < 0) || (r >= N)) return false;
	if ((c < 0) || (c >= M)) return false;
	return true;
}

int solve() {
	queue<pair<int, pair<int, int>>> q;
	int visited[51][51] = { 0 };
	q.push({ 0, { 0, 0 } });

	int maxMove = 1;
	while (!q.empty()) {
		int curMove = q.front().first;
		int curR = q.front().second.first; 
		int curC = q.front().second.second;
		q.pop();

		if (visited[curR][curC]) return -1;
		visited[curR][curC] = 1;

		maxMove = max(maxMove, curMove);

		int X = board[curR][curC] - '0';
		for (int d = 0; d < 4; ++d) {
			int nextR = curR;
			int nextC = curC;

			//오른쪽
			if (d == 0) nextC += X;
			//왼쪽
			else if (d == 1) nextC -= X;
			//위쪽
			else if (d == 3) nextR += X;
			//아래쪽
			else nextR -= X;

			if (!inRange(nextR, nextC)) continue;
			if (board[nextR][nextC] == 'H') continue;
			//if (visited[nextR][nextC]) continue;

			q.push({ curMove + 1, {nextR, nextC} });
		}
	}

	return maxMove;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	cin >> N >> M;
	for (int i = 0; i < N; ++i) {
		string input;
		cin >> input;
		board.push_back(input);
	}

	cout << solve();
	return 0;
}

profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글