[백준] 1103 게임
틀린 풀이
#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;
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;
}