java
N x N 크기의 농장이 격자 모양으로 구성되어 있으며, 각 격자 칸에는 농작물의 가치가 표시되어 있다. 이 농장에서는 특별히 중앙에서 시작하여 마름모 모양으로 농작물을 수확한다. 마름모 구역 내의 농작물 가치의 합을 구하는 문제
각 줄에서 중앙으로부터의 거리에 따라 수확 범위를 결정하고, 해당 범위 내의 값들을 더하여 문제를 풀었다.
마름모 형태의 수확 구역을 계산하는 것이 처음에는 복잡해 보였지만, 중앙으로부터의 거리 개념을 사용하니 조금 수월하게 풀 수 있었습니다.
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
int N = sc.nextInt();
int[][] farm = new int[N][N];
int total = 0;
for (int i = 0; i < N; i++) {
String row = sc.next();
for (int j = 0; j < N; j++) {
farm[i][j] = row.charAt(j) - '0';
}
}
int center = N / 2;
for (int i = 0; i < N; i++) {
int distance = Math.abs(center - i);
for (int j = distance; j < N - distance; j++) {
total += farm[i][j];
}
}
System.out.println("#" + test_case + " " + total);
}
sc.close();
}
}
Java인터넷 네트워크란? "인터넷(Internet)"이라는 단어는 "인터네트워크(Internetwork)"의 줄임말입니다. 즉, 여러 개의 네트워크가 상호 연결되어 전 세계적으로 하나의…
스프링 빈(Spring Bean)과 IoC 컨테이너 스프링 프레임워크의 핵심 철학은 IoC(Inversion of Control) 컨테이너를 통해 객체(빈,…
스프링 빈이란? 스프링 프레임워크에서 "스프링 빈(Bean)"이란, IoC(Inversion of Control) 컨테이너가 직접 관리하는 객체를 말합니다. @Component,…
의존성 주입(Dependency Injection)이란? 프로그램을 개발하다 보면, 여러 클래스나 객체들은 서로 필요한 기능을 사용하기 위해 관계를…
컴포넌트 스캔이란? 컴포넌트 스캔(Component Scan)은 스프링 프레임워크가 특정 패키지를 탐색하면서, 스캔 대상에 해당하는 클래스를 찾아…
스프링 빈이란? 스프링 빈(Spring Bean)은 스프링 IoC(Inversion of Control) 컨테이너가 관리하는 자바 객체를 의미합니다. 간단히…
View Comments
Howdy just wanted to give you a quick heads up. The words in your content seem to be running off the
screen in Firefox. I'm not sure if this is a format
issue or something to do with browser compatibility but I thought I'd post to let you know.
The style and design look great though! Hope you get the issue fixed soon. Thanks
Thank you for letting me know. I will look into it.
It’s the best time to make some plans for the long
run and it’s time to be happy. I’ve read this publish and if I may I want to suggest you few attention-grabbing
issues or tips. Perhaps you can write subsequent articles relating to
this article. I want to learn even more things about it!