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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * @uni_kakurenbo
 * https://github.com/uni-kakurenbo/competitive-programming-workspace
 *
 * CC0 1.0  http://creativecommons.org/publicdomain/zero/1.0/deed.ja
 */
/* #language C++ 20 GCC */

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2320"

#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string_view><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "snippet/aliases.hpp"
#include "snippet/fast_io.hpp"
#include "snippet/iterations.hpp"
#include "adaptor/io.hpp"
#include "adaptor/string.hpp"
#include "structure/grid.hpp"
#include "numeric/repeater.hpp"

constexpr std::string_view DIRS = "NESW";

signed main() {
    while(true) {
        uni::i32 h, w; uni::i64 l;
        input >> h >> w >> l;
        if(h == 0) break;

        uni::grid<char, uni::string> grid(h, w); input >> grid;
        debug(grid);

        uni::i32 sid = 0;
        REP(i, h) REP(j, w) {
            if(const auto d = DIRS.find(grid(i, j)); d != std::string_view::npos) {
                sid = grid.id(i, j) * 4 + d;
                debug(i, j, d);
            }
        }

        auto f = [&](uni::i64 id) -> uni::i64 {
            if(!grid.is_valid(grid.pos(id / 4))) return 0;
            if(grid(grid.pos(id / 4)) == '#') return 0;

            uni::i32 i, j, d = id % 4 - 1;

            uni::i32 cnt = 0;
            do {
                if(++cnt > 4) return 0;

                std::tie(i, j) = grid.pos(id / 4);
                d = uni::mod(d + 1, 4);
                debug(i, j, d);

                i += uni::DIRS4[d].first, j += uni::DIRS4[d].second;
            } while(!grid.is_valid(i, j) || grid(i, j) == '#');

            return grid.id(i, j) * 4 + d;
        };

        uni::repeater<uni::i64, 40000> repeater(f);
        {
            const uni::i64 tid = repeater[l](sid);
            const auto [ i, j ] = grid.pos(tid / 4);
            print(i + 1, j + 1, DIRS[tid % 4]);
        }
    }
}