direct 기법

hy cho·2021년 11월 28일
0

알고리즘 공부

목록 보기
13/26
post-thumbnail

좌표의 상 하 좌 우 offset값 출력하기 위함

좌표 하나 입력받아 상 하 좌 우 합을 구해 출력하기

int arr[4][4] = {
4,7,7,3,
6,6,9,8,
5,3,9,3,
1,1,1,2 };

int direct[4][2] = {
-1,0,
1,0,
0,-1,
0,1
};

int main()
{

int yy, xx;
int indexy, indexx;
cin >> indexy>> indexx;
int sum = 0;

for (int t = 0; t < 4; t++)
{
	int dy = indexy + direct[t][0];
	int dx = indexx + direct[t][1];
	//if (dy < 0 || dy < 3 || dx < 0 || dx>3)continue;  // 조건문 참이면 반복문 맨 위로 올려버림

	if (dy >= 0 && dy <= 3 && dx >= 0 && dx <= 3)
	{
		sum += arr[dy][dx];
	}
	
}

cout << sum;



return 0;

}

for (int t = 0; t < 4; t++)
{
int dy = indexy + direct[t][0];
int dx = indexx + direct[t][1];
}

t = 0 1 2 3 위 아래 좌 우 계산

indexy indexx 1, 1 입력시

t = 0이면
int dy = 1 + direct[0][0]; == 0
int dx = 1 + direct[0][1]; == 1
(0,1) == 7로 t가 0일때 입력한 좌표 바로 위의 값 지정

t = 1
int dy = 1 + direct[1][0]; == 2
int dx = 1 + direct[1][1]; == 1
(2,1) == 3로 t가 1일때 입력한 좌표 바로 아래 값 지정

t = 2
int dy = 1 + direct[2][0]; == 1
int dx = 1 + direct[2][1]; == 0
(1,0) == 6로 t가 2일때 입력한 좌표 바로 왼쪽의 값 지정

t = 3
int dy = 1 + direct[3][0]; == 1
int dx = 1 + direct[3][1]; == 2
(1,2) == 9로 t가 3일때 입력한 좌표 바로 오른쪽 값 지정

if (dy < 0 || dy < 3 || dx < 0 || dx>3)continue;
continue = 조건문 참이면 반복문 맨 위로 올려버림
y,x 좌표가 가운데 네칸이어야 함

profile
hihi

0개의 댓글