목차
(1) (i,j) ~ (x,y) 까지 거리를 구한다.
(2) 이 거리가 R안에 들어오는가??
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int X = sc.nextInt();
int Y = sc.nextInt();
int R = sc.nextInt(); //사거리
int arr[][] = new int[105][105];
for(int i = 1 ; i<=N ; i++){
for(int j = 1; j<=N ; j++){
int a = X-i;
int b = Y-j;
if(a<0)a*=-1; // 단순 거리 계산이기 때문에 부호를 변경해야함
if(b<0)b*=-1; // 단순 거리 계산이기 때문에 부호를 변경해야함
int dist = a+b;
if(dist==0)arr[i][j]=-1;
else if(dist<=R)arr[i][j]=dist;
}
}
for(int i = 1 ; i<=N ; i++){
for(int j = 1; j<=N ; j++){
if(arr[i][j]==-1) System.out.print("x ");
else System.out.print(arr[i][j]+" ");
}
System.out.println();
}
// Please Enter Your Code Here
}
}
입력
8
4 5 3
출력
0 0 0 0 3 0 0 0
0 0 0 3 2 3 0 0
0 0 3 2 1 2 3 0
0 3 2 1 x 1 2 3
0 0 3 2 1 2 3 0
0 0 0 3 2 3 0 0
0 0 0 0 3 0 0 0
0 0 0 0 0 0 0 0
8x8 크기의 형태로, (4,5)위치의 플레이어 x값에 사거리가 3인 x
x를 기준으로 가로, 세로 길이 합이 3인 좌표들을 구해야한다.
a+b의 값을 dist에 집어넣고 dist가 0이라면 그 좌표는 플레이어 x의 좌표이다.
arr은 int형이므로 x의 좌표에 "x"를 넣지 못하므로 -1을 넣는다.
이 후에 -1 값을 "x"로 출력한다.
'알고리즘' 카테고리의 다른 글
백준 13549 (숨바꼭질 3) (0) | 2023.08.11 |
---|---|
백준 1260(DFS와 BFS) (0) | 2023.08.07 |
백준 11657(타임머신) (0) | 2023.08.03 |
점수 계산 (ALGORITHM JOBS) (0) | 2023.07.29 |
백준 17298(오큰수) (0) | 2023.07.29 |