-
[C++] 프로그래머스 리코쳇 로봇알고리즘/프로그래머스 2023. 3. 22. 13:53반응형1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#include <string>#include <vector>#include <queue>#include <iostream>using namespace std;int dx[] = {0,0,-1,1};int dy[] = {-1,1,0,0};int n,m;int answer = -1;bool check[101][101];struct b {int x;int y;int cnt;};void bfs(int x_tmp, int y_tmp, vector<string> board) {queue<b> q;q.push({x_tmp,y_tmp,0});while(!q.empty()) {int x = q.front().x;int y = q.front().y;int cnt = q.front().cnt;check[x][y] = true;if(board[x][y] == 'G') {answer = cnt;break;}q.pop();for(int i=0; i<4; i++) {int x1 = x;int y1 = y;while(1) {int nx = x1 + dx[i];int ny = y1 + dy[i];if(nx < 0 || ny < 0 || nx >=n || ny >= m || board[nx][ny] == 'D') {if(!check[x1][y1]) {q.push({x1,y1,cnt+1});}break;}else {x1 = nx;y1 = ny;}}}}}int solution(vector<string> board) {n = board.size();m = board[0].size();for(int i=0; i<n; i++) {for(int j=0; j<m; j++) {if(board[i][j] == 'R') {bfs(i,j,board);}}}return answer;}
cs BFS + 구현
while 문을 이용해 장애물, 맨끝에 부딪힐때까지 이동하는 코드만 작성하면 쉽게 풀리는 문제
정답률이 낮은 이유는 구현에서 애를먹어서 그런게 아닐까??
728x90반응형'알고리즘 > 프로그래머스' 카테고리의 다른 글
[C++] 프로그래머스 연속된 부분 수열의 합 (0) 2023.04.13 [C++] 프로그래머스 광물 캐기 (0) 2023.03.23 [C++] 프로그래머스 무인도 여행 (2) 2023.01.27 [C++] 백준 26091 현대모비스 소프트웨어 아카데미 (0) 2023.01.15 [C++] 프로그래머스 마법의 엘리베이터 (0) 2023.01.13