Notice
Recent Posts
Recent Comments
Link
오븐 노트
[C++] 연습문제 (달팽이) 본문
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 100;
int board[MAX][MAX] = {};
int N;
void PrintBoard()
{
for (int y = 0; y < N; y++)
{
for (int x = 0; x < N; x++)
{
cout << setfill('0') << setw(2) << board[y][x] << " ";
}
cout << endl;
}
}
enum DIR
{
RIGHT = 0,
DOWN = 1,
LEFT = 2,
UP = 3,
};
bool CanGo(int y, int x)
{
if (y < 0 || y >= N)
return false;
if (x < 0 || x >= N)
return false;
if (board[y][x] != 0)
return false;
return true;
}
void SetBorad()
{
int dir = RIGHT;
int num = 1;
int y = 0;
int x = 0;
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
while (true)
{
board[y][x] = num;
if (num == N * N)
break;
int nextY = y + dy[dir];
int nextX = x + dx[dir];
/*int nextY;
int nextX;
switch (dir)
{
case RIGHT:
nextY = y;
nextX = x + 1;
break;
case DOWN:
nextY = y + 1;
nextX = x;
break;
case LEFT:
nextY = y;
nextX = x - 1;
break;
case UP:
nextY = y - 1;
nextX = x;
break;
}*/
if (CanGo(nextY, nextX))
{
y = nextY;
x = nextX;
num++;
}
else
{
dir = (dir + 1) % 4;
/*switch (dir)
{
case RIGHT:
dir = DOWN;
break;
case DOWN:
dir = LEFT;
break;
case LEFT:
dir = UP;
break;
case UP:
dir = RIGHT;
break;
}*/
}
}
}
int main()
{
cin >> N;
SetBorad();
PrintBoard();
return 0;
}
[C++과 언리얼로 만드는 MMORPG 게임 개발 시리즈] Part1: C++ 프로그래밍 입문 - 인프런 | 강의
C++ 카테고리의 글은 인프런 Rookiss님의 강의를 공부하며 정리하는 내용입니다.
이미 알고 있는 내용도 다시 정리 되어있을 수 있습니다.
모든 글은 제가 공부하기 위해 작성합니다.
'Develop > C++' 카테고리의 다른 글
[C++] 객체지향의 시작 (0) | 2022.09.05 |
---|---|
[C++] 파일 분할 관리 (0) | 2022.08.29 |
[C++] 연습 문제 (문자열) #1 ~ 2 (0) | 2022.08.16 |
[C++] TextRPG #3 (0) | 2022.08.13 |
[C++] 포인터 마무리 (0) | 2022.07.15 |