![[코딩 팁] 방향 배열(Direction Array) : 상하좌우 이동](http://i1.daumcdn.net/thumb/C120x120/?fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2F16kFW%2FbtrmLPjxbAp%2FAAAAAAAAAAAAAAAAAAAAANJDSgPRUTf8wFZu_IKTTgSUqo30p8rGvJ0J21WLQIzE%2Fimg.jpg%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3Dfl3Bv%252BC5JxJ4wyRkgxs0RVg05EY%253D)
[코딩 팁] 방향 배열(Direction Array) : 상하좌우 이동
인트로 2차원 좌표상에서 한 점을 기준으로 상하좌우 좌표를 구할 때 코드를 간결하게 작성하는 방법이다. 방향 배열은 BFS, 다익스트라, A*알고리즘에 자주 등장한다. 코드 switch 혹은 if-else문을 작성하지 않아도 된다. int main() { //상 하 좌 우 int dirY[4] = { -1, 1, 0, 0 }; int dirX[4] = { 0, 0, -1, 1 }; int arr[3][3] = { {1,2,3},{4,5,6}, {7,8,9} }; int posY = 1, posX = 1; cout
- Algorithm/감명 깊게 본 코딩 팁
- · 2021. 10. 3.