From 3c2f6d2157019b0043edffe87b9c60e0f8f29759 Mon Sep 17 00:00:00 2001 From: mordiumaco Date: Mon, 13 Jul 2020 02:15:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?1-6=20=EB=B0=A9=EB=AC=B8=EA=B8=B8=EC=9D=B4?= =?UTF-8?q?=20=EB=8B=A4=EC=8B=9C=20=EC=88=98=EC=A0=95=EC=98=88=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 122 +++++++++++++++++- 1 file changed, 118 insertions(+), 4 deletions(-) diff --git "a/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" "b/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" index 97c8f48..570cc80 100644 --- "a/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" +++ "b/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" @@ -1,10 +1,124 @@ + +import java.util.ArrayList; +import java.util.List; + /* 6. 방문 길이 https://programmers.co.kr/learn/courses/30/lessons/49994 */ -class Solution { - public int solution(String dirs) { - int answer = 0; - return answer; +class Solution6 { + + public static void main(String[] args) { + + String dirs = "ULURRDLLU"; + + System.out.println(solution(dirs)); + } + + public static int solution(String dirs) { + + List visitedList = new ArrayList<>(); + int exceptCount = 0; + Position currentPosition = new Position(0, 0); + visitedList.add(new Position(0, 0)); + + for (String direction : dirs.split("")) { + + switch (direction) { + case "U": + if (currentPosition.getY() >= 5) { + break; + } + currentPosition.addY(1); + break; + case "D": + if (currentPosition.getY() <= -5) { + break; + } + currentPosition.addY(-1); + break; + case "R": + if (currentPosition.getX() >= 5) { + break; + } + currentPosition.addX(1); + break; + case "L": + if (currentPosition.getX() <= -5) { + break; + } + currentPosition.addX(-1); + break; + default: + break; + } + + Position newPosition = new Position(currentPosition.getX(), currentPosition.getY()); + + System.out.println(newPosition.toString()); + + if (!visitedList.contains(newPosition)) { + visitedList.add(newPosition); + } + + } + + // for (Position position : visitedList) { + // if (position.getX() >= -5 || position.getX() >= 5 || position.getY() >= -5 || + // position.getY() >= 5) { + // ignoreSection++; + // } + // } + + System.out.println(visitedList.toString()); + + return visitedList.size() + exceptCount; + } + +} + +class Position { + + private int x; + private int y; + + public Position(int x, int y) { + this.x = x; + this.y = y; + } + + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } + + public void addX(int x) { + this.x += x; + } + + public void addY(int y) { + this.y += y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof Position) && ((Position) obj).getX() == this.getX() + && ((Position) obj).getY() == this.getY(); + } + + @Override + public String toString() { + return "[" + this.x + ":" + this.y + "]"; } } From a29573c2bce0b3e460a5c7cf728d86fafc6f7b40 Mon Sep 17 00:00:00 2001 From: mordiumaco Date: Mon, 13 Jul 2020 23:27:53 +0900 Subject: [PATCH 2/2] =?UTF-8?q?1-6=20=EB=B0=A9=EB=AC=B8=EA=B8=B8=EC=9D=B4?= =?UTF-8?q?=20=EA=B2=A8=EC=9A=B0=ED=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 102 ++++++++++-------- 1 file changed, 55 insertions(+), 47 deletions(-) diff --git "a/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" "b/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" index 570cc80..bd0cdc7 100644 --- "a/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" +++ "b/week1/6. \353\260\251\353\254\270 \352\270\270\354\235\264/Solution.java" @@ -7,44 +7,40 @@ https://programmers.co.kr/learn/courses/30/lessons/49994 */ class Solution6 { - public static void main(String[] args) { - String dirs = "ULURRDLLU"; - + String dirs = "LULLLLLLU"; System.out.println(solution(dirs)); } public static int solution(String dirs) { - List visitedList = new ArrayList<>(); - int exceptCount = 0; - Position currentPosition = new Position(0, 0); - visitedList.add(new Position(0, 0)); + Position currentPosition = new Position(0, 0, 0, 0); + List visitedPositions = new ArrayList<>(); for (String direction : dirs.split("")) { switch (direction) { case "U": - if (currentPosition.getY() >= 5) { + if (currentPosition.getCurrentY() >= 5) { break; } currentPosition.addY(1); break; case "D": - if (currentPosition.getY() <= -5) { + if (currentPosition.getCurrentY() <= -5) { break; } currentPosition.addY(-1); break; case "R": - if (currentPosition.getX() >= 5) { + if (currentPosition.getCurrentX() >= 5) { break; } currentPosition.addX(1); break; case "L": - if (currentPosition.getX() <= -5) { + if (currentPosition.getCurrentX() <= -5) { break; } currentPosition.addX(-1); @@ -53,72 +49,84 @@ public static int solution(String dirs) { break; } - Position newPosition = new Position(currentPosition.getX(), currentPosition.getY()); - - System.out.println(newPosition.toString()); + Position nowPosition = currentPosition.positionFactory(); - if (!visitedList.contains(newPosition)) { - visitedList.add(newPosition); + if (!visitedPositions.contains(nowPosition)) { + visitedPositions.add(nowPosition); } } - // for (Position position : visitedList) { - // if (position.getX() >= -5 || position.getX() >= 5 || position.getY() >= -5 || - // position.getY() >= 5) { - // ignoreSection++; - // } - // } - - System.out.println(visitedList.toString()); - - return visitedList.size() + exceptCount; + return visitedPositions.size(); } - } class Position { - private int x; - private int y; + private int beforeX; + private int beforeY; + private int currentX; + private int currentY; - public Position(int x, int y) { - this.x = x; - this.y = y; + public Position(int beforeX, int beforeY, int currentX, int currentY) { + this.beforeX = beforeX; + this.beforeY = beforeY; + this.currentX = currentX; + this.currentY = currentY; } - public void setX(int x) { - this.x = x; + public int getBeforeX() { + return this.beforeX; } - public void setY(int y) { - this.y = y; + public int getBeforeY() { + return this.beforeY; } - public void addX(int x) { - this.x += x; + public int getCurrentX() { + return this.currentX; } - public void addY(int y) { - this.y += y; + public int getCurrentY() { + return this.currentY; } - public int getX() { - return x; + public void addX(int x) { + this.beforeX = this.currentX; + this.beforeY = this.currentY; + this.currentX += x; } - public int getY() { - return y; + public void addY(int y) { + this.beforeX = this.currentX; + this.beforeY = this.currentY; + this.currentY += y; + } + + public Position positionFactory() { + return new Position(this.beforeX, this.beforeY, this.currentX, this.currentY); } @Override public boolean equals(Object obj) { - return (obj instanceof Position) && ((Position) obj).getX() == this.getX() - && ((Position) obj).getY() == this.getY(); + + if (obj instanceof Position && ((Position) obj).getBeforeX() == this.beforeX + && ((Position) obj).getBeforeY() == this.beforeY && ((Position) obj).getCurrentX() == this.currentX + && ((Position) obj).getCurrentY() == this.currentY) { + return true; + } + + if (obj instanceof Position && ((Position) obj).getBeforeX() == this.currentX + && ((Position) obj).getBeforeY() == this.currentY && ((Position) obj).getCurrentX() == this.beforeX + && ((Position) obj).getCurrentY() == this.beforeY) { + return true; + } + + return false; } @Override public String toString() { - return "[" + this.x + ":" + this.y + "]"; + return "" + this.beforeX + ":" + this.beforeY + "[]" + this.currentX + ":" + this.currentY; } }