Created
August 29, 2019 05:25
-
-
Save zeraf29/7f5b4aa34cadbe3e59dbee5511c89965 to your computer and use it in GitHub Desktop.
BotClean - for문 및 indexOf(Str, fromIndeX) 활용
This file contains hidden or 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
| public class Solution { | |
| static void next_move(int posr, int posc, String[] board){ | |
| //add logic here | |
| int targetX = 0, targetY = 0, tempX = 0, gap = 10, fromIndex = 0; | |
| for(int i=0; i<board.length; i++) { | |
| if(board[i].indexOf('d',fromIndex)>-1) { | |
| tempX = board[i].indexOf('d',fromIndex); | |
| if(Math.abs(posc-tempX)+Math.abs(posr-i)<gap ) { | |
| gap = Math.abs(posc-tempX)+Math.abs(posr-i); | |
| targetX = tempX; | |
| targetY = i; | |
| } | |
| i--; | |
| fromIndex = tempX+1; | |
| }else | |
| fromIndex = 0; | |
| } | |
| if(posc==targetX && posr==targetY) { | |
| System.out.println("CLEAN"); | |
| }else if(posc-targetX>0) { | |
| System.out.println("LEFT"); | |
| }else if(posc-targetX<0) { | |
| System.out.println("RIGHT"); | |
| }else if(posr-targetY>0) { | |
| System.out.println("UP"); | |
| }else if(posr-targetY<0) { | |
| System.out.println("DOWN"); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| Scanner in = new Scanner(System.in); | |
| int [] pos = new int[2]; | |
| String board[] = new String[5]; | |
| for(int i=0;i<2;i++) pos[i] = in.nextInt(); | |
| for(int i=0;i<5;i++) board[i] = in.next(); | |
| next_move(pos[0], pos[1], board); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
문제내용> https://www.hackerrank.com/challenges/botclean
배경이론-맨하탄 거리/유클리디안 거리 > https://vsdevelop.tistory.com/2
indexOf를 활용하여 이미 검색된 d 이후의 값을 탐색하며 최소 거리 찾기