본문 바로가기

알고리즘

Offset

4방향 도는 부분에서 k를 기입했어야 하는데, i를 넣어서 처음에 정상실행이 안되었다.

import java.util.Scanner;
public class Main{
    
    public static int[][] arr = new int[20][20];
    //위,오른쪽,아래,왼쪽
    public static int[] dx = new int[]{-1,0,1,0};
    public static int[] dy = new int[]{0,1,0,-1};
    public static void main(String[] args){

      // 이해 (5x5) 배열이 주어짐(상하좌우에 있는 원소보다 작은 것 * 표시)
      // 존재하는 것만 비교한다.
      // 입력 (5x5) 행렬
      // 출력 (별 표함된 행렬)
      
      // 설계
      // v [1] 배열 생성 (2칸 크게) <- 주변을 10으로 만들거임
      // 10 10 10 10 10
      // 10 2  4  1  4
      // 10 2  3  6       // 1은 그대로 4방향과 비교
      // v [2] dx,dy 배열 만들기 (4방향)
      // [3] for(i=1~<=5)
      //      for(j=1~<=5)
      //     돌면서 4개 방향과 비교해서 현재 좌표값(arr[i][j])이 주변보다 작으면 * 출력
      //     그게 아니면 숫자출력
      
      Scanner scan = new Scanner(System.in);
      
      inputMap(scan);
      init();
      for(int i=1;i<=5;i++)
      {
        
        for(int j=1;j<=5;j++)
        {
          // 4방향 돌기
          
          int cnt=0;
          for(int k=0;k<4;k++)
          {
            if(arr[i][j] < arr[i+dx[k]][j+dy[k]])cnt+=1;
          }
          if(cnt==4){System.out.print("* ");}
          else {System.out.print(arr[i][j]+" ");}
        }System.out.println();
      }
      
      
      // printArr();
            
      scan.close();
    }
    
    public static void init()
    {
      for(int i=0;i<=10;i++)
      {
        arr[0][i]=10;//위
        arr[6][i]=10;//아래
        arr[i][0]=10;//왼쪽
        arr[i][6]=10;//오른쪽
      }
    }
    
    public static void printArr()
    {
      for(int i=0;i<=6;i++)
      {
        for(int j=0;j<=6;j++)
        {
          System.out.print(arr[i][j]+"  ");
        }System.out.println();
      }
    }
    
    public static void inputMap(Scanner scan)
    {
      for(int i=1;i<=5;i++)
      {
        for(int j=1;j<=5;j++)
        {
          arr[i][j]=scan.nextInt();
        }
      }
    }
}

'알고리즘' 카테고리의 다른 글

inequal  (0) 2019.08.01
Class President  (0) 2019.08.01
Seat  (0) 2019.08.01
상자색칠  (0) 2019.04.16
소수판별2  (0) 2019.04.16