[c++] 백준 1331

알감자·2022년 4월 29일
0

백준알고리즘

목록 보기
26/52

#1331

#include <iostream>
using namespace std;

int main()
{
	//freopen("test.txt", "r", stdin);

	char X[36];
	int Y[36];
	bool b_nextStep = true;
	
	for (int i = 0; i < 36; i++)
	{
		cin >> X[i] >> Y[i];

		//나이트가 이동한 곳이 맞는지 체크
		if (i != 0)
		{
			if ((X[i - 1] + 2) == X[i] || (X[i - 1] - 2) == X[i])
			{
				if ((Y[i - 1] + 1) == Y[i] || (Y[i - 1] - 1) == Y[i])
					b_nextStep = true;
				else
				{
					b_nextStep = false;
					break;
				}
			}
			else if ((X[i - 1] + 1) == X[i] || (X[i - 1] - 1) == X[i])
			{
				if ((Y[i - 1] + 2) == Y[i] || (Y[i - 1] - 2) == Y[i])
					b_nextStep = true;
				else
				{
					b_nextStep = false;
					break;
				}
			}
			else
			{
				b_nextStep = false;
				break;
			}
		}

		//마지막 나이트가 이동한 곳이 처음이 맞는지 체크
		if (i == 35)
		{
			if ((X[35] + 1) == X[0] || (X[35] - 1) == X[0])
			{
				if ((Y[35] + 2) == Y[0] || (Y[35] - 2) == Y[0])
					b_nextStep = true;
				else
				{
					b_nextStep = false;
					break;
				}
			}
			else if ((X[35] + 2) == X[0] || (X[35] - 2) == X[0])
			{
				if ((Y[35] + 1) == Y[0] || (Y[35] - 1) == Y[0])
					b_nextStep = true;
				else
				{
					b_nextStep = false;
					break;
				}
			}
			else
			{
				b_nextStep = false;
			}
			
		}

		//나이트가 이동한 곳이 중복이 있는지 체크
		for (int j = 0; j < i; j++)
		{
			if (X[i] == X[j] && Y[i] == Y[j])
			{
				b_nextStep = false;
				break;
			}
		}

			if (!b_nextStep)
				break;
		
	}

	if (b_nextStep)
		cout << "Valid";
	else
		cout << "Invalid";

	return 0;
}

참고 : https://bluebluediary.tistory.com/65

0개의 댓글