Search
Duplicate
📗

달팽이

주차
문제번호
1913
언어
C++
티어
실버
유형
구현
nj_Blog
nj_상태
이해도
100%
풀이
사람
이해도 2
13 more properties

문제접근

놓쳤던 부분

아래, 오른쪽, 위, 왼쪽 방향 dx,dy 설정을 해뒀는데 이 문제는 그래프 문제가 아니라 진행하던 방향으로 계속 진행을 해야함

코드

6000 KB

104 ms

#include <iostream> #include <vector> using namespace std; int main(void) { int dir = 0; int row = 0; int col = 0; int posX, posY; int dy[4] = {1, 0, -1, 0}; int dx[4] = {0, 1, 0, -1}; vector<vector<int> > snail; int target; int number; int n; ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; cin >> target; snail.resize(n, vector<int>(n, 0)); number = n * n + 1; while (--number >= 1) { snail[row][col] = number; if (number == target) { posX = row; posY = col; } while (true) { int nextRow = row + dy[dir % 4]; int nextCol = col + dx[dir % 4]; if (nextRow >= 0 && nextRow < n && nextCol >= 0 && nextCol < n && snail[nextRow][nextCol] == 0) { row = nextRow; col = nextCol; break ; } if (number == 1) break ; dir++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << snail[i][j] << " "; } cout << "\n"; } cout << posX + 1 << " " << posY + 1; return (0); }
C++
복사