Skip to content

Commit 0bc62b7

Browse files
committed
add: 백준 17143 낚시왕 문제 등록
1 parent 181923e commit 0bc62b7

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
"""
3+
문제 이름: 낚시왕
4+
문제 번호: 17143
5+
문제 링크: https://www.acmicpc.net/problem/17143
6+
난이도: Gold II
7+
태그: 구현, 시뮬레이션
8+
"""
9+
import sys
10+
from typing import Tuple
11+
12+
13+
def input(): return sys.stdin.readline().rstrip()
14+
15+
TOP = 1
16+
DOWN = 2
17+
RIGHT = 3
18+
LEFT = 4
19+
20+
direction_reverse = {TOP: DOWN,
21+
DOWN: TOP,
22+
RIGHT: LEFT,
23+
LEFT: RIGHT}
24+
25+
class Shark:
26+
def __init__(self, x, y, s, d, z) -> None:
27+
self.x = x
28+
self.y = y
29+
self.speed = s
30+
self.direction = d
31+
self.size = z
32+
33+
def reverse_direction(self) -> None:
34+
self.direction = direction_reverse[self.direction]
35+
36+
def move(self, x, y) -> Tuple:
37+
self.x = x
38+
self.y = y
39+
return x, y
40+
41+
def __check_boundry(self, width, hegiht) -> Tuple:
42+
43+
if self.x > width or self.x < 0:
44+
self.reverse_direction()
45+
dis = abs(self.x-width)
46+
47+
return (False, dis if self.direction == 4 else dis)
48+
49+
if self.y > hegiht or self.y < 0:
50+
self.reverse_direction()
51+
dis = abs(self.x-hegiht)-1
52+
53+
return (False, dis if self.direction == 2 else dis)
54+
55+
return (True, 0)
56+
57+
def moveVec(self, width, hegiht) -> Tuple:
58+
"""
59+
@return: 새로운 좌표 (x, y)
60+
"""
61+
speed = self.speed
62+
direction = self.direction
63+
cur_x, cur_y = self.x, self.y
64+
65+
if direction == 1:
66+
self.move(cur_x, cur_y-speed)
67+
c = self.__check_boundry(width, hegiht)
68+
if not c[0]:
69+
self.move(self.x, c[1])
70+
return self.x, self.y
71+
72+
if direction == 2:
73+
self.move(cur_x, cur_y+speed)
74+
c = self.__check_boundry(width, hegiht)
75+
if not c[0]:
76+
self.move(self.x, c[1])
77+
return self.x, self.y
78+
79+
if direction == 3:
80+
self.move(cur_x+speed, cur_y)
81+
c = self.__check_boundry(width, hegiht)
82+
if not c[0]:
83+
self.move(c[1], self.y)
84+
return self.x, self.y
85+
86+
if direction == 4:
87+
self.move(cur_x-speed, cur_y)
88+
c = self.__check_boundry(width, hegiht)
89+
if not c[0]:
90+
self.move(c[1], self.y)
91+
return self.x, self.y
92+
93+
def __str__(self) -> str:
94+
direction_text = {TOP: "위", DOWN: "아래", RIGHT: "우측", LEFT: "좌측"}
95+
return f"{self.x}|{self.y}|{direction_text[self.direction]}"
96+
97+
98+
class Grid:
99+
def __init__(self, width, height):
100+
self.__grid = [[None]*width for _ in range(height)]
101+
self.sharks: list[Shark] = []
102+
self.width = width
103+
self.height = height
104+
105+
def add_shark(self, s: Shark) -> None:
106+
self.__grid[s.y][s.x] = s
107+
self.sharks.append(s)
108+
109+
def move_sharks(self):
110+
111+
for shark in self.sharks:
112+
before_x, before_y = shark.x, shark.y
113+
new_pos = shark.moveVec(self.width, self.height)
114+
115+
self.__grid[before_y][before_x] = None
116+
self.__grid[new_pos[1]][new_pos[0]] = shark
117+
118+
def print_grid(self):
119+
for y in range(self.height):
120+
print(f"{', '.join(map(str,self.__grid[y]))}")
121+
122+
123+
g = Grid(6, 4)
124+
125+
g.add_shark(Shark(2, 0, 5, DOWN, 1))
126+
g.print_grid()
127+
g.move_sharks()
128+
print("")
129+
g.print_grid()
130+
g.move_sharks()
131+
print("")
132+
g.print_grid()
133+
134+
# width, height, shark_count = map(int, input().split())
135+
136+
# make grid
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
file: "17143.md"
3+
name: "낚시왕"
4+
src: "https://www.acmicpc.net/problem/17143"
5+
tags:
6+
- 구현
7+
- 시뮬레이션
8+
done: false
9+
draft: false
10+
level: 14
11+
difficulty: "Gold II"
12+
date: 2021-11-09
13+
---

0 commit comments

Comments
 (0)