U E D R , A S I H C RSS

Spiral Array/영동

소감

  • 제대해서 처음으로 숙제를 제외하고 처음 짠 ToyProblem입니다. 1학년 때 프로그래밍잔치에서 못 짰던 걸 이제야 짰네요. 우선 소요시간으로 미루어 볼때 제대하고 나서 머리가 굳었다는 걸 느낄 수 있었고, 그만큼 처음부터 막 짜지 말고 설계 및 구상을 잘 해야겠다고 생각했습니다. 또한 객체지향으로 짠 것도 아니고 변수, 함수를 너무 지저분하게 쓴 거 같기도 하고... 반성할 점이 참 많았습니다. 그리고 일단 배열 크기도 미리 정했고 시작점도 0, 0으로 가정하고 해서 사용자의 잘 못된 입력에 대응하지 않은 점도 미비했네요.

소스 코드

#include<iostream>
using namespace std;
const int RIGHT=0;
const int DOWN=1;
const int LEFT=2;
const int UP=3;
const int DIRECTION=4;//이동 가능한 총 방향수
const int MOVE_X[DIRECTION]={1, 0, -1, 0};
const int MOVE_Y[DIRECTION]={0, 1, 0, -1};
const int MAX_X=5;
const int MAX_Y=5;
struct Mover
{
	int currentX;//현재 x좌표
	int currentY;//현재 y좌표
	int currentDirection;//현재 이동 방향
	Mover(int startingX, int startingY)
	{//생성자
		currentX=startingX;
		currentY=startingY;
		currentDirection=RIGHT;
	}
};

void showBoard(int aBoard[][MAX_X]);//배열을 보여준다
void setEmptyBoard(int aBoard[][MAX_X]);//배열 초기화
void changeDirection(Mover * aMover);//방향을 바꾼다
int setStartingX();//시작 위치 설정: x
int setStartingY();//시작 위치 설정: y
void move(Mover * aMover);//이동
bool isEnd(int endCount);//루프 끝날지 검사
bool needToChangeDirection(Mover * aMover, int aBoard[][MAX_X]);//방향을 바꿀 필요가 있는지 검사
void checkAtBoard(int aBoard[][MAX_X], Mover * aMover, int * aNumber);//이동 결과를 배열에 표시

int main()
{
	Mover mover(setStartingX(), setStartingY());//이동하는 물체
	int board[MAX_X][MAX_Y];//배열
	int countMove=0;//총 이동횟수

	setEmptyBoard(board);//배열 초기화
	checkAtBoard(board, &mover, &countMove);//첫 위치에 번호를 찍어줌
	do 
	{
		if(needToChangeDirection(&mover, board))
			changeDirection(&mover);//방향을 바꿀 필요가 있으면 바꿔줌
		move(&mover);//이동
		checkAtBoard(board, &mover, &countMove);//배열에 표시
	}while(isEnd(countMove));//종료조건

	showBoard(board);//결과 출력
	return 1;
}
void showBoard(int aBoard[][MAX_X])
{
	for(int i=0;i<MAX_Y;i++)
	{
		for(int j=0;j<MAX_X;j++)
			cout<<aBoard[i][j]<<"\t";
		cout<<"\n";
	}
}
void setEmptyBoard(int aBoard[][MAX_X])
{
	for(int i=0;i<MAX_Y;i++)
	{
		for(int j=0;j<MAX_X;j++)
			aBoard[i][j]=0;
	}
}
void changeDirection(Mover * aMover)
{
	if(aMover->currentDirection+1>UP)
		aMover->currentDirection=RIGHT;
	else
		aMover->currentDirection+=1;
}
bool isEnd(int endCount)
{
	if(endCount<MAX_X*MAX_Y)
		return true;
	else
		return false;
}
int setStartingX()
{
	int tempX;
	cout<<"시작 점의 x좌표는?"<<endl;
	cin>>tempX;
	return tempX;
}
int setStartingY()
{
	int tempY;
	cout<<"시작 점의 y좌표는?"<<endl;
	cin>>tempY;
	return tempY;
}
void move(Mover * aMover)
{	
	aMover->currentX=aMover->currentX+MOVE_X[aMover->currentDirection];
	aMover->currentY=aMover->currentY+MOVE_Y[aMover->currentDirection];
}
bool needToChangeDirection(Mover * aMover, int aBoard[][MAX_X])
{
	if(aMover->currentX+MOVE_X[aMover->currentDirection]>=MAX_X)
		return true;//X방향으로 배열을 빠져나갈 경우(MAX이상)
	if(aMover->currentY+MOVE_Y[aMover->currentDirection]>=MAX_Y)
		return true;//Y방향으로 배열을 빠져나갈 경우(MAX이상)
	if(aMover->currentX+MOVE_X[aMover->currentDirection]<0)
		return true;//X방향으로 배열을 빠져나갈 경우(0이하)
	if(aMover->currentY+MOVE_Y[aMover->currentDirection]<0)
		return true;//Y방향으로 배열을 빠져나갈 경우(0이하)
	if(aBoard[aMover->currentY+MOVE_Y[aMover->currentDirection]][aMover->currentX+MOVE_X[aMover->currentDirection]]!=0)
		return true;//이동할 위치에 이미 딴 숫자가 적혀 있는 경우
	else
		return false;
}
void checkAtBoard(int aBoard[][MAX_X], Mover * aMover, int * aNumber)
{
	aBoard[aMover->currentY][aMover->currentX]=(*aNumber)+1;
	(*aNumber)++;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:06
Processing time 0.0104 sec