Inventory


답안 제출

Points: 10
시간 제한: 20.0s
메모리 제한: 256M

문제 유형
허용된 언어
Python

Assignment #5: Inventory

Problem

As a game developer, you are tasked with implementing a sorting feature for a player's inventory in an RPG game. The inventory is represented as a 2D grid, and each cell can hold a stack of items up to a maximum stack size. To make it easier for players to find specific items, you need to sort these items by their ID in ascending order within the 2D grid.

Each item in the game has the following properties:

  • item_id: An integer representing the unique identifier for the item.

  • stack_size: An integer representing the maximum number of items that can be stacked in a single cell.

Each cell can hold up to the stack_size of a single item. If the stack_size is exceeded, the items will have to be placed in another cell.

Each inventory is represented as a 2D grid of cells, with a fixed height and width. For a given inventory of items, implement the sort_inventory function to sort the items by their item_id in ascending order. The function should return the sorted inventory.

Implement the sort_inventory function:

  • sort_inventory(inventory): Sorts the items in the inventory by their item_id in ascending order.

    • The sorted inventory should be filled from left to right, top to bottom.

    • If a cell is filled to its stack_size, the remaining items should be placed in the next available cell in the same row.

    • If the current row is filled, the remaining items should be placed in the next row, starting from the leftmost cell.

Constraints:

  • 1 <= height, width <= 10^4

  • 1 <= item_id, stack_size <= 10^4

당신은 RPG 게임의 개발자로서, 플레이어의 인벤토리에 있는 아이템들을 정렬하는 기능을 구현해야 합니다. 인벤토리는 2차원 격자로 표현되며, 각 칸(cell)은 하나의 아이템을 일정 수량까지 쌓아 둘 수 있습니다(최대 stack_size 까지). 플레이어가 아이템을 쉽게 찾을 수 있도록, 아이템들을 item_id 기준 오름차순으로 정렬해야 합니다.

각 아이템(item)은 다음과 같은 속성을 가집니다:

  • item_id: 해당 아이템을 고유하게 식별하는 정수입니다.

  • stack_size: 하나의 칸(cell)에 쌓을 수 있는 해당 아이템의 최대 개수입니다.

하나의 cell에는 하나의 item만 들어갈 수 있으며, 최대 stack_size까지만 넣을 수 있습니다. 만약 수량이 그 이상일 경우, 나머지 아이템은 다른 칸에 배치되어야 합니다.

인벤토리(inventory)는 고정된 높이와 너비를 가진 2차원 격자입니다. 주어진 인벤토리의 아이템들을 item_id 기준으로 오름차순 정렬하는 sort_inventory 함수를 구현하세요. 정렬된 인벤토리를 반환해야 합니다.

  • sort_inventory(inventory): item_id를 기준으로 아이템들을 오름차순 정렬합니다.

    • 인벤토리는 좌->우, 상->하 순서로 채워집니다.

    • cell이 해당 아이템의 stack_size만큼 가득 차면, 남은 아이템은 같은 행의 다음 칸에 채웁니다.

    • 행이 다 찼으면, 다음 행의 가장 왼쪽 칸부터 채워야 합니다.

제약 조건:

  • 1 <= height, width <= 10^4

  • 1 <= item_id, stack_size <= 10^4

Input specification

You are first given a list of all "available" items in the game, represented as a list of tuples, where each tuple contains the item's ID and its corresponding maximum stack size. (First line of input)

You are given a inventory, unordered, represented as a 2D list of lists, where each inner list represents a row in the inventory. Each inner list contains tuples representing the item_id and the number of items in the cell. An empty cell is represented by -.

모든 "사용 가능한" 아이템들의 리스트가 먼저 주어집니다. 이 리스트는 (item_id, stack_size) 형태의 튜플들로 구성됩니다. (첫 번째 줄)

이어서 인벤토리의 내용이 주어집니다. 이는 2차원 리스트 형태이며, 각 내부 리스트는 행(row)을 나타냅니다. 각 원소는 (item_id, 수량) 형태의 튜플이며, 비어있는 칸은 -로 표시됩니다.

Output specification

Return the sorted inventory after sorting the items by their item_id in ascending order.

아이템들을 item_id 기준으로 정렬한 후, 정렬된 인벤토리를 반환하세요.

Sample 0
Copy
<< in
(1,10),(2,20),(3,10)
(2,10),(1,10),(3,10)

>> out
(1,10),(2,10),(3,10)
Sample 1
Copy
<< in
(1,10),(2,20),(3,10)
(1,5),(2,10),(3,9)
-,(3,2),(1,5)

>> out
(1,10),(2,10),(3,10)
(3,1),-,-
Sample 2
Copy
<< in
(5,10),(6,20),(7,10)
-,-,-
-,-,-
(5,10),(6,10),(7,10)

>> out
(5,10),(6,10),(7,10)
-,-,-
-,-,-

More sample data: link


코멘트


  • 1
    mssggg  commented 42일 전

    교수님 메모리 제한이 잘못 설정된 것 같습니다.


  • 6
    wwwwje2008  commented 43일 전

    메모리 제한이 256M 이 아닌 256K 로 잘못 설정되어있는 것 같습니다