Back

[Java] swea 2105 디저트 카페

문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5VwAr6APYDFAWu&categoryId=AV5VwAr6APYDFAWu&categoryType=CODE?

코드

import java.util.*;
public class Solution {
    static int[][] map;
    static boolean[][] visited;
    static boolean[] check;
    static int N,max,startx,starty;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int t = 0;t<T;t++) {
            N = sc.nextInt();
            map = new int[N][N];
            visited = new boolean[N][N];
            check = new boolean[101];
            for(int i=0;i<N;i++)
                for(int j=0;j<N;j++)
                    map[i][j]=sc.nextInt();
            max = -1;
            for(int i=0;i<N;i++) {
                for(int j=0;j<N;j++) {
                    startx=i;
                    starty=j;
                    go(i,j,0);
                }
            }
            System.out.println("#"+(t+1)+" "+max);
        }
    }
    static void go(int movex,int movey,int dir) {
        check[map[movex][movey]]=true;
        if(dir==4&&startx==movex&&starty==movey) {
            int cnt=0;
            for(int i=0;i<101;i++)
                if(check[i]) cnt++;
            if(cnt%2!=0) return;
            if(cnt>max) max=cnt;
            check[map[movex][movey]]=false;
        }else {
            if(dir==0) {
                if(movex+1<N&&movey+1<N&&!check[map[movex+1][movey+1]])
                    go(movex+1,movey+1,1);
            }
            else if(dir==1) {
                if(movex+1<N&&movey+1<N&&!check[map[movex+1][movey+1]])
                    go(movex+1,movey+1,1);
                if(movex+1<N&&movey-1>=0&&!check[map[movex+1][movey-1]])
                    go(movex+1,movey-1,2);
            }
            else if(dir==2) {
                if(movex+1<N&&movey-1>=0&&!check[map[movex+1][movey-1]])
                    go(movex+1,movey-1,2);
                if(movex-1>=0&&movey-1>=0&&!check[map[movex-1][movey-1]])
                    go(movex-1,movey-1,3);
            }else if(dir==3) {
                if(movex-1>=0&&movey-1>=0&&!check[map[movex-1][movey-1]])
                    go(movex-1,movey-1,3);
                if(startx==movex-1&&starty==movey+1) go(movex-1,movey+1,4);
                else {
                    if(movex-1>=0&&movey+1<N&&!check[map[movex-1][movey+1]])
                        go(movex-1,movey+1,4);
                }
            }else {
                if(startx==movex-1&&starty==movey+1) go(movex-1,movey+1,4);
                else {
                    if(movex-1>=0&&movey+1<N&&!check[map[movex-1][movey+1]])
                        go(movex-1,movey+1,4);
                }
                    
            }
        }
        check[map[movex][movey]]=false;
    }
}
comments powered by Disqus