๐Ÿ™‡โ€โ™€๏ธ[Gold I] ๋‚š์‹œ์™• - 17143

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2352 KB, ์‹œ๊ฐ„: 152 ms

๋ถ„๋ฅ˜

๊ตฌํ˜„, ์‹œ๋ฎฌ๋ ˆ์ด์…˜

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 17์ผ 04:06:56

๋ฌธ์ œ ์„ค๋ช…

๋‚š์‹œ์™•์ด ์ƒ์–ด ๋‚š์‹œ๋ฅผ ํ•˜๋Š” ๊ณณ์€ ํฌ๊ธฐ๊ฐ€ Rร—C์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฉ์žํŒ์˜ ๊ฐ ์นธ์€ (r, c)๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. r์€ ํ–‰, c๋Š” ์—ด์ด๊ณ , (R, C)๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜์— ์žˆ๋Š” ์นธ์ด๋‹ค. ์นธ์—๋Š” ์ƒ์–ด๊ฐ€ ์ตœ๋Œ€ ํ•œ ๋งˆ๋ฆฌ ๋“ค์–ด์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ƒ์–ด๋Š” ํฌ๊ธฐ์™€ ์†๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

๋‚š์‹œ์™•์€ ์ฒ˜์Œ์— 1๋ฒˆ ์—ด์˜ ํ•œ ์นธ ์™ผ์ชฝ์— ์žˆ๋‹ค. ๋‹ค์Œ์€ 1์ดˆ ๋™์•ˆ ์ผ์–ด๋‚˜๋Š” ์ผ์ด๋ฉฐ, ์•„๋ž˜ ์ ํžŒ ์ˆœ์„œ๋Œ€๋กœ ์ผ์–ด๋‚œ๋‹ค. ๋‚š์‹œ์™•์€ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์—ด์˜ ์˜ค๋ฅธ์ชฝ ์นธ์— ์ด๋™ํ•˜๋ฉด ์ด๋™์„ ๋ฉˆ์ถ˜๋‹ค.

  1. ๋‚š์‹œ์™•์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์ด๋™ํ•œ๋‹ค.
  2. ๋‚š์‹œ์™•์ด ์žˆ๋Š” ์—ด์— ์žˆ๋Š” ์ƒ์–ด ์ค‘์—์„œ ๋•…๊ณผ ์ œ์ผ ๊ฐ€๊นŒ์šด ์ƒ์–ด๋ฅผ ์žก๋Š”๋‹ค. ์ƒ์–ด๋ฅผ ์žก์œผ๋ฉด ๊ฒฉ์žํŒ์—์„œ ์žก์€ ์ƒ์–ด๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.
  3. ์ƒ์–ด๊ฐ€ ์ด๋™ํ•œ๋‹ค.

์ƒ์–ด๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์†๋„๋กœ ์ด๋™ํ•˜๊ณ , ์†๋„์˜ ๋‹จ์œ„๋Š” ์นธ/์ดˆ์ด๋‹ค. ์ƒ์–ด๊ฐ€ ์ด๋™ํ•˜๋ ค๊ณ  ํ•˜๋Š” ์นธ์ด ๊ฒฉ์žํŒ์˜ ๊ฒฝ๊ณ„๋ฅผ ๋„˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊ฟ”์„œ ์†๋ ฅ์„ ์œ ์ง€ํ•œ์ฑ„๋กœ ์ด๋™ํ•œ๋‹ค.

์™ผ์ชฝ ๊ทธ๋ฆผ์˜ ์ƒํƒœ์—์„œ 1์ดˆ๊ฐ€ ์ง€๋‚˜๋ฉด ์˜ค๋ฅธ์ชฝ ์ƒํƒœ๊ฐ€ ๋œ๋‹ค. ์ƒ์–ด๊ฐ€ ๋ณด๊ณ  ์žˆ๋Š” ๋ฐฉํ–ฅ์ด ์†๋„์˜ ๋ฐฉํ–ฅ, ์™ผ์ชฝ ์•„๋ž˜์— ์ ํžŒ ์ •์ˆ˜๋Š” ์†๋ ฅ์ด๋‹ค. ์™ผ์ชฝ ์œ„์— ์ƒ์–ด๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž๋ฅผ ์ ์—ˆ๋‹ค.

์ƒ์–ด๊ฐ€ ์ด๋™์„ ๋งˆ์นœ ํ›„์— ํ•œ ์นธ์— ์ƒ์–ด๊ฐ€ ๋‘ ๋งˆ๋ฆฌ ์ด์ƒ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ๋Š” ํฌ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ํฐ ์ƒ์–ด๊ฐ€ ๋‚˜๋จธ์ง€ ์ƒ์–ด๋ฅผ ๋ชจ๋‘ ์žก์•„๋จน๋Š”๋‹ค.

๋‚š์‹œ์™•์ด ์ƒ์–ด ๋‚š์‹œ๋ฅผ ํ•˜๋Š” ๊ฒฉ์žํŒ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‚š์‹œ์™•์ด ์žก์€ ์ƒ์–ด ํฌ๊ธฐ์˜ ํ•ฉ์„ ๊ตฌํ•ด๋ณด์ž.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฒฉ์žํŒ์˜ ํฌ๊ธฐ R, C์™€ ์ƒ์–ด์˜ ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (2 โ‰ค R, C โ‰ค 100, 0 โ‰ค M โ‰ค Rร—C)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ์ƒ์–ด์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ƒ์–ด์˜ ์ •๋ณด๋Š” ๋‹ค์„ฏ ์ •์ˆ˜ r, c, s, d, z (1 โ‰ค r โ‰ค R, 1 โ‰ค c โ‰ค C, 0 โ‰ค s โ‰ค 1000, 1 โ‰ค d โ‰ค 4, 1 โ‰ค z โ‰ค 10000) ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. (r, c)๋Š” ์ƒ์–ด์˜ ์œ„์น˜, s๋Š” ์†๋ ฅ, d๋Š” ์ด๋™ ๋ฐฉํ–ฅ, z๋Š” ํฌ๊ธฐ์ด๋‹ค. d๊ฐ€ 1์ธ ๊ฒฝ์šฐ๋Š” ์œ„, 2์ธ ๊ฒฝ์šฐ๋Š” ์•„๋ž˜, 3์ธ ๊ฒฝ์šฐ๋Š” ์˜ค๋ฅธ์ชฝ, 4์ธ ๊ฒฝ์šฐ๋Š” ์™ผ์ชฝ์„ ์˜๋ฏธํ•œ๋‹ค.

๋‘ ์ƒ์–ด๊ฐ€ ๊ฐ™์€ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” ๊ฒฝ์šฐ๋Š” ์—†๊ณ , ํ•˜๋‚˜์˜ ์นธ์— ๋‘˜ ์ด์ƒ์˜ ์ƒ์–ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

๋‚š์‹œ์™•์ด ์žก์€ ์ƒ์–ด ํฌ๊ธฐ์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚˜๊ฒŒ ํ’€์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ƒ์–ด๊ฐ€ ์ด๋™ํ•˜๋Š”๊ฒŒ ์ตœ์†Œํ•œ์œผ๋กœ ์›€์ง์ด๊ฒŒ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.

์ƒ์–ด์˜ ์†๋„๊ฐ€ ๋งค์šฐ ๋†’์•„์„œ ๋งต์„ ๋ฐ˜๋ณตํ•ด์„œ ์›€์ง์ธ๋‹ค๋ฉด ์ค‘๋ณต๋˜๋Š” ๊ฐ’์„ ์—†์•จ์ˆ˜์žˆ๋‹ค.

R, C ๋ณด๋“œ์—์„œ ์ƒ์–ด๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š”๊ฑธ ์—†์• ๋ ค๋ฉด
s = s % ((R - 1) * 2) ๋กœ ๋ฐฉํ–ฅ์ด R๋ผ์ธ์ผ ๋•Œ ์ด๋ ‡๊ฒŒ ๊ณ ์น ์ˆ˜ ์žˆ๊ณ  ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ C ๋ฐฉํ–ฅ์ด๋ผ๋ฉด
s = s % ((C - 1) * 2) ๋กœ ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ƒ์–ด๊ฐ€ ๋‹ค ์›€์ง์ด๊ณ  ์ƒ์–ด๋ฅผ ์žก์•„๋จน๋Š”๋‹ค๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค.

M์€ ์ตœ๋Œ€ 10000์ด๋ผ์„œ ๋งŒ๋ฒˆ * ๋งŒ๋ฒˆ์„ ๋Œ๋ฉด ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค.

๊ทธ๋ž˜์„œ ์ƒ์–ด๊ฐ€ ์ด๋™ํ•˜์ž๋งˆ์ž ๋จน๋Š” ๊ฒƒ์„ ํ•ด์•ผํ•˜๋Š”๋ฐ ๋งŒ๋‚œ ์ƒ์–ด๊ฐ€ ์ด๋™ํ•œ ํ›„ ์ƒ์–ด์ธ์ง€ ์ด๋™ ์ „ ์ƒ์–ด์ธ์ง€ ์•Œ์•„์•ผํ•œ๋‹ค.

// ์ด๋™์„ ์™„๋ฃŒํ•œ ์ƒ์–ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•ด์•ผํ•จ
if (board[y][x] == 0)
{
    board[y][x] = i;
    sharks[i].r = y;
    sharks[i].c = x;
}
else if (board[y][x] < i)
{
    if (sharks[board[y][x]].z > sharks[i].z)
    {
        removeShark(i);
    }
    else
    {
        removeShark(board[y][x]);
        board[y][x] = i;
        sharks[i].r = y;
        sharks[i].c = x;
    }
}

๊ทธ๋ž˜์„œ ๊ฐ ์ƒ์–ด๋งˆ๋‹ค ์ธ๋ฑ์Šค๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๊ทธ๊ฒƒ์„ ํ†ตํ•ด์„œ ํŒ๋ณ„ํ–ˆ๋‹ค.

์ƒ์–ด๊ฐ€ ์›€์ง์ด๋Š” ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

void moveShark()
{
	for (int i = 0; i < sharks.size(); ++i)
	{
		if (sharks[i].r == 0)
			continue;

		int y = sharks[i].r;
		int x = sharks[i].c;
		//board[y][x] = 0; // ์ž๋ฆฌ๋ฅผ ๋– ๋‚ฌ๋‹ค.

		for (int j = 0; j < sharks[i].s; ++j)
		{
			int ny = y + dy[sharks[i].d];
			int nx = x + dx[sharks[i].d];

			if (ny < 1 || nx < 1 || ny > R || nx > C)
			{
				if (sharks[i].d == 1 || sharks[i].d == 3)
				{
					sharks[i].d++;
					ny = y + dy[sharks[i].d];
					nx = x + dx[sharks[i].d];
				}
				else if (sharks[i].d == 2 || sharks[i].d == 4)
				{
					sharks[i].d--;
					ny = y + dy[sharks[i].d];
					nx = x + dx[sharks[i].d];
				}
			}

			y = ny;
			x = nx;
		}

		// ์ด๋™์„ ์™„๋ฃŒํ•œ ์ƒ์–ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•ด์•ผํ•จ
		if (board[y][x] == 0)
		{
			board[y][x] = i;
			sharks[i].r = y;
			sharks[i].c = x;
		}
		else if (board[y][x] < i)
		{
			if (sharks[board[y][x]].z > sharks[i].z)
			{
				removeShark(i);
			}
			else
			{
				removeShark(board[y][x]);
				board[y][x] = i;
				sharks[i].r = y;
				sharks[i].c = x;
			}
		}
	}

	//printBoard();

	for (int i = 0; i < sharks.size(); ++i)
	{
		board[sharks[i].r][sharks[i].c] = 0;
	}
}

๋งˆ์ง€๋ง‰์— ์ƒ์–ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‹ค ์ง€์›Œ์„œ ํ”์ ์„ ๋‚จ๊ธฐ์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

์ƒ์–ด๋ฅผ ์—†์• ๋Š” ๊ฑด ์ƒ์–ด์˜ ์ •๋ณด๋ฅผ ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

์™œ๋ƒํ•˜๋ฉด vector๋กœ ์ƒ์–ด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š”๋ฐ ์ •๋ง๋กœ ์ง€์šฐ๊ฒŒ ๋œ๋‹ค๋ฉด ์ธ๋ฑ์Šค ๋ฌธ์ œ๋„ ์žˆ์„ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๋ณต์‚ฌ ๊ณผ์ •์ด ์žˆ์œผ๋ฏ€๋กœ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ธฐ๋•Œ๋ฌธ์ด๋‹ค.

image

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

struct Shark
{
	int r, c, s, d, z;

	bool operator<(const Shark other)
	{
		if (r < other.r)
			return true;
		return false;
	}
};

void setting();
void solve();
void moveShark();
void removeShark(int idx);
void printBoard();

// 1. ๋‚š์‹œ์™•์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์ด๋™
// 2. ๋‚š์‹œ์™•์ด ์žˆ๋Š” ์—ด์— ์žˆ๋Š” ์ƒ์–ด ์ค‘์—์„œ ๋•…๊ณผ ์ œ์ผ ๊ฐ€๊นŒ์šด ์ƒ์–ด๋ฅผ ์žก๋Š”๋‹ค. ์ƒ์–ด๋ฅผ ์žก์œผ๋ฉด ๊ฒฉ์žํŒ์—์„œ ์žก์€ ์ƒ์–ด๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.
// 3. ์ƒ์–ด๊ฐ€ ์ด๋™ํ•œ๋‹ค.

int R, C, M, r, c, s, d, z, res = 0;
int dy[] = { 0, -1, 1, 0, 0 };
int dx[] = { 0, 0, 0, 1, -1 };
vector<vector<int>> board;
vector<Shark> sharks;

void setting()
{
	cin >> R >> C >> M;
	board = vector<vector<int>>(R + 1, vector<int>(C + 1));
	sharks = vector<Shark>(M + 1);

	sharks[0].r = 0;
	sharks[0].c = 0;
	sharks[0].s = 0;
	sharks[0].d = 0;
	sharks[0].z = 0;

	for (int i = 1; i <= M; ++i)
	{
		cin >> r >> c >> s >> d >> z;
		sharks[i].r = r;
		sharks[i].c = c;
		sharks[i].s = s;
		sharks[i].d = d;
		sharks[i].z = z;

		if (d <= 2)
		{
			sharks[i].s = s % ((R - 1) * 2);
		}
		else if (d >= 3)
		{
			sharks[i].s = s % ((C - 1) * 2);
		}
	}
}

void moveShark()
{
	for (int i = 0; i < sharks.size(); ++i)
	{
		if (sharks[i].r == 0)
			continue;

		int y = sharks[i].r;
		int x = sharks[i].c;
		//board[y][x] = 0; // ์ž๋ฆฌ๋ฅผ ๋– ๋‚ฌ๋‹ค.

		for (int j = 0; j < sharks[i].s; ++j)
		{
			int ny = y + dy[sharks[i].d];
			int nx = x + dx[sharks[i].d];

			if (ny < 1 || nx < 1 || ny > R || nx > C)
			{
				if (sharks[i].d == 1 || sharks[i].d == 3)
				{
					sharks[i].d++;
					ny = y + dy[sharks[i].d];
					nx = x + dx[sharks[i].d];
				}
				else if (sharks[i].d == 2 || sharks[i].d == 4)
				{
					sharks[i].d--;
					ny = y + dy[sharks[i].d];
					nx = x + dx[sharks[i].d];
				}
			}

			y = ny;
			x = nx;
		}

		// ์ด๋™์„ ์™„๋ฃŒํ•œ ์ƒ์–ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•ด์•ผํ•จ
		if (board[y][x] == 0)
		{
			board[y][x] = i;
			sharks[i].r = y;
			sharks[i].c = x;
		}
		else if (board[y][x] < i)
		{
			if (sharks[board[y][x]].z > sharks[i].z)
			{
				removeShark(i);
			}
			else
			{
				removeShark(board[y][x]);
				board[y][x] = i;
				sharks[i].r = y;
				sharks[i].c = x;
			}
		}
	}

	//printBoard();

	for (int i = 0; i < sharks.size(); ++i)
	{
		board[sharks[i].r][sharks[i].c] = 0;
	}
}

void removeShark(int idx)
{
	sharks[idx].r = 0;
	sharks[idx].c = 0;
	sharks[idx].d = 0;
	sharks[idx].s = 0;
	sharks[idx].z = 0;
}

void printBoard()
{
	for (int i = 1; i <= R; ++i)
	{
		for (int j = 1; j <= C; ++j)
		{
			cout << board[i][j] << " ";
		}

		cout << endl;
	}

	cout << endl;
}

void solve()
{
	setting();
	// ์ฃผ์ธ๊ณต์€ 1๋ถ€ํ„ฐ C๊นŒ์ง€ ์ด๋™
	for (int pos = 1; pos <= C; ++pos)
	{
		// ์ƒ์–ด ์žก๊ธฐ
		pair<int, int> temp = { 1000, 1000 }; // ์ธ๋ฑ์Šค, r๊ฐ’
		for (int k = 0; k < sharks.size(); ++k)
		{
			if (sharks[k].c == pos)
			{
				if (sharks[k].r < temp.second)
				{
					temp.first = k;
					temp.second = sharks[k].r;
				}
			}
		}

		if (temp.first != 1000)
		{
			res += sharks[temp.first].z;
			removeShark(temp.first);
		}

		moveShark();
	}

	cout << res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: