Problem:
Problem A:
Infnity Maze
Dr. Fukuoka has placed a simple robot in a two-dimensional maze. It moves within the maze and never goes out of the maze as there is no exit.
The maze is made up of
H
×
W
grid cells as depicted below. The upper side of the maze faces north. Consequently, the right, lower and left sides face east, south and west respectively. Each cell is either empty or wall and has the coordinates of (
i
,
j
) where the north-west corner has (1, 1). The row
i
goes up toward the south and the column
j
toward the east.
The robot moves on empty cells and faces north, east, south or west. It goes forward when there is an empty cell in front, and rotates 90 degrees to the right when it comes in front of a wall cell or on the edge of the maze. It cannot enter the wall cells. It stops right after moving forward by
L
cells.
Your mission is, given the initial position and direction of the robot and the number of steps, to write a program to calculate the final position and direction of the robot.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
H W L
c
1,1
c
1,2
...
c
1,
W
.
.
.
c
H
,1
c
H
,2
...
c
H
,
W
The first line of a dataset contains three integers
H
,
W
and
L
(1 ≤
H
,
W
≤ 100, 1 ≤
L
≤ 10
18
).
Each of the following
H
lines contains exactly
W
characters. In the
i
-th line, the
j
-th character
c
i,j
represents a cell at (
i
,
j
) of the maze. "
.
" denotes an empty cell. "
#
" denotes a wall cell. "
N
", "
E
", "
S
", "
W
" denote a robot on an empty cell facing north, east, south and west respectively; it indicates the initial position and direction of the robot.
You can assume that there is at least one empty cell adjacent to the initial position of the robot.
The end of input is indicated by a line with three zeros. This line is not part of any dataset.
Output
For each dataset, output in a line the final row, column and direction of the robot, separated by a single space. The direction should be one of the following: "
N
" (north), "
E
" (east), "
S
" (south) and "
W
" (west).
No extra spaces or characters are allowed.
Sample Input
3 3 10
E..
.#.
...
5 5 19
####.
.....
.#S#.
...#.
#.##.
5 5 6
#.#..
#....
##.#.
#..S.
#....
5 4 35
..##
....
.##.
.#S.
...#
0 0 0
Output for the Sample Input
1 3 E
4 5 S
4 4 E
1 1 N

Submitted solutions:

code_snippet: // competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/2320
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>
#include <cassert>
#include <utility>
/// @brief ダブリング
template <int L = 20, class M = void>
struct doubling {
  private:
    using T = typename M::value_type;
  public:
    template <class U>
    doubling(const std::vector<int> &to, const std::vector<U> &v) : doubling(to.size()) {
        build(to, v);
    }
    std::pair<int, T> jump(int f, std::uint64_t k) { return solve(f, k); }
    std::pair<int, T> solve(int f, std::uint64_t k) {
        assert(-1 <= f && f < _size);
        T res = M::id();
        for (int cnt = 0; k > 0; k >>= 1, ++cnt) {
            if ((k & 1) && f != -1) {
                res = M::op(res, data[cnt][f]);
                f = table[cnt][f];
            }
        }
        return std::make_pair(f, res);
    }
  private:
    int _size;
    std::vector<std::vector<int>> table;
    std::vector<std::vector<T>> data;
    doubling(int n) : _size(n), table(L, std::vector<int>(n)), data(L, std::vector<T>(n)) {}
    template <class U>
    void build(const std::vector<int> &to, const std::vector<U> &v) {
        assert(to.size() == v.size());
        for (int i = 0; i < _size; ++i) {
            assert(-1 <= to[i] && to[i] < _size);
            table[0][i] = to[i];
            data[0][i] = v[i];
        }
        for (int i = 0; i < L - 1; ++i) {
            for (int j = 0; j < _size; ++j) {
                int k = table[i][j];
                if (k != -1) {
                    table[i + 1][j] = table[i][k];
                    data[i + 1][j] = M::op(data[i][j], data[i][k]);
                } else {
                    table[i + 1][j] = table[i][j];
                    data[i + 1][j] = data[i][j];
                }
            }
        }
    }
};
/// @brief ダブリング
template <int L>
struct doubling<L, void> {
    doubling(const std::vector<int> &v) : doubling(v.size()) { build(v); }
    int jump(int f, std::uint64_t k) { return solve(f, k); }
    int solve(int f, std::uint64_t k) {
        assert(-1 <= f && f < _size);
        for (int cnt = 0; k > 0; k >>= 1, ++cnt) {
            if ((k & 1) && f != -1) f = table[cnt][f];
        }
        return f;
    }
  private:
    int _size;
    std::vector<std::vector<int>> table;
    doubling(int n) : _size(n), table(L, std::vector<int>(n)) {}
    void build(const std::vector<int> &v) {
        for (int i = 0; i < _size; ++i) {
            assert(-1 <= v[i] && v[i] < _size);
            table[0][i] = v[i];
        }
        for (int i = 0; i < L - 1; ++i) {
            for (int j = 0; j < _size; ++j) {
                if (table[i][j] != -1) table[i + 1][j] = table[i][table[i][j]];
            }
        }
    }
};
namespace internal {
template <int Idx>
struct grid_impl {
    template <class Head, class... Tail>
    constexpr grid_impl(Head &&head, Tail &&...tail)
        : limit(head), impl(std::forward<Tail>(tail)...) {}
    template <class Head, class... Tail>
    constexpr bool in_field(Head x, Tail &&...tail) const {
        return 0 <= x && x < limit && impl.in_field(std::forward<Tail>(tail)...);
    }
    template <class Head, class... Tail>
    constexpr std::int64_t flatten(Head x, Tail &&...tail) const {
        return x + limit * impl.flatten(std::forward<Tail>(tail)...);
    }
  private:
    std::int64_t limit;
    grid_impl<Idx - 1> impl;
};
template <>
struct grid_impl<0> {
    constexpr grid_impl() {}
    constexpr bool in_field() const { return true; }
    constexpr std::int64_t flatten() const { return 0; }
};
}  // namespace internal
template <int Idx>
struct Grid {
    template <class... Args, std::enable_if_t<(sizeof...(Args) == Idx)> * = nullptr>
    constexpr Grid(Args &&...args) : entity(std::forward<Args>(args)...) {}
    template <class... Args, std::enable_if_t<(sizeof...(Args) == Idx)> * = nullptr>
    constexpr bool in_field(Args &&...args) const {
        return entity.in_field(std::forward<Args>(args)...);
    }
    template <class... Args, std::enable_if_t<(sizeof...(Args) == Idx)> * = nullptr>
    constexpr std::int64_t flatten(Args &&...args) const {
        return entity.flatten(std::forward<Args>(args)...);
    }
  private:
    internal::grid_impl<Idx> entity;
};
int main(void) {
    while (true) {
        int h, w;
        std::int64_t l;
        std::cin >> h >> w >> l;
        if (!h && !w && !l)
            break;
        std::vector<std::string> s(h);
        for (auto &e : s) std::cin >> e;
        Grid<2> grid(h, w);
        auto in_grid = [&](int x, int y) {
            return grid.in_field(x, y) && s[x][y] != '#';
        };
        auto flatten = [h, w](int x, int y, int d) {
            return (x * w + y) * 4 + d;
        };
        std::vector<int> to(h * w * 4);
        std::iota(to.begin(), to.end(), 0);
        std::vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (!in_grid(i, j))
                    continue;
                for (int k = 0; k < 4; ++k) {
                    for (int d = 0; d < 4; ++d) {
                        int nx = i + dx[(k + d) % 4];
                        int ny = j + dy[(k + d) % 4];
                        if (in_grid(nx, ny)) {
                            to[flatten(i, j, k)] = flatten(nx, ny, (k + d) % 4);
                            break;
                        }
                    }
                }
            }
        }
        std::string dir = "NESW";
        doubling<64> db(to);
        int x = 0, y = 0, d = 0;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                for (int k = 0; k < 4; ++k) {
                    if (s[i][j] == dir[k])
                        x = i, y = j, d = k;
                }
            }
        }
        int ans = db.solve(flatten(x, y, d), l);
        x = ans / w / 4;
        ans %= w * 4;
        y = ans / 4;
        d = ans % 4;
        std::cout << x + 1 << ' ' << y + 1 << ' ' << dir[d] << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 14048
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
int main(void) {
    constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    while(true) {
        ll h, w, k;
        cin >> h >> w >> k;
        if(h == 0 and w == 0 and k == 0) break;
        auto idx = [&](ll x, ll y, ll dir) -> ll {
            return (x * w + y) * 4 + dir;
        };
        vector<string> c(h);
        ll cur = -1;
        rep(i, 0, h) {
            cin >> c[i];
            rep(j, 0, w) {
                if(c[i][j] != '.' and c[i][j] != '#') {
                    ll dir = -1;
                    if(c[i][j] == 'E') dir = 0;
                    if(c[i][j] == 'S') dir = 1;
                    if(c[i][j] == 'W') dir = 2;
                    if(c[i][j] == 'N') dir = 3;
                    cur = idx(i, j, dir);
                    c[i][j] = '.';
                }
            }
        }
        vector<vector<ll>> doubling(4 * h * w, vector<ll>(60));
        rep(i, 0, 4 * h * w) {
            doubling[i][0] = i;
            ll x = i / 4 / w, y = i / 4 % w, dir = i % 4;
            if(c[x][y] == '#') continue;
            rep(j, 0, 4) {
                ll nx = x + dx[dir], ny = y + dy[dir];
                if(nx < 0 or nx >= h or ny < 0 or ny >= w or c[nx][ny] == '#') {
                    dir = (dir + 1) % 4;
                } else {
                    doubling[i][0] = idx(nx, ny, dir);
                    break;
                }
            }
        }
        rep(i, 0, 59) {
            rep(j, 0, 4 * h * w) {
                doubling[j][i + 1] = doubling[doubling[j][i]][i];
            }
        }
        rep(i, 0, 60) {
            if(k & (1ll << i)) {
                cur = doubling[cur][i];
            }
        }
        ll x = cur / 4 / w, y = cur / 4 % w, dir = cur % 4;
        cout << x + 1 << ' ' << y + 1 << ' ' << "ESWN"[dir] << '\n';
    }
}
accuracy: 1
time_ms: 300
memory_kb: 23368
final_rank: 12

code_snippet: #line 1 "a.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2320"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>

#line 1 "/home/kuhaku/home/github/algo/lib/algorithm/doubling.hpp"
#include <cassert>
#line 3 "/home/kuhaku/home/github/algo/lib/algorithm/doubling.hpp"
#include <utility>
#line 5 "/home/kuhaku/home/github/algo/lib/algorithm/doubling.hpp"

/**
 * @brief ダブリング
 *
 * @tparam L
 * @tparam Monoid モノイド
 */
template <int L, class Monoid = void>
struct Doubling {
  private:
    using T = typename Monoid::value_type;

  public:
    template <class U>
    Doubling(const std::vector<int> &to, const std::vector<U> &v) : Doubling(to.size()) {
        build(to, v);
    }
    std::pair<int, T> jump(int f, std::uint64_t k) { return solve(f, k); }
    std::pair<int, T> solve(int f, std::uint64_t k) {
        assert(-1 <= f && f < _size);
        T res = Monoid::id;
        for (int cnt = 0; k > 0; k >>= 1, ++cnt) {
            if ((k & 1) && f != -1) {
                res = Monoid::op(res, data[cnt][f]);
                f = table[cnt][f];
            }
        }
        return std::make_pair(f, res);
    }

  private:
    int _size;
    std::vector<std::vector<int>> table;
    std::vector<std::vector<T>> data;

    Doubling(int n) : _size(n), table(L, std::vector<int>(n)), data(L, std::vector<T>(n)) {}

    template <class U>
    void build(const std::vector<int> &to, const std::vector<U> &v) {
        assert(to.size() == v.size());
        for (int i = 0; i < _size; ++i) {
            assert(-1 <= to[i] && to[i] < _size);
            table[0][i] = to[i];
            data[0][i] = v[i];
        }

        for (int i = 0; i < L - 1; ++i) {
            for (int j = 0; j < _size; ++j) {
                int k = table[i][j];
                if (k != -1) {
                    table[i + 1][j] = table[i][k];
                    data[i + 1][j] = Monoid::op(data[i][j], data[i][k]);
                } else {
                    table[i + 1][j] = table[i][j];
                    data[i + 1][j] = data[i][j];
                }
            }
        }
    }
};

/**
 * @brief ダブリング
 *
 * @tparam L
 */
template <int L>
struct Doubling<L, void> {
    Doubling(const std::vector<int> &v) : Doubling(v.size()) { build(v); }

    int jump(int f, std::uint64_t k) { return solve(f, k); }
    int solve(int f, std::uint64_t k) {
        assert(-1 <= f && f < _size);
        for (int cnt = 0; k > 0; k >>= 1, ++cnt) {
            if ((k & 1) && f != -1) f = table[cnt][f];
        }
        return f;
    }

  private:
    int _size;
    std::vector<std::vector<int>> table;

    Doubling(int n) : _size(n), table(L, std::vector<int>(n)) {}

    void build(const std::vector<int> &v) {
        for (int i = 0; i < _size; ++i) {
            assert(-1 <= v[i] && v[i] < _size);
            table[0][i] = v[i];
        }

        for (int i = 0; i < L - 1; ++i) {
            for (int j = 0; j < _size; ++j) {
                if (table[i][j] != -1) table[i + 1][j] = table[i][table[i][j]];
            }
        }
    }
};
#line 3 "/home/kuhaku/home/github/algo/lib/algorithm/in_field.hpp"

namespace internal {

template <int Idx>
struct in_field_impl {
    template <class Head, class... Tail>
    constexpr in_field_impl(Head &&head, Tail &&...tail)
        : limit(head), impl(std::forward<Tail>(tail)...) {}

    template <class Head, class... Tail>
    constexpr bool is_internal(Head x, Tail &&...tail) const {
        return 0 <= x && x < limit && impl.is_internal(std::forward<Tail>(tail)...);
    }

  private:
    std::int64_t limit;
    in_field_impl<Idx - 1> impl;
};

template <>
struct in_field_impl<0> {
    constexpr in_field_impl() {}

    constexpr bool is_internal() const {
        return true;
    }
};

}  // namespace internal

template <int Idx>
struct InField {
    template <class... Args, std::enable_if_t<(sizeof...(Args) == Idx)> * = nullptr>
    constexpr InField(Args &&...args) : entity(std::forward<Args>(args)...) {}

    template <class... Args>
    constexpr bool operator()(Args &&...args) const {
        return entity.is_internal(std::forward<Args>(args)...);
    }

  private:
    internal::in_field_impl<Idx> entity;
};
#line 12 "a.cpp"

int main(void) {
    while (true) {
        int h, w;
        std::int64_t l;
        std::cin >> h >> w >> l;
        if (!h && !w && !l)
            break;
        std::vector<std::string> s(h);
        for (auto &e : s) std::cin >> e;

        InField<2> in_field(h, w);
        auto in_grid = [&](int x, int y) {
            return in_field(x, y) && s[x][y] != '#';
        };

        auto flatten = [h, w](int x, int y, int d) {
            return (x * w + y) * 4 + d;
        };

        std::vector<int> to(h * w * 4);
        std::iota(to.begin(), to.end(), 0);
        std::vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (!in_grid(i, j))
                    continue;
                for (int k = 0; k < 4; ++k) {
                    for (int d = 0; d < 4; ++d) {
                        int nx = i + dx[(k + d) % 4];
                        int ny = j + dy[(k + d) % 4];
                        if (in_grid(nx, ny)) {
                            to[flatten(i, j, k)] = flatten(nx, ny, (k + d) % 4);
                            break;
                        }
                    }
                }
            }
        }

        std::string dir = "NESW";
        Doubling<64> db(to);
        int x, y, d;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                for (int k = 0; k < 4; ++k) {
                    if (s[i][j] == dir[k])
                        x = i, y = j, d = k;
                }
            }
        }

        int ans = db.solve(flatten(x, y, d), l);
        x = ans / w / 4;
        ans %= w * 4;
        y = ans / 4;
        d = ans % 4;
        std::cout << x + 1 << ' ' << y + 1 << ' ' << dir[d] << std::endl;
    }

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 13852
final_rank: 8

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 2 "/home/kuhaku/atcoder/github/algo/lib/algorithm/doubling.hpp"

/**
 * @brief ダブリング
 *
 * @tparam L
 */
template <int L>
struct Doubling {
    Doubling(int n) : _size(n), data(L, std::vector<int>(n)) {}
    Doubling(const std::vector<int> &v) : _size(v.size()), data(L, std::vector<int>(v.size())) {
        this->build(v);
    }

    void build(const std::vector<int> &v) {
        for (int i = 0; i < this->_size; ++i) {
            assert(0 <= v[i] && v[i] < this->_size);
            this->data[0][i] = v[i];
        }

        for (int i = 0; i < L - 1; ++i) {
            for (int j = 0; j < this->_size; ++j) {
                this->data[i + 1][j] = this->data[i][this->data[i][j]];
            }
        }
    }

    int solve(int f, std::int64_t k) {
        for (int cnt = 0; k > 0; k >>= 1, ++cnt) {
            if (k & 1) f = this->data[cnt][f];
        }
        return f;
    }

  private:
    int _size;
    std::vector<std::vector<int>> data;
};
#line 3 "/home/kuhaku/atcoder/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/atcoder/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/atcoder/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 4 "a.cpp"

int main(void) {
    while (true) {
        int h, w;
        ll l;
        cin >> h >> w >> l;
        if (!h && !w && !l)
            break;
        vector<string> s(h);
        cin >> s;

        auto in_field = [h, w, s](int x, int y) {
            return 0 <= x && x < h && 0 <= y && y < w && s[x][y] != '#';
        };

        auto to_line = [h, w](int x, int y, int d) {
            return (x * w + y) * 4 + d;
        };

        vector<int> to(h * w * 4);
        iota(all(to), 0);
        vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
        rep (i, h) {
            rep (j, w) {
                if (!in_field(i, j))
                    continue;
                rep (k, 4) {
                    rep (d, 4) {
                        int nx = i + dx[(k + d) % 4];
                        int ny = j + dy[(k + d) % 4];
                        if (in_field(nx, ny)) {
                            to[to_line(i, j, k)] = to_line(nx, ny, (k + d) % 4);
                            break;
                        }
                    }
                }
            }
        }

        string dir = "NESW";
        Doubling<64> db(to);
        int x, y, d;
        rep (i, h) {
            rep (j, w) {
                rep (k, 4) {
                    if (s[i][j] == dir[k])
                        x = i, y = j, d = k;
                }
            }
        }

        int ans = db.solve(to_line(x, y, d), l);
        x = ans / w / 4;
        ans %= w * 4;
        y = ans / 4;
        ans %= 4;
        d = ans;
        co(x + 1, y + 1, dir[d]);
    }

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 13708
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
// const int mod=998244353;
//int dx[8]={1,0,-1,0,-1,-1,1,1};
//int dy[8]={0,1,0,-1,-1,1,-1,1};
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
char d[4]={'N','E','S','W'};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h,w,l;
    while(cin >> h >> w >> l){
        if(h==0&&w==0&&l==0) break;
        int lgl=1;
        vector<vector<char>> c(h,vector<char>(w));
        REP(i,h) REP(j,w) cin >> c[i][j];
        while((1LL<<lgl)<l) lgl++;
        vector db(lgl+1,vector(h,vector(w,vector<tuple<int,int,int>>(4,{-1,-1,-1}))));
        REP(i,h) REP(j,w) REP(k,4){
            bool f=false;
            REP(l,4){
                int x=i+dx[(k+l)%4],y=j+dy[(k+l)%4];
                if(x>=0&&x<h&&y>=0&&y<w){
                    if(c[x][y]!='#'){
                        db[0][i][j][k]={x,y,(k+l)%4};
                        break;
                    }
                }
            }
        }
        REP(t,lgl){
            REP(i,h) REP(j,w) REP(k,4){
                auto [u,v,x]=db[t][i][j][k];
                if(u==-1) continue;
                db[t+1][i][j][k]=db[t][u][v][x];
            }
        }
        int nx,ny,nd;
        REP(i,h) REP(j,w){
            if(c[i][j]!='.'&&c[i][j]!='#'){
                REP(k,4) if(c[i][j]==d[k]){
                    nx=i,ny=j,nd=k;
                }
            }
        }/*
        REP(i,h) REP(j,w){
            auto [u,v,x]=db[0][i][j][1];
            cerr << x << " \n"[j==w-1];
        }*/
        int cnt=0;
        while(l){
            if(l&1){
                auto [u,v,x]=db[cnt][nx][ny][nd];
                nx=u,ny=v,nd=x;
            }
            // cerr << nx << " " << ny << " " << nd << endl;
            cnt++;
            l>>=1;
        }
        cout << nx+1 << " " << ny+1 << " " << d[nd] << endl;
    }
}
accuracy: 1
time_ms: 390
memory_kb: 56936
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int LMAX = 62;
const int MAX = 1e4+10;
using P = pair<int,int>;
P dp[MAX][4][LMAX];

void init(){
  for(int i=0; i<MAX; i++){
    for(int j=0; j<4; j++){
      for(int k=0; k<LMAX; k++){
        dp[i][j][k] = P(-1,-1);
      }
    }
  }
}

void solve(int H, int W){
  int64_t L;
  cin >> L;
  vector<string> vec(H);
  for(int i=0; i<H; i++){
    cin >> vec[i];
  }
  
  vector<int> dx = {-1,0,1,0};
  vector<int> dy = {0,1,0,-1};
  //init();
  
  for(int i=0; i<H*W; i++){
    int x = i/W;
    int y = i%W;
    for(int j=0; j<4; j++){
      if(vec[x][y] != '#'){
        dp[i][j][0] = P(i,j);
        int k = j;
        bool space = false;
        while(true){
          if(0 <= x+dx[k] && x+dx[k] < H && 0 <= y+dy[k] && y+dy[k] < W &&
             vec[x+dx[k]][y+dy[k]] != '#'){
            space = true;
            break;
          }
          k = (k+1)%4;
          if(k == j){
            break;
          }
        }
        if(space){
          dp[i][j][1] = P((x+dx[k])*W + y+dy[k], k);
        }
        else{
          dp[i][j][1] = dp[i][j][0];
        }
      }
    }
  }
  for(int k=2; k<LMAX; k++){
    for(int i=0; i<H*W; i++){
      for(int j=0; j<4; j++){
        auto[x,y] = dp[i][j][k-1];
        dp[i][j][k] = dp[x][y][k-1];
      }
    }
  }
  
  P ans = {-1,-1};
  for(int i=0; i<H*W; i++){
    if(vec[i/W][i%W] != '.' && vec[i/W][i%W] != '#'){
      ans.first = i;
      if(vec[i/W][i%W] == 'N'){
        ans.second = 0;
      }
      if(vec[i/W][i%W] == 'E'){
        ans.second = 1;
      }
      if(vec[i/W][i%W] == 'S'){
        ans.second = 2;
      }
      if(vec[i/W][i%W] == 'W'){
        ans.second = 3;
      }
    }
  }
  
  for(int i=1; i<LMAX; i++){
    if(L & (1LL<<(i-1))){
      auto[x,y] = ans;
      ans = dp[x][y][i];
    }
  }
  vector<char> dir = {'N','E','S','W'};
  cout << ans.first/W + 1 << " " << ans.first%W + 1 << " " << dir[ans.second] << '\n';
}

int main(){
  while(true){
    int H, W;
    cin >> H >> W;
    if(H == 0){
      break;
    }
    solve(H,W);
    //break;
  }
}
accuracy: 1
time_ms: 140
memory_kb: 22512
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;
using T = tuple<int, int, int>;

int h, w;
long long l;
int dir[4] = {0, -1, 0, 1};
string ds = "WNES";
vector<string> s;

void solve();
bool isvalid(int x, int y) {
  return x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.';
}

int main() {
  while (1) {
    cin >> h >> w >> l;
    if (!(h + w + l)) break;
    s.resize(h);
    for (auto &p : s) cin >> p;
    solve();
  }
  return 0;
}

void solve() {
  int x, y, d;
  vector<vector<vector<vector<T>>>> dp(61, vector(h, vector(w, vector<T>(4))));
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      for (int k = 0; k < 4; ++k)
        if (s[i][j] == ds[k]) s[i][j] = '.', x = i, y = j, d = k;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      for (int k = 0; k < 4; ++k) {
        int nd = k;
        dp[0][i][j][k] = T(i, j, k);
        for (int di = 0; di < 4; ++di) {
          int tx = i + dir[nd], ty = j + dir[1 ^ nd];
          if (isvalid(tx, ty)) {
            dp[0][i][j][k] = T(tx, ty, nd);
            break;
          }
          if (++nd >= 4) nd = 0;
        }
      }
  for (int b = 1; b < 61; ++b)
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        for (int k = 0; k < 4; ++k) {
          auto [tx, ty, td] = dp[b - 1][i][j][k];
          dp[b][i][j][k] = dp[b - 1][tx][ty][td];
        }
  for (int i = 0; i < 61; ++i)
    if (l >> i & 1) {
      auto [tx, ty, td] = dp[i][x][y][d];
      x = tx, y = ty, d = td;
    }
  cout << x + 1 << " " << y + 1 << " " << ds[d] << endl;
}
accuracy: 1
time_ms: 550
memory_kb: 57032
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

template <typename F>
class
#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
  [[nodiscard]]
#endif  // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
FixPoint final : private F
{
public:
  template <typename G>
  explicit constexpr FixPoint(G&& g) noexcept
    : F{std::forward<G>(g)}
  {}

  template <typename... Args>
  constexpr decltype(auto)
  operator()(Args&&... args) const
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9
  noexcept(noexcept(F::operator()(std::declval<FixPoint>(), std::declval<Args>()...)))
#endif  // !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 9
  {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};  // class FixPoint


#if defined(__cpp_deduction_guides)
template <typename F>
FixPoint(F&&)
  -> FixPoint<std::decay_t<F>>;
#endif  // defined(__cpp_deduction_guides)


namespace
{
  template <typename F>
#if !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)
  #  if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)
__attribute__((warn_unused_result))
#  elif defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_Check_return_)
_Check_return_
#  endif  // defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#endif  // !defined(__has_cpp_attribute) || !__has_cpp_attribute(nodiscard)
  inline constexpr decltype(auto)
  makeFixPoint(F&& f) noexcept
  {
    return FixPoint<std::decay_t<F>>{std::forward<std::decay_t<F>>(f)};
  }
}  // namespace

vector<int64> dijkstra(const vector<vector<int64>>& G, int s) {
  priority_queue<PLL, vector<PLL>, greater<>> pq;
  vector<int64> d(G.size(), INF_LL);
  d[s] = 0;
  pq.emplace(0, s);

  while (pq.size()) {
    int64 v, c;
    tie(c, v) = pq.top(); pq.pop();
    if (d[v] < c) continue;
    for (auto& u : G[v]) {
      if (d[u] > c + 1) {
        d[u] = c + 1;
        pq.emplace(c+1, u);
      }
    }
  }
  return d;
}

double comb(int a, int b) {
  double res = 1;
  REP(i, b) {
    res = res * (a - i) / (b - i);
  }
  return res;
}

int main(void) {
  int64 H, W, L;
  int64 dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};
  string dir("NESW");
  while (cin >> H >> W >> L && H + W + L) {
    vector<string> c(H+2, string(W+2, '#'));
    int64 y, x, d;
    REP(i, H) {
      cin >> c[i+1];
      c[i+1] = "#" + c[i+1] + "#";
      REP(j, W) {
        if (dir.find(c[i+1][j+1]) != string::npos) {
          y = i+1; x = j+1;
          d = dir.find(c[i+1][j+1]);
        }
      }
    }
    int64 move_count = 0;
    auto memo = make_v<int64>(H+2, W+2, 4);
    fill_v<int64>(memo, -1);
    memo[y][x][d] = 0;
    while (L) {
//      cout << move_count << " " << y << " " << x << " " << dir[d] << endl;
      int64 yy = y + dy[d], xx = x + dx[d];
      if (memo[y][x][d] != move_count && memo[y][x][d] != -1) {
        assert(move_count - memo[y][x][d] > 0);

        L %= move_count - memo[y][x][d];
        if (L == 0) L += move_count - memo[y][x][d];
      }
      memo[y][x][d] = move_count;
      if (c[yy][xx] != '#') {
        move_count++;
        L--;
        y = yy; x = xx;
      } else {
         d = (d + 1) % 4;
      }
    }
    cout << y << " " << x << " " << dir[d] << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 4260
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main(){
    while(true){
        int H, W; ll L; cin >> H >> W >> L;
        if(H == 0) break;

        vector<string> S(H);
        rep(i, H) cin >> S[i];

        vector<vector<int>> p(61, vector<int>(4*H*W));
        vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};

        rep(i, H) rep(j, W) rep(k, 4){
            int now = 4*(W*i+j)+k;
            if(S[i][j] == '#') {p[0][now] = now; continue;}
            rep(l, 4){
                int m = (k+l)%4;
                int ni = i+dx[m], nj = j+dy[m], nk = m;
                if(ni < 0 || ni >= H || nj < 0 || nj >= W || S[ni][nj] == '#') continue;
                int nxt = 4*(W*ni+nj)+nk;
                p[0][now] = nxt;
                break;
            }
        }

        rep2(i, 1, 60){
            rep(j, H*W*4){
                p[i][j] = p[i-1][p[i-1][j]];
            }
        }

        int si, sj, sk;
        rep(i, H) rep(j, W){
            if(S[i][j] == '.' || S[i][j] == '#') continue;
            si = i, sj = j;
            if(S[i][j] == 'E') sk = 0;
            if(S[i][j] == 'S') sk = 1;
            if(S[i][j] == 'W') sk = 2;
            if(S[i][j] == 'N') sk = 3;
        }

        int s = 4*(W*si+sj)+sk;
        rep(j, 61){
            if((L>>j)&1) s = p[j][s];
        }

        int ti, tj, tk;
        tk = s%4, s /= 4;
        tj = s%W, s /= W;
        ti = s;

        string T = "ESWN";
        cout << ti+1 << ' ' << tj+1 << ' ' << T[tk] << '\n';
    }
}
accuracy: 1
time_ms: 80
memory_kb: 13108
final_rank: 4

code_snippet: #include <iostream>
using namespace std;
typedef long long ll;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const char r[4] = {'E', 'S', 'W', 'N'};

int n[40005][100];

void init_doubling(int m){
    for(int k = 1; k < 60; k++){
        for(int i = 0; i < m; i++){
            n[i][k] = n[n[i][k - 1]][k - 1];
        }
    }
}

int doubling(int u, ll l){
    for(int k = 0; k < 60; k++){
        if((l >> k) & 1) u = n[u][k];
    }
    return u;
}

int main()
{
    while(true){
        int h, w;
        ll l;
        cin >> h >> w >> l;
        if(h == 0 && w == 0 && l == 0) break;
        string c[102];
        for(int i = 0; i < h; i++) cin >> c[i];
        for(int x = 0; x < h; x++){
            for(int y = 0; y < w; y++){
                for(int i = 0; i < 4; i++){
                    int t = (x * w + y) * 4 + i;
                    n[t][0] = t;
                    if(c[x][y] == '#') continue;
                    for(int j = i; j < i + 4; j++){
                        int u = x + dx[j % 4], v = y + dy[j % 4];
                        if(u >= 0 && u < h && v >= 0 && v < w && c[u][v] != '#'){
                            n[t][0] = (u * w + v) * 4 + j % 4;
                            break;
                        }
                    }
                }
            }
        }
        init_doubling(h * w * 4);
        int u = -1;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(c[i][j] != '#' && c[i][j] != '.'){
                    int d = -1;
                    if(c[i][j] == 'E') d = 0;
                    if(c[i][j] == 'S') d = 1;
                    if(c[i][j] == 'W') d = 2;
                    if(c[i][j] == 'N') d = 3;
                    u = doubling((i * w + j) * 4 + d, l);
                }
            }
        }
        cout << u / (w * 4) + 1 << " " << u / 4 % w + 1 << " " << r[u % 4] << endl;
    }
}
accuracy: 1
time_ms: 120
memory_kb: 18692
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
const int MAX_H=510,MAX_LOG=60;
tuple<int,int,int> D[MAX_LOG][MAX_H][MAX_H][4];

void solve(int H,int W,ll L){
    vector<string> S(H);
    for (int i=0;i<H;++i) cin >> S[i];

    for (int i=0;i<H;++i){
        for (int j=0;j<W;++j){
            if (S[i][j]=='#') continue;
            for (int k=0;k<4;++k){
                for (int l=0;l<4;++l){
                    int nx=i+dx[(k+l)%4],ny=j+dy[(k+l)%4];
                    if (nx<0||H<=nx||ny<0||W<=ny||S[nx][ny]=='#') continue;
                    D[0][i][j][k]=make_tuple(nx,ny,(k+l)%4);
                    break;
                }
            }
        }
    }

    for (int l=0;l<MAX_LOG-1;++l){
        for (int i=0;i<H;++i){
            for (int j=0;j<W;++j){
                if (S[i][j]=='#') continue;
                for (int k=0;k<4;++k){
                    auto t=D[l][i][j][k];
                    int nx,ny,nk;
                    tie(nx,ny,nk)=t;
                    D[l+1][i][j][k]=D[l][nx][ny][nk];
                }
            }
        }
    }

    int x,y,d;
    string T="SWNE";
    auto ctoi=[](char c){
        if (c=='S') return 0;
        if (c=='W') return 1;
        if (c=='N') return 2;
        return 3;
    };

    for (int i=0;i<H;++i){
        for (int j=0;j<W;++j){
            if (S[i][j]!='#'&&S[i][j]!='.'){
                x=i; y=j; d=ctoi(S[i][j]);
            }
        }
    }

    for (int i=MAX_LOG-1;i>=0;--i){
        if (L&1LL<<i){
            auto t=D[i][x][y][d];
            tie(x,y,d)=t;
        }
    }

    cout << x+1 << ' ' << y+1 << ' ' << T[d] << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H,W; ll L;
    while (cin >> H >> W >> L,H){
        solve(H,W,L);
    }
}
accuracy: 1
time_ms: 110
memory_kb: 55068
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;
// const long long MOD=998244353;
#define LOCAL
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(),(x).end()
const int INF=1e9;
const long long IINF=1e18;
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U,typename V>
ostream&operator<<(ostream &os,const tuple<T,U,V> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
    return os;
}
template<typename T,typename U,typename V,typename W>
ostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const unordered_map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const multiset<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const unordered_set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const deque<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

template<class T1,class T2> inline bool chmin(T1 &a,T2 b){
    if (a>b){a=b; return true;} return false;
}
template<class T1,class T2> inline bool chmax(T1 &a,T2 b){
    if (a<b){a=b; return true;} return false;
}
#pragma endregion

const int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
const int MAX_H=510,MAX_LOG=60;
tuple<int,int,int> D[MAX_LOG][MAX_H][MAX_H][4];

void solve(int H,int W,ll L){
    vector<string> S(H);
    for (int i=0;i<H;++i) cin >> S[i];

    for (int i=0;i<H;++i){
        for (int j=0;j<W;++j){
            if (S[i][j]=='#') continue;
            for (int k=0;k<4;++k){
                for (int l=0;l<4;++l){
                    int nx=i+dx[(k+l)%4],ny=j+dy[(k+l)%4];
                    if (nx<0||H<=nx||ny<0||W<=ny||S[nx][ny]=='#') continue;
                    D[0][i][j][k]=make_tuple(nx,ny,(k+l)%4);
                    break;
                }
            }
        }
    }

    for (int l=0;l<MAX_LOG-1;++l){
        for (int i=0;i<H;++i){
            for (int j=0;j<W;++j){
                if (S[i][j]=='#') continue;
                for (int k=0;k<4;++k){
                    auto t=D[l][i][j][k];
                    int nx,ny,nk;
                    tie(nx,ny,nk)=t;
                    D[l+1][i][j][k]=D[l][nx][ny][nk];
                }
            }
        }
    }

    int x,y,d;
    string T="SWNE";
    auto ctoi=[](char c){
        if (c=='S') return 0;
        if (c=='W') return 1;
        if (c=='N') return 2;
        return 3;
    };

    for (int i=0;i<H;++i){
        for (int j=0;j<W;++j){
            if (S[i][j]!='#'&&S[i][j]!='.'){
                x=i; y=j; d=ctoi(S[i][j]);
            }
        }
    }

    for (int i=MAX_LOG-1;i>=0;--i){
        if (L&1LL<<i){
            auto t=D[i][x][y][d];
            tie(x,y,d)=t;
        }
    }

    cout << x+1 << ' ' << y+1 << ' ' << T[d] << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H,W; ll L;
    while (cin >> H >> W >> L,H){
        solve(H,W,L);
    }
}
accuracy: 1
time_ms: 100
memory_kb: 55096
final_rank: 14

code_snippet: // #include <atcoder/all>
// using namespace atcoder;
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
using namespace std;
using ll = long long;
template<typename T>
inline bool chmax(T& a, const T& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T>
inline bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

int solve() {
    int h, w; ll l;
    cin >> h >> w >> l;
    if (h == 0) return 1;
    vector<string> s(h);
    rep(i, h) cin >> s[i];
    int k = 60;
    auto to = make_vec<tuple<int, int, int>>({k, h, w, 4}, {0, 0, 0});
    array<int, 4> di = {-1, 0, 1, 0}, dj = {0, 1, 0, -1};
    rep(i, h) rep(j, w) {
        if (s[i][j] == '#') continue;
        rep(dir, 4) {
            rep(ddir, 4) {
                int ndir = (dir + ddir) % 4;
                int ni = i + di[ndir], nj = j + dj[ndir];
                if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
                if (s[ni][nj] == '#') continue;
                to[0][i][j][dir] = {ni, nj, ndir};
                break;
            }
        }
    }

    for (int t = 0; t < k-1; ++t) {
        rep(i, h) rep(j, w) rep(dir, 4) {
            auto [ni, nj, ndir] = to[t][i][j][dir];
            auto [nni, nnj, nndir] = to[t][ni][nj][ndir];
            to[t+1][i][j][dir] = {nni, nnj, nndir};
        }
    }

    int ni, nj, ndir;
    rep(i, h) rep(j, w) {
        if (isalpha(s[i][j])) {
            ni = i, nj = j;
            if (s[i][j] == 'N') ndir = 0;
            if (s[i][j] == 'E') ndir = 1;
            if (s[i][j] == 'S') ndir = 2;
            if (s[i][j] == 'W') ndir = 3;
        }
    }

    rep(t, k) {
        if (l >> t & 1) {
            auto [nni, nnj, nndir] = to[t][ni][nj][ndir];
            ni = nni, nj = nnj, ndir = nndir;
        }
    }
    string d = "NESW";
    cout << ni + 1 << ' ' << nj + 1 << ' ' << d[ndir] << '\n';

    return 0;
}

int main() {
    while (!solve());
}
accuracy: 1
time_ms: 540
memory_kb: 55864
final_rank: 18

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------



bool solve(){
  ll H,W,L;
  cin >> H >> W >> L;
  if(H == 0) return false;
  vector<string> G(H);
  for(int i=0; i<H; i++) cin >> G[i];
  using tup = tuple<int,int,int>;
  // dir y x
  map<tup,tup> mp;
  tup pos;
  string str = "ESWN";
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      for(int k=0; k<4; k++){
        if(str[k] == G[i][j]){
          pos = tup(k,i,j);
          i = H;
          j = W;
          break;
        }
      }
    }
  }
  tup st = pos;
  set<tup> used;
  bool fin = false;
  while(1){
    if(used.count(st)) break;
    used.emplace(st);
    int d,i,j;
    tie(d,i,j) = st;
    tup nxt;
    for(int k=0; k<4; k++){
      int ny = i + dy[(d+k)%4];
      int nx = j + dx[(d+k)%4];
      if(ny<0 || ny>=H || nx<0 || nx>=W) continue;
      if(G[ny][nx] == '#') continue;
      nxt = tup((d+k)%4,ny,nx);
      break;
    }
    mp[tup(d,i,j)] = nxt;
    st = nxt;
  }
  using ptup = pair<ll,tup>;
  map<ptup,tup> db;
  for(auto m: mp){
    db[ptup(1LL,m.first)] = m.second;
  }
  for(int i=1; i<64; i++){
    for(auto m: mp){
      db[ptup((1LL<<i),m.first)] = db[ptup((1LL<<(i-1)),db[ptup((1LL<<(i-1)),m.first)])];
    }
  }
  for(int i=0; i<64; i++){
    if(L & (1LL<<i)){
      pos = db[ptup((1LL<<i),pos)];
    }
  }
  int y,x,k;
  tie(k,y,x) = pos;
  cout << y+1 << " " << x+1 << " " << str[k] << "\n";
  return true;
}


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while(solve());
  return 0;
}
accuracy: 1
time_ms: 1250
memory_kb: 29876
final_rank: 20

code_snippet: #pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,-1,1};
ll dx[8] = {1,0,-1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-10;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

bool in(ll y, ll x, ll h, ll w){
	return 0 <= y && y < h && 0 <= x && x < w;
}

ll d[2][103][103][4];
char saki[2][103][103][4];

int main(){
	while(1){
		ll n,m,L; cin >> n >> m >> L;
		if(!n) break;
		vs s(n); rep(i,n) cin >> s[i];
		rep(i,2) rep(j,101) rep(k,101) rep(l,4){
			d[i][j][k][l] = inf;
			saki[i][j][k][l] = '@';
		}
		string dir = "ESWN";
		queue<tuple<ll,ll,ll,ll>> q;
		rep(i,n){
			rep(j,m){
				rep(k,4){
					if(s[i][j] == dir[k]){
						q.emplace(i,j,k,0), d[0][i][j][k] = 0; 
						if(saki[0][i][j][k] == '@') saki[0][i][j][k] = dir[k];
					}
				}
			}
		}
		while(!q.empty()){
			ll y,x,di,t;
			tie(y,x,di,t) = q.front(); q.pop();
			int ny,nx,nd;
			nd = di;
			ny = y + dy[nd];
			nx = x + dx[nd];
			bool upd = true;
			while(!in(ny,nx,n,m) || s[ny][nx] == '#'){
				upd = false;
				nd = (nd + 1) % 4;
				ny = y + dy[nd];
				nx = x + dx[nd];
				rep(i,2){
					if(chmin(d[i][y][x][nd], d[t][y][x][di])){
						if(saki[i][y][x][nd] == '@') saki[i][y][x][nd] = dir[di];
						upd = true;
						break;
					}
				}
				if(!upd) break;
			}
			if(!upd) break;
			upd = false;
			rep(i,2){
				if(chmin(d[i][ny][nx][nd], d[t][y][x][di] + 1)){
					q.emplace(ny,nx,nd,i);
					if(saki[i][ny][nx][nd] == '@') saki[i][ny][nx][nd] = dir[nd];
					upd = true;
					break;
				}
			}
			if(!upd) break;
		}
		int Y,X; char c;
		rep(i,n){
			rep(j,m){
				if(s[i][j] == '#') continue;
				rep(k,4){
					if(L < d[0][i][j][k]) continue;
					if(d[0][i][j][k] == L) Y = i, X = j, c = saki[0][i][j][k];
					if(d[1][i][j][k] == L) Y = i, X = j, c = saki[1][i][j][k];
					if(d[1][i][j][k] == inf) continue;
					if((L - d[0][i][j][k]) % (d[1][i][j][k] - d[0][i][j][k]) == 0){
						Y = i, X = j, c = saki[1][i][j][k];
					}
				}
			}
		}
		cout << Y+1 << " " << X+1 << " " << c << "\n";
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3940
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

char fi[110][110];
pair<pair<int,int>,int> d[65][4][110][110];

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h,w;
    map<char,int> mp;
    map<int,char> c;
    mp['N']=3,mp['E']=0,mp['S']=1,mp['W']=2;
    c[0]='E'; c[1]='S'; c[2]='W'; c[3]='N';
    while(cin >> h >> w,h){
        ll l; cin >> l;
        int sx,sy,f;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> fi[i][j];
                if(fi[i][j]!='.' and fi[i][j]!='#'){
                    sx=i,sy=j,f=mp[fi[i][j]];
                    fi[i][j]='.';
                }
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(fi[i][j]=='#')continue;
                for(int k=0;k<4;k++){
                    int nx=i+dx[k],ny=j+dy[k];
                    if(0<=nx and nx<h and 0<=ny and ny<w and fi[nx][ny]=='.'){
                        d[0][k][i][j]=make_pair(make_pair(nx,ny),k);
                    }
                    else{
                        int kk=k;
                        int cnt=0;
                        while(1){
                            cnt++; if(cnt>=6)break;
                            kk++; kk%=4;
                            nx=i+dx[kk],ny=j+dy[kk];
                            if(0<=nx and nx<h and 0<=ny and ny<w and fi[nx][ny]=='.'){
                                d[0][k][i][j]=make_pair(make_pair(nx,ny),kk);
                                break;
                            }
                        }
                        if(cnt>=6){
                            d[0][k][i][j]=make_pair(make_pair(i,j),k);
                        }
                    }
                }
            }
        }
        for(int _=0;_+1<65;_++){
            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++){
                    for(int k=0;k<4;k++){
                        int nx=d[_][k][i][j].first.first,ny=d[_][k][i][j].first.second,nf=d[_][k][i][j].second;
                        d[_+1][k][i][j]=d[_][nf][nx][ny];
                    }
                }
            }
        }
        for(int i=60;i>=0;i--){
            if((1LL<<i)&l){
                auto p=d[i][f][sx][sy];
                sx=p.first.first,sy=p.first.second,f=p.second;
            }
        }
        cout << sx+1 << " " << sy+1 << " " << c[f] << endl;
    }
}
accuracy: 1
time_ms: 140
memory_kb: 37720
final_rank: 13

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

#define EPS 1e-10
//ここから編集

pair<pair<int, int>, int> dp[60][110][110][4];

/* 0: N, 1: E, 2: S, 3: W */
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(6);

  ll H, W, L;
  while(cin >> H >> W >> L){
    if(H == 0 && W == 0 && L == 0) break;

    vector<string> field;
    REP(i,H){
      string s; cin >> s;
      field.push_back(s);
    }
    REP(i,110) REP(j,110) REP(k,60) REP(l,4) dp[k][i][j][l] = make_pair(make_pair(-1, -1), -1);

    int sy, sx, dir;
    REP(i,H){
      REP(j,W){
        if(field[i][j] != '.' && field[i][j] != '#'){
          sy = i;
          sx = j;
          if(field[i][j] == 'N') dir = 0;
          else if(field[i][j] == 'E') dir = 1;
          else if(field[i][j] == 'S') dir = 2;
          else dir = 3;
        }

      }
    }
    REP(i,H){
      REP(j,W){
        //cout << i << " " << j << endl;
        if(i-1 >= 0 && field[i-1][j] != '#'){
          dp[0][i][j][0] = make_pair(make_pair(i-1, j), 0);
        }else{
          if(j+1 < W && field[i][j+1] != '#'){
            dp[0][i][j][0] = make_pair(make_pair(i, j+1), 1);
          }else{
            if(i+1<H && field[i+1][j] != '#'){
              dp[0][i][j][0] = make_pair(make_pair(i+1, j), 2);
            }else{
              dp[0][i][j][0] = make_pair(make_pair(i, j-1), 3);
            }
          }
        }

        if(i+1<H && field[i+1][j] != '#'){
          dp[0][i][j][2] = make_pair(make_pair(i+1, j), 2);
        } else{
          if(j-1>=0 && field[i][j-1] != '#'){
            dp[0][i][j][2] = make_pair(make_pair(i, j-1), 3);
          }else{
            if(i-1>=0 && field[i-1][j] != '#'){
              dp[0][i][j][2] = make_pair(make_pair(i-1, j), 0);
            }else{
              dp[0][i][j][2] = make_pair(make_pair(i, j+1), 1);
            }
          }
        }

        if(j+1<W && field[i][j+1] != '#'){
          dp[0][i][j][1] = make_pair(make_pair(i, j+1), 1);
        }else{
          if(i+1<H && field[i+1][j] != '#'){
            dp[0][i][j][1] = make_pair(make_pair(i+1, j), 2);
          }else{
            if(j-1>=0 && field[i][j-1] != '#'){
              dp[0][i][j][1] = make_pair(make_pair(i, j-1), 3);
            }else{
              dp[0][i][j][1] = make_pair(make_pair(i-1, j), 0);
            }
          }
        }

        if(j-1 >= 0 && field[i][j-1] != '#'){
          dp[0][i][j][3] = make_pair(make_pair(i, j-1), 3);
        }else{
          if(i-1>=0 && field[i-1][j] != '#'){
            dp[0][i][j][3] = make_pair(make_pair(i-1, j), 0);
          }else{
            if(j+1<W && field[i][j+1] != '#'){
              dp[0][i][j][3] = make_pair(make_pair(i, j+1), 1);
            }else{
              dp[0][i][j][3] = make_pair(make_pair(i+1, j), 2);
            }
          }
        }
      }
    }

    int nowy = sy, nowx = sx, ndir = dir;
    for(int k=0; k+1<60; k++){
      for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
          for(int d=0; d<4; d++){
            int py = dp[k][i][j][d].first.first, px = dp[k][i][j][d].first.second, pdir = dp[k][i][j][d].second;
            if(py == -1) continue;
            dp[k+1][i][j][d] = dp[k][py][px][pdir];
          }
          
        }
      }
    }

    for(int k=59; k>=0; k--){
      if(L >> k & 1){
        auto p = dp[k][nowy][nowx][ndir];
        nowy = p.first.first;
        nowx = p.first.second;
        ndir = p.second;
      }
    }
    cout << nowy+1 << " " << nowx+1 << " ";
    if(ndir == 0) cout << 'N';
    else if(ndir == 1) cout << 'E';
    else if(ndir == 2) cout << 'S';
    else cout << "W";
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 720
memory_kb: 37292
final_rank: 16

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000



int main(){
	
	while(true){
		int h,w;
		long long L;
		cin>>h>>w>>L;
		if(h==0)break;
		int maxi = 0;
		rep(i,60){
			if((L>>i)&1)maxi = i+1;
		}
		vector<string> S(h);
		rep(i,h)cin>>S[i];
		vector<vector<int>> nxt(maxi,vector<int>(h*w*4,0));
		
		rep(i,maxi){
			rep(j,h*w*4){
				if(i==0){
					int d = j%4;
					int x = (j/4)%w;
					int y = (j/4)/w;
					
					if(S[y][x]=='#')nxt[i][j] = j;
					else{
						int t = 0;
						while(true){
							t++;
							if(t>5){
								nxt[i][j] = j;
								break;
							}
							int yy = y,xx = x;
							if(d==0)xx++;
							if(d==1)yy--;
							if(d==2)xx--;
							if(d==3)yy++;
							
							if(yy<0||yy>=h||xx<0||xx>=w||S[yy][xx]=='#'){
								d += 3;
								d %= 4;
								continue;
							}
							else{
								nxt[i][j] = 4 * w * yy + 4 * xx + d;
								break;
							}
						}
						
					}
					
				}
				else{
					nxt[i][j] = nxt[i-1][nxt[i-1][j]];
				}				
			}
		}
		int now = -1;
		rep(i,h){
			rep(j,w){
				if(S[i][j]=='#'||S[i][j]=='.')continue;
				now = 4 * w * i + 4 * j;
				if(S[i][j]=='N')now++;
				if(S[i][j]=='W')now+=2;
				if(S[i][j]=='S')now+=3;
				break;
			}
			if(now!=-1)break;
		}

		rep(i,maxi){
			if((L>>i)&1)now = nxt[i][now];
		}
		
		int d = now%4;
		now/=4;
		int x = now%w;
		int y = now/w;
		
		cout<<y+1<<' '<<x+1<<' ';
		char c;
		if(d==0)c='E';
		if(d==1)c='N';
		if(d==2)c='W';
		if(d==3)c='S';
		cout<<c<<endl;
		
	}
	
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 12952
final_rank: 3

code_snippet: #include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>


template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

int dh[] = { -1,0,1,0 };
int dw[] = { 0,1,0,-1 };
void solve() {
	int H, W; cin >> H >> W; if (H == 0) exit(0);
	ll L; cin >> L;
	vector<string> vs(H); for (int i = 0; i < H; i++) cin >> vs[i];
	vector<vector<int>> to(62, vector<int>(H * W * 4));
	for (int i = 0; i < H * W * 4; i++) {
		int d = i / (H * W);
		int h = (i % (H * W) / W);
		int w = i % W;
		int nd = d;
		int nh = h + dh[nd];
		int nw = w + dw[nd];
		if (vs[h][w] == '#') continue;
		int cnt = 0;
		while (nh < 0 or nh >= H or nw < 0 or nw >= W or vs[nh][nw] == '#') {
			cnt++;
			if (cnt >= 5) break;
			nd += 1; if (nd >= 4) nd -= 4;
			nh = h + dh[nd];
			nw = w + dw[nd];
		}
		to[0][i] = nd * H * W + nh * W + nw;
		if (cnt >= 5) to[0][i] = 0;
	}
	for (int i = 1; i < 62; i++) for (int j = 0; j < H * W * 4; j++) to[i][j] = to[i - 1][to[i - 1][j]];
	char c[] = { 'N','E','S','W' };
	for (int i = 0; i < H; i++)for (int j = 0; j < W; j++) for (int k = 0; k < 4; k++) {
		if (vs[i][j] == c[k]) {
			int s = k * H * W + i * W + j;
			for (int bit = 0; bit < 62; bit++) {
				if (L >> bit & 1) {
					s = to[bit][s];
				}
			}
			int d = s / (H * W);
			int h = (s % (H * W)) / W;
			int w = s % W;
			cout << h + 1 << " " << w + 1 << " " << c[d] << "\n";
			return;
		}
	}
}

int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int kkt = 89;
	while (kkt) {
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 13148
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long LOG = 61;
vector<int> dy = {-1, 0, 1, 0};
vector<int> dx = {0, 1, 0, -1};
int main(){
  while (1){
    int H, W;
    long long L;
    cin >> H >> W >> L;
    if (H == 0 && W == 0 && L == 0){
      break;
    }
    vector<vector<char>> c(H + 2, vector<char>(W + 2, '#'));
    for (int i = 1; i <= H; i++){
      for (int j = 1; j <= W; j++){
        cin >> c[i][j];
      }
    }
    int s;
    for (int i = 1; i <= H; i++){
      for (int j = 1; j <= W; j++){
        if (c[i][j] != '.' && c[i][j] != '#'){
          s = ((i - 1) * W + (j - 1)) * 4;
          if (c[i][j] == 'E'){
            s++;
          }
          if (c[i][j] == 'S'){
            s += 2;
          }
          if (c[i][j] == 'W'){
            s += 3;
          }
          c[i][j] = '.';
        }
      }
    }
    vector<int> next(H * W * 4, -1);
    for (int i = 1; i <= H; i++){
      for (int j = 1; j <= W; j++){
        for (int k = 0; k < 4; k++){
          int v = ((i - 1) * W + (j - 1)) * 4 + k;
          if (c[i + dy[k]][j + dx[k]] == '.'){
            next[v] = ((i + dy[k] - 1) * W + (j + dx[k] - 1)) * 4 + k;
          } else {
            for (int l = 0; l < 4; l++){
              if (c[i + dy[(k + l) % 4]][j + dx[(k + l) % 4]] == '.'){
                next[v] = ((i + dy[(k + l) % 4] - 1) * W + (j + dx[(k + l) % 4] - 1)) * 4 + (k + l) % 4;
                break;
              }
            }
          }
        }
      }
    }
    vector<vector<int>> p(LOG, vector<int>(H * W * 4, -1));
    for (int i = 0; i < H * W * 4; i++){
      p[0][i] = next[i];
    }
    for (int i = 1; i < LOG; i++){
      for (int j = 0; j < H * W * 4; j++){
        if (p[i - 1][j] != -1){
          p[i][j] = p[i - 1][p[i - 1][j]];
        }
      }
    }
    for (int i = 0; i < LOG; i++){
      if (L >> i & 1){
        s = p[i][s];
      }
    }
    int y = s / 4 / W + 1;
    int x = s / 4 % W + 1;
    char d;
    if (s % 4 == 0){
      d = 'N';
    }
    if (s % 4 == 1){
      d = 'E';
    }
    if (s % 4 == 2){
      d = 'S';
    }
    if (s % 4 == 3){
      d = 'W';
    }
    cout << y << ' ' << x << ' ' << d << endl;
  }
}
accuracy: 1
time_ms: 100
memory_kb: 13192
final_rank: 9