영원히 흘러가는 강
코드업 100제 (1096~1099) 본문
728x90
1096. 바둑판에 올려 놓을 흰 돌의 개수(n)가 첫 줄에 입력된다.
둘째 줄 부터 n+1 번째 줄까지 힌 돌을 놓을 좌표(x, y)가 n줄 입력된다.
n은 10이하의 자연수이고 x, y 좌표는 1 ~ 19 까지이며, 같은 좌표는 입력되지 않는다.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[][]=new int[20][20];
int n=sc.nextInt();
for(int i=0;i<n;i++) {
int x=sc.nextInt();
int y=sc.nextInt();
arr[x-1][y-1]=1;
}
for(int j=0;j<arr.length-1;j++) {
for(int z=0;z<arr.length-1;z++) {
System.out.print(arr[j][z]+" ");
}
System.out.println();
}
}
}
줄 바꿈 문제로 시간 걸림.
---------------------------------------------
1097
--------------------------------------------2020 /10/05-------------------------
1098. 첫 줄에 격자판의 세로(h), 가로(w) 가 공백을 두고 입력되고,
두 번째 줄에 놓을 수 있는 막대의 개수(n)
세 번째 줄부터 각 막대의 길이(l), 방향(d), 좌표(x, y)가 입력된다.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int[][] arr = new int[a][b];
int n=sc.nextInt();
int d,l,x,y;
for(int i=0;i<n;i++) {
l=sc.nextInt();
d=sc.nextInt();
x=sc.nextInt();
y=sc.nextInt();
if(d==0) {
for(int z=y;z<y+l;z++) {
arr[x-1][z-1]=1;
}
}
else{
for(int z=x;z<x+l;z++) {
arr[z-1][y-1]=1;
}
}
}
for(int i=0;i<arr.length;i++) {
for(int j=0; j<arr.length;j++) {
System.out.print(arr[i][j]+ " ");
}
System.out.println(" ");
}
}
}
1099. 성실한 개미
미로 상자의 구조가 0(갈 수 있는 곳), 1(벽 또는 장애물)로 주어지고,
먹이가 2로 주어질 때, 성실한 개미의 이동 경로를 예상해보자.
public class main2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] arr = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
arr[i][j] = scanner.nextInt();
}
}
int flag = 1;
int end = 0;
for (int i = 1; i < arr.length; i++) {
if (end != 1) {
for (int j = flag; j < arr.length; j++) {
if (arr[i][j] == 0) {
arr[i][j] = 9;
} else if (arr[i][j] == 2) {
arr[i][j] = 9;
end = 1;
break;
} else {
flag = j - 1;
break;
}
}
} else {
break;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
배열 상으로 우측으로 이동 후 벽을 만나면 밑으로 진행
728x90
'알고리즘' 카테고리의 다른 글
백준 알고리즘 재귀 (0) | 2020.10.08 |
---|---|
백준 알고리즘 (0) | 2020.10.07 |
코드업 100제 (1091~1095) (0) | 2020.09.25 |
코드업 100제(1086~1090) (0) | 2020.09.25 |
코드업 100제 (1081~1085) (0) | 2020.09.24 |
Comments