티스토리 뷰

1.문제 링크

https://www.acmicpc.net/problem/3187

 

3187번: 양치기 꿍

문제 양치기 꿍은 맨날 늑대가 나타났다고 마을 사람들을 속였지만 이젠 더이상 마을 사람들이 속지 않는다. 화가 난 꿍은 복수심에 불타 아예 늑대들을 양들이 있는 울타리안에 마구 집어넣어 양들을 잡아먹게 했다. 하지만 양들은 보통 양들이 아니다. 같은 울타리 영역 안의 양들의 숫자가 늑대의 숫자보다 더 많을 경우 늑대가 전부 잡아먹힌다. 물론 그 외의 경우는 양이 전부 잡아먹히겠지만 말이다. 꿍은 워낙 똑똑했기 때문에 이들의 결과는 이미 알고있다. 만약 빈

www.acmicpc.net

#include <cstdio>
#include <queue>
using namespace std;

char map[250][250];
bool visited[250][250];

const int dx[] = { 1,-1,0,0 };
const int dy[] = { 0,0,1,-1 };

struct COORD {
	int y, x;
};
int sheep, wolf;
int main(void)
{
	int r, c;
	scanf("%d %d", &r, &c);

	for (int i = 0; i < r; i++)
		scanf("%s", map[i]);

	for(int i=0;i<r;i++)
		for (int j = 0; j < c; j++)
		{
			if (visited[i][j] == false)
			{
				//bfs

				int s=0, w=0;
				queue<COORD> q;
				COORD start;
				start.x = j, start.y = i;

				visited[start.y][start.x] = true;
				q.push(start);

				while (!q.empty())
				{
					COORD cur = q.front(); q.pop();
					if (map[cur.y][cur.x] == 'v')
						w++;
					else if (map[cur.y][cur.x] == 'k')
						s++;

					for (int dir = 0; dir < 4; dir++)
					{
						COORD next;
						next.y = cur.y + dy[dir];
						next.x = cur.x + dx[dir];

						if (next.x < 0 || next.y <0 || next.x >c || next.y > r || map[next.y][next.x] == '#')
							continue;

						if (visited[next.y][next.x] == false)
						{
							visited[next.y][next.x] = true;
							q.push(next);
						}

					}
				}
				//bfs탐색 끝
				if (s > w)
					sheep += s;
				else
					wolf += w;

			}
		}

	printf("%d %d", sheep, wolf);
	return 0;
}
댓글
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Total
Today
Yesterday