fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 9130 (C++) Herbert
최초 업로드: 2025-07-07 21:24:12
최근 수정 시간: 2025-07-25 08:45:57
게시자: rlatjwls3333
카테고리: 백준
조회수: 10
# [Silver I] Herbert [문제 링크](https://www.acmicpc.net/problem/9130) ## 문제 설명 <p>Herbert is a game in which you control a robot on an infinite two-dimensional grid. There are three commands available to you:</p> <ul> <li>s: Go one square forward in the current direction. </li> <li>l: Turn ninety degrees counterclockwise.</li> <li>r: Turn ninety degrees clockwise.</li> </ul> <p>After playing this game for a while, you wonder how many squares you can reach within a certain number of moves. Write a program to calculate the answer to this question.</p> ## 입력 <p>On the first line an integer t (1 ≤ t ≤ 100): the number of test cases. Then for each test case:</p> <ul> <li>One line with an integer n (0 ≤ n ≤ 1 000 000 000): the maximum number of moves.</li> </ul> ## 출력 <p>For each test case:</p> <ul> <li>One line with the number of reachable squares.</li> </ul> ## 풀이 #### 규칙을 찾아, 등차수열의 합 공식을 이용하여 풀 수 있다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { long long n; cin >> n; long long sum = n+1; // 앞 if(n>=1) sum += (n-1)*2 // 중앙 위 아래 + (n-2)*(n-1); // 앞쪽 위 아래 if(n>=2) sum += max(n-2, 0LL) // 뒤 + (n-3)*(n-2); // 뒤쪽 위 아래 cout << sum << ' '; } } ```