Skip to content

ABC_182_E - Akari

#50

Problem link

https://atcoder.jp/contests/abc182/tasks/abc182_e

Problem Summary

n개의 전구가 4방향으로 빛을 쏘고, m개의 블럭은 빛을 차단한다.
전체 그리드 (H*W)에서 총 몇 칸이 빛이 도달하는지 확인.

Solution

간단한 시뮬레이션 문제.
모든 전구에서 상 하 좌 우로 빛을 쏴주면서 카운트를 해주면 된다.

Source Code

#include <iostream>
#include <vector>
#include <cstring>
#include <utility>
using namespace std;

vector<pair<int, int> > bulbs;
int grid[1502][1502];
int h, w, n, m;

const int direction[][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };

int main() {
	cin >> h >> w >> n >> m;

	// set border to -1
	for (int i = 0; i <= h; i++)
	{
		grid[i][0] = -1;
		grid[i][w + 1] = -1;
	}
	for (int i = 0; i <= w; i++)
	{
		grid[0][i] = -1;
		grid[h + 1][i] = -1;
	}

	for (int i = 0; i < n; i++)
	{
		int a, b;
		cin >> a >> b;

		grid[a][b] = 1;
		bulbs.push_back(make_pair(a, b));
	}

	for (int i = 0; i < m; i++)
	{
		int c, d;
		cin >> c >> d;

		grid[c][d] = -1;
	}

	int ans = n;

	for (int i = 0; i < n; i++)
	{
		int a = bulbs[i].first;
		int b = bulbs[i].second;

		for (int d = 0; d < 4; d++)
		{
			int x = a, y = b;
			int cnt = 1;

			while (true)
			{
				int nx = x + direction[d][0] * cnt;
				int ny = y + direction[d][1] * cnt;

				if (grid[nx][ny] == -1 || grid[nx][ny] == 1)
				{
					break;
				}

				if (grid[nx][ny] == 0)
				{
					ans++;
				}
				grid[nx][ny] = 2;
				cnt++;
			}
		}
	}

	cout << ans << endl;
}