넘치게 채우기

[BOJ] 17143 - 낚시왕 본문

PS/BOJ

[BOJ] 17143 - 낚시왕

riveroverflow 2025. 2. 5. 17:58
728x90
반응형

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

BOJ - 낚시왕

문제 유형: 구현, 시뮬레이션

문제 난이도: Gold I

시간 제한: 1초

메모리 제한: 512MB

 

문제

낚시왕이 상어 낚시를 하는 곳은 크기가 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인 경우는 왼쪽을 의미한다.

두 상어가 같은 크기를 갖는 경우는 없고, 하나의 칸에 둘 이상의 상어가 있는 경우는 없다.

 

출력

낚시왕이 잡은 상어 크기의 합을 출력한다.

 

풀이

그냥 빡구현이다.

1~3과정을 시뮬레이션하면 되는데,

배열에 물고기정보를 담는데, 물고기정보중에 현재 살아있는지 여부를 담는 불리언도 같이 담는다.

낚시왕이 i열에서 물고기배열을 순회하여 i열인 가장 높은 물고기를 잡고 처리한다음에, 크기를 누적한다.

그 뒤, 물고기 시뮬레이션을 해준다.

물고기의 움직임 계산을 어떻게 하였는지에 대해서는 코드를 참조.. toWall은 현재 바라보는 벽에 대한 거리, toNextWall은 반대 벽까지의 거리이다. 이를 이용해서 움직임 계산을 할 수 있다.

가상의 2D배열에 물고기의 id를 더 큰 물고기의 id고 갱신했다.

2D배열을 읽어서 물고기 배열의 정보를 새로 업데이트해주면 이동 완료이다.

 

코드

C++

#include <bits/stdc++.h>

using namespace std;

struct shark {
  int id;
  int r;
  int c;
  int s;
  int d;
  int z;
  bool isAlive;

  shark(int id, int r, int c, int s, int d, int z)
      : id(id), r(r), c(c), s(s), d(d), z(z), isAlive(true) {}
};

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int r, c, m;
  cin >> r >> c >> m;

  vector<shark *> arr(m);
  for (int i = 0; i < m; ++i) {
    int row, col, s, d, z;
    cin >> row >> col >> s >> d >> z;
    if (d == 1 || d == 2) {
      if (s >= 2 * (r - 1))
        s %= 2 * (r - 1);
    } else {
      if (s >= 2 * (c - 1))
        s %= 2 * (c - 1);
    }
    arr[i] = new shark(i, row - 1, col - 1, s, d, z);
  }

  int ans = 0;
  for (int i = 0; i < c; ++i) {
    int highestID = -1;
    int highestRow = r;
    for (const auto &sh : arr) {
      if (!(sh->isAlive) || sh->c != i)
        continue;
      if (highestRow > sh->r) {
        highestRow = sh->r;
        highestID = sh->id;
      }
    }

    if (highestID != -1) {
      arr[highestID]->isAlive = false;
      ans += arr[highestID]->z;
    }

    vector<vector<int>> tempMat(r, vector<int>(c, -1));
    for (const auto &sh : arr) {
      if (!(sh->isAlive))
        continue;
      int dir = sh->d;
      int newRow;
      int newCol;
      if (dir == 1) {
        newCol = sh->c;
        int toWall = sh->r;
        int toNextWall = toWall + r - 1;
        if (sh->s <= toWall) {
          newRow = (sh->r) - (sh->s);
          if (newRow == 0)
            sh->d = 2;
        } else if (sh->s <= toNextWall) {
          newRow = (sh->s) - toWall;
          if (newRow != r - 1)
            sh->d = 2;
        } else {
          newRow = r - 1 - ((sh->s) - toNextWall);
        }
      } else if (dir == 2) {
        newCol = sh->c;
        int toWall = r - 1 - (sh->r);
        int toNextWall = toWall + r - 1;
        if (sh->s <= toWall) {
          newRow = (sh->r) + (sh->s);
          if (newRow == r - 1)
            sh->d = 1;
        } else if (sh->s <= toNextWall) {
          newRow = toNextWall - (sh->s);
          if (newRow != 0)
            sh->d = 1;
        } else {
          newRow = (sh->s) - toNextWall;
        }
      } else if (dir == 3) {
        newRow = sh->r;
        int toWall = c - 1 - (sh->c);
        int toNextWall = toWall + c - 1;
        if (sh->s <= toWall) {
          newCol = (sh->c) + (sh->s);
          if (newCol == c - 1)
            sh->d = 4;
        } else if (sh->s <= toNextWall) {
          newCol = toNextWall - (sh->s);
          if (newCol != 0)
            sh->d = 4;
        } else {
          newCol = (sh->s) - toNextWall;
        }
      } else {
        newRow = sh->r;
        int toWall = sh->c;
        int toNextWall = toWall + c - 1;
        if (sh->s <= toWall) {
          newCol = (sh->c) - (sh->s);
          if (newCol == 0)
            sh->d = 3;
        } else if ((sh->s) <= toNextWall) {
          newCol = (sh->s) - toWall;
          if (newCol != c - 1)
            sh->d = 3;
        } else {
          newCol = c - 1 - ((sh->s) - toNextWall);
        }
      }

      if (tempMat[newRow][newCol] == -1) {
        tempMat[newRow][newCol] = sh->id;
      } else {
        if (arr[tempMat[newRow][newCol]]->z < sh->z) {
          arr[tempMat[newRow][newCol]]->isAlive = false;
          tempMat[newRow][newCol] = sh->id;
        } else {
          sh->isAlive = false;
        }
      }
    }

    for (int row = 0; row < r; ++row) {
      for (int col = 0; col < c; ++col) {
        if (tempMat[row][col] != -1) {
          arr[tempMat[row][col]]->r = row;
          arr[tempMat[row][col]]->c = col;
        }
      }
    }
  }

  cout << ans << "\n";

  return 0;
}

 

Go

package main

import (
	"bufio"
	"fmt"
	"os"
)

type shark struct {
	id      int
	r       int
	c       int
	s       int
	d       int
	z       int
	isAlive bool
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var r, c, m int
	fmt.Fscan(reader, &r, &c, &m)

	arr := make([]*shark, m)
	for i := 0; i < m; i++ {
		var row, col, s, d, z int
		fmt.Fscan(reader, &row, &col, &s, &d, &z)

		if d == 1 || d == 2 {
			s %= 2 * (r - 1)
		} else {
			s %= 2 * (c - 1)
		}

		arr[i] = &shark{i, row - 1, col - 1, s, d, z, true}
	}

	ans := 0

	for i := 0; i < c; i++ {
		highestID := -1
		highestRow := r

		for _, sh := range arr {
			if !sh.isAlive || sh.c != i {
				continue
			}
			if highestRow > sh.r {
				highestRow = sh.r
				highestID = sh.id
			}
		}

		if highestID != -1 {
			arr[highestID].isAlive = false
			ans += arr[highestID].z
		}

		tempMat := make([][]int, r)
		for i := 0; i < r; i++ {
			tempMat[i] = make([]int, c)
			for j := 0; j < c; j++ {
				tempMat[i][j] = -1
			}
		}

		for _, sh := range arr {
			if !sh.isAlive {
				continue
			}

			dir := sh.d
			newRow, newCol := sh.r, sh.c

			if dir == 1 {
				toWall := sh.r
				toNextWall := toWall + r - 1
				if sh.s <= toWall {
					newRow = sh.r - sh.s
					if newRow == 0 {
						sh.d = 2
					}
				} else if sh.s <= toNextWall {
					newRow = sh.s - toWall
					if newRow != r-1 {
						sh.d = 2
					}
				} else {
					newRow = r - 1 - (sh.s - toNextWall)
				}
			} else if dir == 2 {
				toWall := r - 1 - sh.r
				toNextWall := toWall + r - 1
				if sh.s <= toWall {
					newRow = sh.r + sh.s
					if newRow == r-1 {
						sh.d = 1
					}
				} else if sh.s <= toNextWall {
					newRow = toNextWall - sh.s
					if newRow != 0 {
						sh.d = 1
					}
				} else {
					newRow = sh.s - toNextWall
				}
			} else if dir == 3 {
				toWall := c - 1 - sh.c
				toNextWall := toWall + c - 1
				if sh.s <= toWall {
					newCol = sh.c + sh.s
					if newCol == c-1 {
						sh.d = 4
					}
				} else if sh.s <= toNextWall {
					newCol = toNextWall - sh.s
					if newCol != 0 {
						sh.d = 4
					}
				} else {
					newCol = sh.s - toNextWall
				}
			} else {
				toWall := sh.c
				toNextWall := toWall + c - 1
				if sh.s <= toWall {
					newCol = sh.c - sh.s
					if newCol == 0 {
						sh.d = 3
					}
				} else if sh.s <= toNextWall {
					newCol = sh.s - toWall
					if newCol != c-1 {
						sh.d = 3
					}
				} else {
					newCol = c - 1 - (sh.s - toNextWall)
				}
			}

			if tempMat[newRow][newCol] == -1 {
				tempMat[newRow][newCol] = sh.id
			} else {
				if arr[tempMat[newRow][newCol]].z < sh.z {
					arr[tempMat[newRow][newCol]].isAlive = false
					tempMat[newRow][newCol] = sh.id
				} else {
					sh.isAlive = false
				}
			}
		}

		for row := 0; row < r; row++ {
			for col := 0; col < c; col++ {
				if tempMat[row][col] != -1 {
					arr[tempMat[row][col]].r = row
					arr[tempMat[row][col]].c = col
				}
			}
		}
	}

	fmt.Fprintln(writer, ans)
}
728x90
반응형

'PS > BOJ' 카테고리의 다른 글

[BOJ] 16566 - 카드 게임  (0) 2025.02.06
[BOJ] 2162 - 선분 그룹  (0) 2025.02.04
[BOJ] 15649 - N과 M (1)  (0) 2025.02.03
[BOJ] 2583 - 영역 구하기  (0) 2025.02.03
[BOJ] 7562 - 나이트의 이동  (0) 2025.02.03