본문 바로가기
Coding Test/CodeUp

CodeUp_Java 기초 100제 - [기초-2차원배열] 96~99

by SeleniumBindingProtein 2022. 2. 14.
728x90
반응형

96 : 바둑판에 흰돌 놓기

//자바코드
import java.util.Scanner;

public class Main{
	public static void main (String[] args){
    	Scanner sc = new Scanner(System.in);
		
		int num = sc.nextInt();
        int[][] arr = new int[19][19];
        
        for(int i=0;i<arr.length;i++){
        	for(int j=0;j<arr[i].length;j++){
        		arr[i][j] = 0;	//바둑판 0으로 초기화
        	}
        }
        
        for(int i=0;i<num;i++){
        	arr[sc.nextInt() - 1][sc.nextInt() - 1] = 1; //흰돌 올려놓기
        }
        
        for(int i=0;i<arr.length;i++){
        	for(int j=0;j<arr[i].length;j++){
        		System.out.print(arr[i][j]+" "); //불린 횟수만큼 값 증가
        	}
            System.out.println(); 
        }
        
        sc.close();
   }
}

97 : 바둑알 십자 뒤집기

 

//자바코드
import java.util.Scanner;

public class Main{
	public static void main (String[] args){
    	Scanner sc = new Scanner(System.in);
		
        int[][] a = new int[19][19];
        
        for(int i=0;i<a.length;i++){
        	for(int j=0;j<a[i].length;j++){
        		a[i][j] = sc.nextInt();	//바둑판 0으로 초기화
        	}
        }
        
        int num = sc.nextInt();
        int x = 0, y =0;
        
        
        for(int n=0;n<num;n++){
        	x=sc.nextInt()-1;
            y=sc.nextInt()-1;
            
            for(int i = 0; i< a.length;i++){
            	for(int j=0;j<a[i].length;j++){
                	//i가 x인 모든 데이터, j가 y인 모든 데이터
                	if(i==x||j==y){
                        //입력한 좌표의 값은 그대로 저장
                        if(i==x && j==y){
                            a[i][j] = a[i][j];
                            //입력한 좌표를 제외한 데이터 중 값이 0이면 1로 저장
                        } else if(a[i][j] == 0){
                            a[i][j] = 1;
                        } else {	//입력한 좌표를 제외한 데이터 중 값이 1이면 0으로 저장
                            a[i][j] = 0;
                        }
                    }
                }
            }
        }
        
        for(int i=0;i<a.length;i++){
        	for(int j=0;j<a[i].length;j++){
        		System.out.print(a[i][j]+" "); 
        	}
            System.out.println(); 
        }
        
        sc.close();
   }
}

98 : 설탕과자 뽑기

//자바코드
import java.util.Scanner;

public class Main{
	public static void main (String[] args){
    	Scanner sc = new Scanner(System.in);
		
        int length = 0, direct = 0;
        int x = 0, y =0;
        int width = sc.nextInt();
        int height = sc.nextInt();
        int[][] arr = new int[width][height];
        
        for(int i = 0;i<width;i++){
        	for(int j = 0;j<height;j++){
            	arr[i][j] = 0;
            }    
        }
        
        
        int input = sc.nextInt();
        for(int i=0;i<input;n++){
        	length = sc.nextInt();
            direct = sc.nextInt();
        	x=sc.nextInt();
            y=sc.nextInt();
            
            if(direct ==0) {  //막대의 방향이 가로일때(열만 변경)
                for(int j = y-1;j<y-1+length;j++){	//열 변경 
                    arr[j][x-1]=1;
                }
            } else{   //막대의 방향이 세로일때(행만 변경)
                for(int j = x-1;j<x-1+length;j++){	//행 변경 
                    arr[j][y-1]=1;
            	}
            }
        }

        for(int i=0;i<width;i++){
        	for(int j=0;j<height;j++){
        		System.out.print(arr[i][j]+" "); 
        	}
            System.out.println(); 
        }
        
        sc.close();
   }
}

99 : 성실한 개미

 

//자바코드
import java.util.*;

public class Main{
	public static void main (String[] args){
    	Scanner sc = new Scanner(System.in);
		int[][] arr = new int[11][11];
        
        for(int i = 1;i<11;i++){
        	String a = sc.nextLine();
            Stirng b[] = a.split("");
        	for(int j = 0;j<10;j++){
            	arr[i][j+1] = Integer.parseInt(b[j]);
            }    
        }
     
        int x = 2, y =2;
        
        while(true){
       		if(arr[x][y]==2{
            	arr[x][y]=9;
                break;
            }
            
            if(arr[x][y+1]==1{
            	if(arr[x+1][y]==1) break;
                else x++;
            }
            
            else if(arr[x][y+1]!=1) y++;
            
            if(arr[x][y]==2{
            	arr[x][y]=9;
                break;
            }
            
            arr[x][y]=9;
        }
        
        arr[2][2]=9;
        for(int i=1;i<11;i++){
        	for(int j=1;j<11;j++){
            	System.out.print(arr[i][j]+"");
            }
            System.out.println("");
        }
        
        sc.close();
   }
}
728x90
반응형

댓글