본문 바로가기
algorithm

[JAVA] 로봇 청소기 (백준 14503)

by onejunu 2020. 8. 12.

 

 

자바 스트림을 좀 활용하고 싶은데 맘처럼 잘 안된다. 스트림을 잘 활용하면 정말 편하게 풀수 있을거 같은데..

 

알고리즘을 다 주어줬기 때문에 구현만 하면 된다. 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

import static java.lang.System.*;


public class Main{
    static int n,m;
    static final int[][] d4 = {{-1,0},{0,1},{1,0},{0,-1}};

    public static void main(String[] args) throws IOException{
        FastScanner fs = new FastScanner();
        n = fs.nextInt();
        m = fs.nextInt();
        int[][] arr = new int[n][m];
        Pos startPos = new Pos(fs.nextInt(),fs.nextInt(),fs.nextInt());

        for(int i=0;i<n;i++) arr[i] = fs.readArray(m);
        //print2DArray(arr);
        out.println(solve(arr,startPos));



    }


    public static int solve(int[][] arr,Pos cur){
        int ans = 0;
        int[][] visited = new int[n][m];
        boolean ok = false;

        while(true){
            ok = false;
            //1. clean curPos
            visited[cur.x][cur.y] = 1;
            int curDir = cur.dir;
            //2. 4 direction from curDir
            for(int i =0;i<4;i++){
                curDir = curDir==0?3:curDir-1;
                int nx = cur.x + d4[curDir][0];
                int ny = cur.y + d4[curDir][1];

                if(nx>0 && nx<n-1
                && ny>0 && ny<m-1
                && visited[nx][ny]==0
                && arr[nx][ny]==0){
                    ok = true;
                    cur.x = nx;
                    cur.y = ny;
                    cur.dir = curDir;
                    break;
                }
            }
            //
            if(!ok){
                int backX = cur.x - d4[curDir][0];
                int backY = cur.y - d4[curDir][1];
                if(arr[backX][backY]==0){
                    ok = true;
                    cur.x = backX;
                    cur.y = backY;
                }
            }

            if(!ok) break;
        }


        return Arrays.stream(visited).flatMapToInt(Arrays::stream).sum();
    }

    public static void print2DArray(int[][] arr){
        for(int[] a : arr){
            for(int x: a){
                out.print(x);
            }
            out.println();
        }
    }

    public static class Pos{
        int x,y,dir;

        public Pos(int x, int y,int dir) {
            this.x = x;
            this.y = y;
            this.dir = dir;
        }
    }

    public static class FastScanner{
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        StringTokenizer st = new StringTokenizer("");

        public String next() throws IOException {
            while(!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }
        public int nextInt() throws IOException{
            return Integer.parseInt(next());
        }
        public int[] readArray(int n) throws IOException{
            int[] a = new int[n];
            for(int i=0;i<n;i++) a[i] = nextInt();
            return a;
        }

    }
}

 

 

댓글