본문 바로가기

USACO/Bronze

백준 11978 Mowing the Field (USACO January 2016 Bronze 3번)

문제 링크: https://www.acmicpc.net/problem/11978

 

11978번: Mowing the Field (Bronze)

The first line of input contains \(N\) (\(1 \leq N \leq 100\)). Each of the remaining \(N\) lines contains a single statement and is of the form 'D S', where D is a character describing a direction (N=north, E=east, S=south, W=west) and S is the number of

www.acmicpc.net

이미 지나갔던 칸을 재방문할때까지의 시간의 최소값을 구함.

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
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
 
int n, field[2001][2001];
int dy[4= {-1010}, dx[4= {010-1};
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int y = 1000, x = 1000, time = 2, ans = 1e9;
    field[y][x] = 1;
 
    cin >> n;
    for(int i = 0; i < n; i++) {
        char dir;
        int steps, d;
        cin >> dir >> steps;
 
        if(dir == 'N') d = 0;
        if(dir == 'E') d = 1;
        if(dir == 'S') d = 2;
        if(dir == 'W') d = 3;
 
        while(steps--) {
            int yy = y + dy[d], xx = x + dx[d];
 
            if(field[yy][xx] != 0)
                ans = min(ans, time - field[yy][xx]);
 
            field[yy][xx] = time;
            y = yy, x = xx;
            time++;
        }
    }
 
    if(ans == 1e9cout << -1 << '\n';
    else cout << ans << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter