본문 바로가기

알고리즘/SWEA

4013

static list<int> table[5];
static int visit[5];
static int score[] = { 0, 1, 2, 4, 8 };
static int K, N, R; //K, 톱날위치, 회전방향

static void rotation(int n, int r)
{
	list<int> &cur = table[n];

	if (r > 0) //시계 방향으로 회전
	{
		cur.push_front(cur.back());
		cur.pop_back();
	}
	else //반시계 방향으로 회전
	{
		cur.push_back(cur.front());
		cur.pop_front();
	}
}

static void __solution(int n, int r)
{
	if (n <= 0 || n > 5)
		return;

	list<int>::iterator ml, mr; //지역변수로 선언해야함.
	
	visit[n] = 1;
	if (n - 1 >= 1 && !visit[n - 1]) //왼쪽 톱날 점검 가능
	{
		mr = table[n - 1].begin();
		ml = table[n].end();
		--(--ml); ++(++mr);
		if (*ml != *mr)
			__solution(n - 1, -r);
	}
	if (n + 1 <= 4 && !visit[n + 1]) ///오른쪽 톱날 점검 가능
	{
		mr = table[n].begin();
		ml = table[n + 1].end();
		--(--ml); ++(++mr);
		if (*ml != *mr)
			__solution(n + 1, -r);
	}

	rotation(n, r);
}

static int solution(void)
{
	int ret = 0, tmp;
	scanf("%d", &K);

	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 8; j++)
		{
			scanf("%d", &tmp);
			table[i].push_back(tmp);
		}
	}

	for (int i = 1; i <= K; i++)
	{
		scanf("%d %d", &N, &R);
		__solution(N, R);
		memset(visit, 0, sizeof(visit));
	}

	for (int i = 1; i <= 4; i++)
	{
		ret += score[i * table[i].front()];
		table[i].clear();
	}

	return ret;
}

...

'알고리즘 > SWEA' 카테고리의 다른 글

6808  (0) 2021.11.01
4012  (0) 2021.11.01
7466  (0) 2021.11.01
8191  (0) 2021.11.01
8189  (0) 2021.11.01