-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from HYU-PS-STUDY/clean2001
[week-11] 2304, 17837
- Loading branch information
Showing
3 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Main { | ||
static int N, K; | ||
static LinkedList<Node>[][] map; | ||
static int[][] color; | ||
static ArrayList<Node> list = new ArrayList<>(); | ||
|
||
static class Node { | ||
int num; | ||
int direction; | ||
int y, x; | ||
|
||
Node(int num, int direction, int y, int x) { | ||
this.num = num; | ||
this.direction = direction; | ||
this.y = y; | ||
this.x = x; | ||
} | ||
} | ||
|
||
static int[] dy = {0, 0, -1, 1}; | ||
static int[] dx = {1, -1, 0, 0}; | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
N = Integer.parseInt(st.nextToken()); | ||
K = Integer.parseInt(st.nextToken()); | ||
|
||
map = new LinkedList[N+1][N+1]; | ||
for(int i=0; i<N+1; ++i) { | ||
for(int j=0; j<N+1; ++j) { | ||
map[i][j] = new LinkedList<Node>(); | ||
} | ||
} | ||
color = new int[N+1][N+1]; | ||
|
||
|
||
// 색 입력 받기 | ||
for(int i=1; i<=N; ++i) { | ||
st = new StringTokenizer(br.readLine()); | ||
for(int j=1; j<=N; ++j) { | ||
color[i][j] = Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
|
||
list.add(null); | ||
for(int i=1; i<=K; ++i) { | ||
st = new StringTokenizer(br.readLine()); | ||
int r = Integer.parseInt(st.nextToken()); | ||
int c = Integer.parseInt(st.nextToken()); | ||
int d = Integer.parseInt(st.nextToken()); | ||
|
||
list.add(new Node(i, d-1, r ,c)); | ||
map[r][c].add(list.get(i)); // 말을 체스판에 놓기 | ||
} | ||
|
||
|
||
// 게임 시작 | ||
int i; | ||
boolean flag = false; | ||
for(i=1; i<=1000; ++i) { | ||
for(int j=1; j<=K; ++j) { // 말 하나씩 이동 | ||
Node n = list.get(j); | ||
int ny = n.y + dy[n.direction]; | ||
int nx = n.x + dx[n.direction]; | ||
|
||
if(ny > N || nx > N || ny < 1 || nx < 1) { // 밖으로 나감 -> 방향 바꾸고 한칸 가기 (파랑이거나 바깥이면 안가기) | ||
if(n.direction==0) n.direction =1; | ||
else if(n.direction==1) n.direction = 0; | ||
else if(n.direction==2) n.direction = 3; | ||
else if(n.direction==3) n.direction = 2; | ||
if(move(j)) { | ||
flag = true; | ||
break; | ||
} | ||
} else if(color[ny][nx] == 0) { // 그냥 가면 됨 | ||
if(move(j)) { | ||
flag = true; | ||
break; | ||
} | ||
} else if(color[ny][nx] == 1) { // 빨강 | ||
if(move(j)) { | ||
flag = true; | ||
break; | ||
} | ||
} else if(color[ny][nx] == 2) { // 파랑 | ||
if(n.direction==0) n.direction =1; | ||
else if(n.direction==1) n.direction = 0; | ||
else if(n.direction==2) n.direction = 3; | ||
else if(n.direction==3) n.direction = 2; | ||
if(move(j)) { | ||
flag = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if(flag) break; | ||
|
||
} | ||
|
||
if (i >= 1001) { | ||
System.out.println(-1); | ||
} else { | ||
System.out.println(i); | ||
} | ||
|
||
} | ||
|
||
static boolean move(int node) { // 노드 번호 | ||
Node n = list.get(node); | ||
// 새로 가야하는 곳 | ||
int ny = n.y + dy[n.direction]; | ||
int nx = n.x + dx[n.direction]; | ||
|
||
int cur_y = n.y; | ||
int cur_x = n.x; | ||
|
||
|
||
if (ny > N || nx > N || ny < 1 || nx < 1) { | ||
// 밖이거나 파랑이면 가지 않는다. | ||
return map[cur_y][cur_x].size() >= 4; | ||
} else if(color[ny][nx] == 2) { // 4개인지만 체크 | ||
return map[ny][nx].size() >= 4; | ||
} | ||
|
||
|
||
|
||
// 내가 덱에서 몇번째인지 찾기 | ||
int idx = map[cur_y][cur_x].indexOf(n); | ||
|
||
// 나와 내 위에 있는거 옮겨주기 | ||
List<Node> moved = new ArrayList<>(); | ||
if(color[ny][nx] == 0) { | ||
for (int i = idx; i < map[cur_y][cur_x].size(); ++i) { | ||
Node cur = map[cur_y][cur_x].get(i); | ||
moved.add(cur); | ||
map[ny][nx].addLast(cur); // 위에 쌓기 | ||
cur.y = ny; | ||
cur.x = nx; | ||
} | ||
} else { // 빨강 | ||
|
||
for (int i = map[cur_y][cur_x].size()-1; i >= idx ; --i) { | ||
Node cur = map[cur_y][cur_x].get(i); | ||
moved.add(cur); | ||
map[ny][nx].addLast(cur); // 위에 쌓기 | ||
cur.y = ny; | ||
cur.x = nx; | ||
} | ||
|
||
} | ||
|
||
// 옮겨졌던 것들을 제거 | ||
for(Node m : moved) map[cur_y][cur_x].remove(m); | ||
|
||
|
||
if (map[ny][nx].size() >= 4) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
// 2304. 창고 다각형 | ||
class Main { | ||
static int N; | ||
static ArrayList<Node> list = new ArrayList<>(); | ||
static int[] arr = new int[1001]; | ||
static class Node { | ||
int x, y; | ||
|
||
Node(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
N = Integer.parseInt(br.readLine()); | ||
|
||
int max_x = 0, max_y = 0; | ||
for(int i=0; i<N; ++i) { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int x = Integer.parseInt(st.nextToken()); | ||
int y = Integer.parseInt(st.nextToken()); | ||
|
||
arr[x] = y; | ||
|
||
if(max_y < y) { // 높이가 가장 높은 기둥의 위치를 찾아주기 | ||
max_x = x; | ||
max_y = y; | ||
} | ||
|
||
} | ||
|
||
|
||
int area = max_y; // 가운데것을 더해주고 시작 | ||
|
||
// 가장 높은 것을 기준으로 왼쪽 | ||
Deque<Node> deque = new LinkedList<>(); // 스택처럼 사용 | ||
for(int i=0; i<=max_x; ++i) { | ||
if(arr[i] == 0) continue; | ||
if(deque.isEmpty()) { | ||
deque.addFirst(new Node(i, arr[i])); | ||
} else if(deque.peekFirst().y <= arr[i]) { // 다음 기둥이 더 크거나 **같음** | ||
int dx = i - deque.peekFirst().x; | ||
int dy = deque.peekFirst().y; | ||
|
||
area += dy*dx; | ||
deque.addFirst(new Node(i, arr[i])); | ||
} | ||
} | ||
|
||
|
||
deque = new LinkedList<>(); // 스택처럼 사용 | ||
for(int i=1000; i>=max_x; --i) { | ||
if(arr[i] == 0) continue; | ||
if(deque.isEmpty()) { | ||
deque.addFirst(new Node(i, arr[i])); | ||
} else if(deque.peekFirst().y <= arr[i]) { // 다음 기둥이 더 크거나 **같음** | ||
int dx = deque.peekFirst().x - i; | ||
int dy = deque.peekFirst().y; | ||
|
||
area += dy*dx; | ||
deque.addFirst(new Node(i, arr[i])); | ||
} | ||
} | ||
|
||
System.out.println(area); | ||
} | ||
} |