일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 스프링
- php 프로그래밍 입문 솔루션
- programmers
- php 프로그래밍 입문 연습문제
- 자바
- 한정 분기
- 백준
- php 프로그래밍 입문 3판
- php 프로그래밍 입문 예제
- 페이코 추천인
- 플러터 개발환경 설정
- php
- Java
- 배열
- 페이코 친구코드
- 파이썬
- 자바 스프링
- Flutter
- 페이코 추천인코드
- spring
- 최단 경로
- php 프로그래밍 입문
- JAVA SPRING
- SWEA
- C언어
- php 프로그래밍
- C
- php 프로그래밍 입문 문제풀이
- 페이코 초대코드
- 플러터
Archives
- Today
- Total
04-10 03:36
ImJay
[SWEA/Java] 1954. 달팽이 숫자 본문
반응형
[SWEA/Java] 1954. 달팽이 숫자
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
package edu.ssafy.im.SWEA.D2.No1954;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
StringBuilder sb = new StringBuilder();
int[][] graph;
int n;
int direction[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public static void main(String[] args) throws IOException {
new Solution().sol();
}
private void sol() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
for (int t = 1; t <= testCase; t++) {
sb.append("#").append(t).append("\n");
n = Integer.parseInt(br.readLine());
graph = new int[n][n];
graph[0][0] = 1;
makeArray(0, 0, 0, 1);
}
System.out.println(sb);
}
private void makeArray(int x, int y, int d, int v) {
// basis part
if(v == n*n) { // 모든 칸에 숫자가 채워지면 출력
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
sb.append(graph[r][c]).append(" ");
}
sb.append("\n");
}
return;
}
// inductive part
x += direction[d][0];
y += direction[d][1];
if(checkStatus(x, y)) { // 다음 칸이 비어있으면
graph[x][y] = ++v; // 값을 채우고
makeArray(x, y, d, v); // 계속 진행
} else { // 다음 칸이 이미 채워져 있으면
x -= direction[d][0]; // 현재 위치로 되돌아가고
y -= direction[d][1];
d++; // 방향 전환
d %= 4; // 4방향 중 하나로 변경
makeArray(x, y, d, v); // 계속 진행
}
}
private boolean checkStatus(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < n && graph[x][y] == 0;
}
}
반응형
'SW Expert Academy > D2' 카테고리의 다른 글
[SWEA/Java] 2001. 파리 퇴치 (1) | 2024.01.23 |
---|---|
[SWEA/Java] 1974. 스도쿠 검증 (3) | 2024.01.22 |
[SWEA/Java] 1940. 가랏! RC카! (0) | 2024.01.21 |
[SWEA/Java] 1859. 백만 장자 프로젝트 (0) | 2024.01.20 |
[SW Expert Academy] 1974. 스도쿠 검증 (1) | 2024.01.02 |
Comments