Problem:
D: 遭難
問題
$H * W$ のグリッドで表される地図が与えられる。
  各マスは '#' か '.' であり、前者は陸地であることを、後者は海であることを示している。
以下のように上下左右に隣接している陸地の集合を島と呼ぶ。
.....
.###.
..##.
..#..
.....
陸地 $6$ マスの島。
......
.##.#.
....#.
......
陸地 $2$ マスの島が $2$ つ。
与えられる地図は、以下の条件を満たす。
地図の端 $1$ マスは必ず '.' である。
同じ形の島が二つ以上存在することはない。
つまり、島に含まれる陸地のうち、最も上にあり、その中で最も左にある陸地を基準とした陸地の座標集合が等しい島は存在しない。
全ての '.' マスは連結である。つまり、以下のような島は存在しない。
.....
.###.
.#.#.
.###.
.....
陸地のどこかにAORイカちゃんがいる。
  あなたの目標は、AORイカちゃんが最初にいたマスを当てることである。
  そのために、あなたは次のクエリを繰り返し送ることができる。
'U', 'D', 'L', 'R' のうち、ひとつを選ぶ。AORイカちゃんの現在位置を $c_{y, x}$ とすると、それぞれ $c_{y - 1, x},\ c_{y + 1, x},\ c_{y, x - 1},\ c_{y, x + 1}$ に移動するように指示することができる。
移動先が '.' である場合、AORイカちゃんは "NO" を返し、移動しない。 そうでない場合、AORイカちゃんは "YES" を返し、指定したマスに移動する。
クエリを高々 $800000$ 回 まで送ることで、AORイカちゃんが最初にいたマスを求めよ。
入出力
まず、地図の大きさを表す非負整数 $H, W$ と、地図を表す $H$ 行 $W$ 列の文字 $c_{i,j}$ が与えられる。
$H\ W$
$c_{0,0}, \dots , c_{0, W - 1}$
$\vdots$
$c_{H - 1, 0}, \dots , c_{H - 1, W - 1}$
続いて、あなたのプログラムは何回か応答プログラムに質問をする．質問のフォーマットは以下のとおりである。
? com
$com$ は U, D, L, R のうちいずれか $1$ 文字である。
  この質問で、指定された方向へ移動可能かを表す文字列が標準入力に $1$ 行で渡される。
何回か質問を行った後、あなたはAORイカちゃんの初期位置を当てる。AORイカちゃんが最初にいたマスを $c_{y,x}$ だとすると、
! y x
というフォーマットで出力する。初期位置を出力した後、あなたのプログラムは直ちに終了しなければならない。終了しなかった場合のジャッジ結果は不定である。
  また、これら以外のフォーマットで出力した場合のジャッジ結果も不定である。
なお、質問や初期位置を応答プログラムに送ったあとフラッシュしないとTLEとなることがある。
フラッシュはCでは
fflush(stdout);
C++では
std::cout << endl;
で行うことができる。
  fflush() は stdlib.h をインクルードすると使用できる。
制約
$3 \leq H, W \leq 500$
地図に含まれる文字は '#' か '.' のみである。
入出力例
プログラムへの入力
プログラムの出力
4 8
........
.#.#.##.
...#.##.
........
? R
NO
? U
NO
? D
YES
? L
NO
! 1 3

Submitted solutions:

code_snippet: // AOJ #2899 Accident
// 2025.5.29

#include <bits/stdc++.h>
using namespace std;
const uint64_t BASE = 1000003;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int H,W;
    cin>>H>>W;
    vector<string> G(H);
    for(int i=0;i<H;i++) cin>>G[i];

    vector<vector<char>> used(H, vector<char>(W,0));
    unordered_map<uint64_t,pair<int,int>> mp;
    mp.reserve(H*W/4);

    int dy[4]={-1,1,0,0}, dx[4]={0,0,-1,1};
    vector<pair<int,int>> cells;
    cells.reserve(H*W);

    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(!used[i][j] && G[i][j]=='#'){
                queue<pair<int,int>> q;
                q.push({i,j});
                used[i][j]=1;
                cells.clear();
                cells.emplace_back(i,j);

                while(!q.empty()){
                    auto [y,x]=q.front(); q.pop();
                    for(int d=0;d<4;d++){
                        int ny=y+dy[d], nx=x+dx[d];
                        if(ny>=0 && ny<H && nx>=0 && nx<W
                           && !used[ny][nx] && G[ny][nx]=='#'){
                            used[ny][nx]=1;
                            q.push({ny,nx});
                            cells.emplace_back(ny,nx);
                        }
                    }
                }

                int my=H, mx=W;
                for(auto &p: cells){
                    my = min(my, p.first);
                    mx = min(mx, p.second);
                }
                vector<pair<int,int>> rel;
                rel.reserve(cells.size());
                for(auto &p: cells) rel.emplace_back(p.first-my, p.second-mx);
                sort(rel.begin(), rel.end());

                uint64_t h = rel.size();
                for(auto &p: rel){
                    h = h * BASE + p.first;
                    h = h * BASE + p.second;
                }
                mp[h] = {my, mx};
            }
        }
    }

    static bool vis[1001][1001] = {};
    const int OFF = 500;
    int dy2[4]={-1,1,0,0}, dx2[4]={0,0,-1,1};
    char qc[4] = {'U','D','L','R'};
    char rc[4] = {'D','U','R','L'};
    vector<pair<int,int>> local;
    local.reserve(H*W);

    struct St{int y,x,dir,entry;};
    vector<St> st;
    st.reserve(H*W);

    vis[OFF][OFF]=true;
    local.emplace_back(0,0);
    st.push_back({0,0,0,-1});

    while(!st.empty()){
        St &cur = st.back();
        if(cur.dir < 4){
            int d = cur.dir++;
            int ny = cur.y + dy2[d];
            int nx = cur.x + dx2[d];
            if(!vis[ny+OFF][nx+OFF]){
                cout << "? " << qc[d] << endl;
//              cout.flush();
                string r; cin >> r;
                if(r == "YES"){
                    vis[ny+OFF][nx+OFF] = true;
                    local.emplace_back(ny,nx);
                    st.push_back({ny,nx,0,d});
                }
            }
        } else {
            int e = cur.entry;
            st.pop_back();
            if(e!=-1){
                cout << "? " << rc[e] << endl;
//              cout.flush();
                string _;
                cin>>_;
            }
        }
    }

    int my=INT_MAX, mx=INT_MAX;
    for(auto &p: local){
        my = min(my, p.first);
        mx = min(mx, p.second);
    }
    vector<pair<int,int>> rel2;
    rel2.reserve(local.size());
    for(auto &p: local){
        rel2.emplace_back(p.first-my, p.second-mx);
    }
    sort(rel2.begin(), rel2.end());
    uint64_t h2 = rel2.size();
    for(auto &p: rel2){
        h2 = h2 * BASE + p.first;
        h2 = h2 * BASE + p.second;
    }
    auto [gy,gx] = mp[h2];
    int sy = gy - my, sx = gx - mx;
    cout << "! " << sy << " " << sx << endl;
//  cout.flush();
    return 0;
}
accuracy: 1
time_ms: 3130
memory_kb: 13216
final_rank: 4

code_snippet: // AOJ #2899 Accident
// 2025.5.29

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

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int H, W;
    cin >> H >> W;
    vector<string> g(H);
    for(int i = 0; i < H; i++) cin >> g[i];

    vector<vector<bool>> seen(H, vector<bool>(W,false));
    unordered_map<string, pair<int,int>> shape2anchor;
    shape2anchor.reserve(H*W/4);

    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            if(!seen[i][j] && g[i][j]=='#'){
                vector<pair<int,int>> cells;
                queue<pair<int,int>> q;
                seen[i][j]=true;
                q.push({i,j});
                cells.push_back({i,j});
                for(;!q.empty();q.pop()){
                    auto [y,x] = q.front();
                    constexpr int dy[4]={-1,1,0,0}, dx[4]={0,0,-1,1};
                    for(int d=0; d<4; d++){
                        int ny=y+dy[d], nx=x+dx[d];
                        if(ny>=0&&ny<H&&nx>=0&&nx<W
                           &&!seen[ny][nx]&&g[ny][nx]=='#'){
                            seen[ny][nx]=true;
                            q.push({ny,nx});
                            cells.push_back({ny,nx});
                        }
                    }
                }
                int minY=H, minX=W;
                for(auto &c: cells){
                    minY = min(minY, c.first);
                    minX = min(minX, c.second);
                }
                vector<pair<int,int>> rel;
                rel.reserve(cells.size());
                for(auto &c: cells){
                    rel.emplace_back(c.first-minY, c.second-minX);
                }
                sort(rel.begin(), rel.end());
                ostringstream oss;
                for(auto &p: rel){
                    oss << p.first << ',' << p.second << ';';
                }
                shape2anchor[oss.str()] = {minY, minX};
            }
        }
    }

    const int OFF = 500;
    static bool vis[1001][1001] = {};
    struct Node { int y, x, dir, entry; };
    vector<pair<int,int>> local_cells;
    local_cells.reserve(100000);

    const char mvC[4] = {'U','D','L','R'};
    const char opC[4] = {'D','U','R','L'};
    const int dy[4] = {-1,1,0,0};
    const int dx[4] = {0,0,-1,1};

    stack<Node> st;
    st.push({0,0,0,-1});
    vis[OFF][OFF] = true;
    local_cells.emplace_back(0,0);

    while(!st.empty()){
        Node &cur = st.top();
        if(cur.dir < 4){
            int d = cur.dir++;
            int ny = cur.y + dy[d];
            int nx = cur.x + dx[d];
            if(!vis[ny+OFF][nx+OFF]){
                cout << '?' << ' ' << mvC[d] << endl;
                cout.flush();
                string resp;
                cin >> resp;
                if(resp == "YES"){
                    vis[ny+OFF][nx+OFF] = true;
                    local_cells.emplace_back(ny, nx);
                    st.push({ny, nx, 0, d});
                }
            }
        } else {
            int e = cur.entry;
            st.pop();
            if(e != -1){
                cout << '?' << ' ' << opC[e] << endl;
                cout.flush();
                string dummy;
                cin >> dummy;
            }
        }
    }

    int minY = INT_MAX, minX = INT_MAX;
    for(auto &p: local_cells){
        minY = min(minY, p.first);
        minX = min(minX, p.second);
    }
    vector<pair<int,int>> rel2;
    rel2.reserve(local_cells.size());
    for(auto &p: local_cells){
        rel2.emplace_back(p.first - minY, p.second - minX);
    }
    sort(rel2.begin(), rel2.end());
    ostringstream oss2;
    for(auto &p: rel2){
        oss2 << p.first << ',' << p.second << ';';
    }
    string sig = oss2.str();

    auto it = shape2anchor.find(sig);
    if(it == shape2anchor.end()) return 0;
    auto [gy, gx] = it->second;

    int startY = gy - minY;
    int startX = gx - minX;

    cout << '!' << ' ' << startY << ' ' << startX << endl;
    cout.flush();
    return 0;
}
accuracy: 1
time_ms: 3150
memory_kb: 17016
final_rank: 5

code_snippet: #include <bits/stdc++.h>

const int dx[4]{ -1, 0, 1, 0 }, dy[4]{ 0, 1, 0, -1 };
const char dir[4]{ 'U', 'R', 'D', 'L' };

bool query(int d) {
    assert(0 <= d and d < 4);
    std::cout << '?' << ' ' << dir[d] << std::endl;
    std::string S;
    std::cin >> S;
    assert(S == "YES" or S == "NO");
    return S == "YES";
}

int main() {
    int H, W;
    std::cin >> H >> W;
    std::vector<std::string> S(H);
    for (auto& s : S) std::cin >> s;
    using Point = std::pair<int, int>;
    std::set<Point> T;
    auto dfs{[&](auto dfs, int x, int y) -> void {
        T.insert(Point{ x, y });
        for (int d{} ; d < 4 ; d++) {
            int nx{x + dx[d]}, ny{y + dy[d]};
            Point np{nx, ny};
            // std::cerr << d << ' ' << nx << ' ' << ny << std::endl;
            if (T.find(np) != T.end()) continue;
            if (query(d)) {
                dfs(dfs, nx, ny);
                bool back{query((d + 2) % 4)};
                assert(back);
            }
        }
    }};
    dfs(dfs, 0, 0);
    int minx{(int)1e9}, miny{(int)1e9};
    {
        for (auto [x, y] : T) {
            if (minx > x) {
                minx = x;
                miny = y;
            }
            else if (minx == x and miny > y) {
                minx = x;
                miny = y;
            }
        }
        std::set<Point> next;
        for (auto [x, y] : T) next.insert(Point{ x - minx, y - miny });
        T = std::move(next);
    }
    std::vector vis(H, std::vector<bool>(W));
    auto in{[&](int x, int y) -> bool {
        return 0 <= x and x < H and 0 <= y and y < W;
    }};
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            if (vis[i][j]) continue;
            if (S[i][j] == '.') continue;
            std::set<Point> shape;
            auto rec{[&](auto rec, int x, int y) -> void {
                shape.insert(Point{ x - i, y - j });
                vis[x][y] = true;
                for (int d{} ; d < 4 ; d++) {
                    int nx{x + dx[d]}, ny{y + dy[d]};
                    if (!in(nx, ny)) continue;
                    if (vis[nx][ny]) continue;
                    if (S[nx][ny] == '.') continue;
                    rec(rec, nx, ny);
                }
            }};
            rec(rec, i, j);
            if (shape == T) {
                std::cout << '!' << ' ' << i - minx << ' ' << j - miny << std::endl;
                return 0;
            }
        }
    }
    assert(false);
}
accuracy: 1
time_ms: 3340
memory_kb: 53156
final_rank: 10

code_snippet: #include <bits/stdc++.h>

int dx[4]{ -1, 0, 1, 0 };
int dy[4]{ 0, 1, 0, -1 };
char com[4]{ 'U', 'R', 'D', 'L' };

bool question(int d) {
    assert(0 <= d and d < 4);
    std::cout << '?' << ' ' << com[d] << std::endl;
    std::string res;
    std::cin >> res;
    if (res == "YES") {
        return true;
    }
    else if (res == "NO") {
        return false;
    }
    else {
        assert(false);
    }
}

int main() {
    int H, W;
    std::cin >> H >> W;
    std::vector<std::string> S(H);
    for (auto& s : S) std::cin >> s;
    std::set<std::pair<int, int>> set;
    set.insert(std::pair{ 0, 0 });

    auto dfs{[&](auto dfs, int x, int y) -> void {
        assert(set.find(std::pair{ x, y }) != set.end());
        for (int d{} ; d < 4 ; d++) {
            int nx{x + dx[d]}, ny{y + dy[d]};
            if (set.find(std::pair{ nx, ny }) != set.end()) continue;
            bool reach{question(d)};
            if (reach == false) continue;
            set.insert(std::pair{nx, ny});
            dfs(dfs, nx, ny);
            bool back{question((d + 2) % 4)};
            assert(back == true);
        }
    }};
    dfs(dfs, 0, 0);
    int lx{(int)1e9}, ly{(int)1e9};
    for (auto [x, y] : set) {
        if (lx > x) {
            lx = x;
            ly = y;
        }
        else if (lx == x and ly > y) {
            lx = x;
            ly = y;
        }
    }
    {
        std::set<std::pair<int, int>> tmp;
        for (const auto& [x, y] : set) {
            tmp.insert(std::pair{x - lx, y - ly});
        }
        set = std::move(tmp);
    }

    auto in{[&](int x, int y) -> bool {
        return 0 <= x and x < H and 0 <= y and y < W;
    }};
    
    std::vector vis(H, std::vector<bool>(W));
    auto dfs2{[&](auto dfs, int x, int y, std::set<std::pair<int, int>>& st) -> void {
        assert(in(x, y));
        assert(S[x][y] == '#');
        st.insert(std::pair{x, y});
        vis[x][y] = true;
        for (int d{} ; d < 4 ; d++) {
            int nx{x + dx[d]}, ny{y + dy[d]};
            if (!in(nx, ny)) continue;
            if (S[nx][ny] == '.') continue;
            if (vis[nx][ny]) continue;
            dfs(dfs, nx, ny, st);
        }
    }};
    
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            if (S[i][j] == '.') continue;
            if (vis[i][j]) continue;
            std::set<std::pair<int, int>> st;
            dfs2(dfs2, i, j, st);
            {
                std::set<std::pair<int, int>> tmp;
                for (const auto& [x, y] : st) {
                    tmp.insert(std::pair{x - i, y - j});
                }
                st = std::move(tmp);
            }
            if (set == st) {
                int ansx{i - lx}, ansy{j - ly};
                assert(in(ansx, ansy));
                assert(S[ansx][ansy] == '#');
                std::cout << '!' << ' ' << ansx << ' ' << ansy << std::endl;
                return 0;
            }
        }
    }
    assert(false);
}
accuracy: 1
time_ms: 3450
memory_kb: 64760
final_rank: 15

code_snippet: #include <bits/stdc++.h>

constexpr int DEBUG = 0;

const int DI[] = {-1, 0, 1, 0};
const int DJ[] = {0, 1, 0, -1};
const char DIRECTION[] = {'U', 'R', 'D', 'L'};

int h, w;
std::vector<std::string> s;

struct Island {
    Island(int i, int j, std::vector<std::pair<int, int>> p): rela_i(i), rela_j(j), ps(p) {}
    int rela_i;
    int rela_j;
    std::vector<std::pair<int, int>> ps;
};

int init_i = -1;
int init_j = -1;
int cur_i = -1;
int cur_j = -1;

void init() {
    if (DEBUG) {
        std::random_device rd;
        std::mt19937 mt(rd());
        while (true) {
            int i = mt() % h;
            int j = mt() % w;
            if (s[i][j] == '#') {
                cur_i = init_i = i;
                cur_j = init_j = j;
                break;
            }
        }
    }
}

void answer(int i, int j) {
    assert(0 <= i && i < h);
    assert(0 <= j && j < w);
    if (DEBUG) {
        assert(init_i == i);
        assert(init_j == j);
        std::cout << "init " << init_i << ' ' << init_j << std::endl;
        std::cout << "ans " << i << ' ' << j << std::endl;
    } else {
        std::cout << "! " << i << ' ' << j << std::endl;
    }
}

std::string query(char c) {
    if (DEBUG) {
        int ni = cur_i + (c == 'U' ? -1 : (c == 'D' ? 1 : 0));
        int nj = cur_j + (c == 'L' ? -1 : (c == 'R' ? 1 : 0));
        if (s[ni][nj] == '.') return "NO";
        cur_i = ni;
        cur_j = nj;
        return "YES";
    } else {
        std::cout << '?' << ' ' << c << std::endl;
        std::string s;
        std::cin >> s;
        return s;
    }
}

void solve() {
    init();
    int first_u_count = 0;
    while (true) {
        auto res = query('U');
        if (res == "YES") {
            first_u_count++;
        } else {
            break;
        }
    } 
    assert(query('U') == "NO");

    std::set<std::pair<int, int>> used;
    auto dfs = [&](auto self, int i, int j) -> void {
        for (int d = 0; d < 4; d++) {
            int ni = i + DI[d];
            int nj = j + DJ[d];
            std::pair<int, int> np = {ni, nj};
            if (used.find(np) != used.end()) continue;
            auto res = query(DIRECTION[d]);
            if (res == "YES") {
                used.insert(np);
                self(self, ni, nj);
                auto temp = query(DIRECTION[(d + 2) % 4]);
                assert(temp == "YES");
            }
        }
    };
    used.emplace(0, 0);
    dfs(dfs, 0, 0);

    auto norm = [](std::vector<std::pair<int, int>> p) {
        int min_i = 1e9;
        int min_j = 1e9;
        for (auto [i, j]: p) {
            min_i = std::min(min_i, i);
            min_j = std::min(min_j, j);
        }
        for (auto& [i, j]: p) {
            i -= min_i;
            j -= min_j;
        }
        return Island(min_i, min_j, p);
    };
    auto target = norm(std::vector(used.begin(), used.end()));
    std::sort(target.ps.begin(), target.ps.end());

    std::pair<int, int> rela = {};
    {
        int min_i = 1e9;
        int min_j = 1e9;
        for (auto [i, j]: used) {
            min_i = std::min(min_i, i);
            min_j = std::min(min_j, j);
        }
        rela = {-min_i, -min_j};
    }

    std::vector<Island> islands;
    {
        std::vector<std::pair<int, int>> island;
        std::vector used(h, std::vector<bool>(w));
        auto dfs = [&](auto self, int i, int j) -> void {
            assert(!used[i][j]);
            used[i][j] = true;
            island.emplace_back(i, j);
            for (int d = 0; d < 4; d++) {
                int ni = i + DI[d];
                int nj = j + DJ[d];
                if (s[ni][nj] == '.') continue;
                if (used[ni][nj]) continue;
                self(self,ni, nj);
            }
        };
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (s[i][j] == '.') continue;
                if (used[i][j]) continue;
                island.clear();
                dfs(dfs, i, j);
                islands.push_back(norm(island));
            }
        }
    }

    // matchする島を探すパート
    int match_id = -1;
    for (auto& island: islands) {
        std::sort(island.ps.begin(), island.ps.end());
    }
    for (int i = 0; i < (int)islands.size(); i++) {
        if (islands[i].ps == target.ps) {
            int x = -target.rela_i + islands[i].rela_i + first_u_count;
            int y = -target.rela_j + islands[i].rela_j;
            answer(x, y);
            return;
        }
    }
    assert(match_id != -1);
}

int main() {
    std::cin >> h >> w;
    s.resize(h);
    for (int i = 0; i < h; i++) std::cin >> s[i];
    solve();
}
accuracy: 1
time_ms: 3380
memory_kb: 62876
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int MAX=505,INF=1<<30;
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T> bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())

int H,W;
vector<int> dh={0,1,0,-1},dw={1,0,-1,0};
char S[MAX][MAX];
int seen[MAX][MAX];
string U="RDLU";

int t=0;

map<pair<int,int>,int> MA;

void DFS(int h,int w){

    for(int k=0;k<4;k++){
        int toh=h+dh[k];
        int tow=w+dw[k];

        if(MA.count(mp(toh,tow))) continue;

        cout<<"? "<<U[k]<<endl;
        string T;cin>>T;
        if(T=="NO"){
            MA[mp(toh,tow)]=0;
            continue;
        }else{
            MA[mp(toh,tow)]=1;
            DFS(toh,tow);

            cout<<"? "<<U[(k+2)%4]<<endl;
            string TT;cin>>TT;
        }
    }
}

void solve(int h,int w,map<pair<int,int>,int> &memo){
    if(seen[h][w]==t) return;
    memo[mp(h,w)]=(S[h][w]=='#');
    seen[h][w]=t;

    if(S[h][w]=='.') return;

    for(int k=0;k<4;k++){
        int toh=h+dh[k];
        int tow=w+dw[k];
        if(toh<0||toh>=H||tow<0||tow>=W) continue;

        if(seen[toh][tow]==t) continue;

        solve(toh,tow,memo);
    }
}

int main(){
    cin>>H>>W;
    for(int i=0;i<H;i++) for(int j=0;j<W;j++) cin>>S[i][j];

    map<vector<pair<int,int>>,pair<int,int>> dp;

    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            t++;
            if(S[i][j]=='#'&&!seen[i][j]){
                map<pair<int,int>,int> memo;
                solve(i,j,memo);
                vector<pair<int,int>> X;
                for(auto a:memo) X.push_back(a.fi);
                sort(all(X));
                int mih=INF,miw=INF;
                for(auto x:X){
                    chmin(mih,x.fi);
                    chmin(miw,x.se);
                }

                for(int k=0;k<si(X);k++){
                    X[k].fi-=mih;
                    X[k].se-=miw;
                }

                dp[X]=mp(mih,miw);
            }
        }
    }

    MA[mp(0,0)]=1;
    DFS(0,0);

    vector<pair<int,int>> X;
    for(auto x:MA) X.push_back(x.fi);

    sort(all(X));

    int mih=INF,miw=INF;
    for(auto x:X){
        chmin(mih,x.fi);
        chmin(miw,x.se);
    }
    for(int k=0;k<si(X);k++){
        X[k].fi-=mih;
        X[k].se-=miw;
    }

    assert(dp.count(X));
    auto s=dp[X];
    cout<<"! "<<s.fi-mih<<" "<<s.se-miw<<endl;

}
accuracy: 1
time_ms: 3210
memory_kb: 67548
final_rank: 14

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

vector<vector<bool>> A;
vector<vector<bool>> visited;
vector<char> D = {'R', 'U', 'L', 'D'};
array<int, 4> dw = {1, 0, -1, 0};
array<int, 4> dh = {0, -1, 0, 1};

bool query(char dir){
    cout << "? " << dir << endl;
    string res;
    cin >> res;
    return res == "YES";
}

void ans(int h, int w){
    cout << "! " << h << " " << w << endl;
}

int min_hb, min_wb;

vector<vector<bool>> create(vector<array<int, 2>> route){
    int min_w = route[0][1], min_h = route[0][0], max_w = route[0][1], max_h = route[0][0];
    for (const auto &r: route){
        int w = r[1];
        int h = r[0];
        min_w = min(min_w, w);
        min_h = min(min_h, h);
        max_w = max(max_w, w);
        max_h = max(max_h, h);
    }
    vector<vector<bool>> M(max_h - min_h + 1, vector<bool>(max_w - min_w + 1));
    for (const auto &r: route){
        M[r[0] - min_h][r[1] - min_w] = true;
    }
    min_hb = min_h;
    min_wb = min_w;

    return M;
}

tuple<int, int> get_min(const vector<array<int, 2>> &route){
    int min_w = route[0][1];
    int min_h = route[0][0];
    for (const auto &r: route){
        int w = r[1];
        int h = r[0];
        min_w = min(min_w, w);
        min_h = min(min_h, h);
    }

    return make_tuple(min_h, min_w);
}

void dfs(int h, int w, vector<array<int, 2>> &route){
    if (visited[h][w] or !A[h][w])
        return;
    route.push_back({h, w});
    visited[h][w] = true;
    for (int d = 0; d < 4; d++){
        dfs(h + dh[d], w + dw[d], route);
    }
}

void dfs2(int h, int w, vector<array<int, 2>> &route){
    route.push_back({h, w});
    //cerr << h << " " << w << endl;
    visited[h][w] = true;
    for (int d = 0; d < 4; d++){
        int nh = h + dh[d];
        int nw = w + dw[d];
        if (visited[nh][nw])
            continue;
        if (!query(D[d]))
            continue;
        dfs2(nh, nw, route);
        query(D[(d + 2) % 4]);
    }
}

void debug(vector<vector<bool>> &A){
    for (const auto &a: A){
        for (const auto &b: a)
            cerr << b << " ";
        cerr << endl;
    }
}

signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    cout << fixed << setprecision(20);
    int H, W;
    cin >> H >> W;
    A.resize(H, vector<bool>(W));
    for (int i = 0; i < H; i++){
        string s;
        cin >> s;
        for (int j = 0; j < W; j++){
            A[i][j] = s[j] == '#';
        }
    }

    vector<array<int, 2>> R;
    visited.resize(4 * H, vector<bool>(4 * W));
    dfs2(2 * H, 2 * W, R);
    auto M = create(R);

    for (auto &v: visited){
        fill(v.begin(), v.end(), false);
    }
    //debug(M);

    for (int h = 0; h < H; h++){
        for (int w = 0; w < W; w++){
            if (visited[h][w] or !A[h][w])
                continue;
            vector<array<int, 2>> route;
            dfs(h, w, route);
            auto B = create(route);
            //debug(B);
            if (M == B){
                //debug(B);
                int min_ha, min_wa, min_hb, min_wb;
                tie(min_ha, min_wa) = get_min(R);
                tie(min_hb, min_wb) = get_min(route);
                ans(min_hb + 2 * H - min_ha, min_wb + 2 * W - min_wa);
            }
        }
    }
    
}
accuracy: 1
time_ms: 3950
memory_kb: 32084
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
string dir = "RDLU";

int h, w;
string field[505];
vector<P> pos, tmp;
bool visited[1005][1005];
int mrg = 500;

// 位置の候補を消しながら移動
void move(int k, bool f){
    tmp.clear();
    for(auto p : pos){
        int ni = p.first+di[k], nj = p.second+dj[k];
        if((field[ni][nj]=='#') == f){
            if(f) tmp.push_back({ni,nj});
            else tmp.push_back(p);
        }
    }
    pos = tmp;
}

bool dfs(int i, int j){
    visited[i+mrg][j+mrg] = true;
    for(int k=0;k<4;k++){
        int ni = i+di[k], nj = j+dj[k];
        if(visited[ni+mrg][nj+mrg]) continue;
        string res;
        cout << "? " << dir[k] << endl;
        cin >> res;
        bool ok = res == "YES";
        move(k, ok);
        if(ok){
            if(dfs(ni, nj)) return true;
            cout << "? " << dir[(k+2)%4] << endl;
            cin >> res;
            move((k+2)%4, true);
        }
    }
    if(pos.size() == 1){
        cout << "! " << pos.front().first-i << " " << pos.front().second-j << endl;
        return true;
    }
    return false;
}

int main() {
    cin >> h >> w;
    for(int i=0;i<h;i++){
        cin >> field[i];
        for(int j=0;j<w;j++){
            if(field[i][j] == '#'){
                pos.push_back({i,j});
            }
        }
    }
    dfs(0,0);
    return 0;
}
accuracy: 1
time_ms: 1450
memory_kb: 19756
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
string dir = "RDLU";

int h, w;
string field[505];
vector<P> pos, tmp;
bool visited[1005][1005];
int mrg = 500;

void move(int k, bool f){
    tmp.clear();
    for(auto p : pos){
        int ni = p.first+di[k], nj = p.second+dj[k];
        if((field[ni][nj]=='#') == f){
            if(f) tmp.push_back({ni,nj});
            else tmp.push_back(p);
        }
    }
    pos = tmp;
}

bool dfs(int i, int j){
    visited[i+mrg][j+mrg] = true;
    if(pos.size() == 1){
        cout << "! " << pos.front().first-i << " " << pos.front().second-j << endl;
        return true;
    }
    for(int k=0;k<4;k++){
        int ni = i+di[k], nj = j+dj[k];
        if(visited[ni+mrg][nj+mrg]) continue;
        cout << "? " << dir[k] << endl;
        string res;
        cin >> res;
        bool ok = res == "YES";
        move(k, ok);
        if(ok){
            if(dfs(ni, nj)) return true;
            cout << "? " << dir[(k+2)%4] << endl;
            cin >> res;
            move((k+2)%4, true);
        }
    }
    if(pos.size() == 1){
        cout << "! " << pos.front().first-i << " " << pos.front().second-j << endl;
        return true;
    }
    return false;
}

int main() {
    cin >> h >> w;
    for(int i=0;i<h;i++){
        cin >> field[i];
        for(int j=0;j<w;j++){
            if(field[i][j] == '#'){
                pos.push_back({i,j});
            }
        }
    }
    dfs(0,0);
    return 0;
}
accuracy: 1
time_ms: 1430
memory_kb: 19676
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

int h, w, now = 0;
int xd[] = {1, 0, -1, 0};
int yd[] = {0, 1, 0, -1};
char d[] = {'R', 'D', 'L', 'U'};
int moved[2] = {0};
// mp[y][x]
bool mp[505][505] = {0};
string ans;
// fi = y,se = x
list<pair<int, int>> ls;

void solve();

int main() {
  cin >> h >> w;
  for(int i = 0; i < h; ++i)
    for(int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      mp[i][j] = (c == '#');
    }
  for(int i = 0; i < h; ++i)
    for(int j = 0; j < w; ++j)
      if(mp[i][j]) ls.push_back(make_pair(i, j));
  solve();
  pair<int, int> a = *(ls.begin());
  cout << "! " << a.fi << " " << a.se << endl;
  return 0;
}

void solve() {
  int nx, ny;
  pair<int, int> np;
  bool init = 1;
  while(ls.size() > 1) {
    bool isyes;
    cout << "? " << d[now] << endl;
    cin >> ans;
    isyes = (ans == "YES");
    nx = moved[0] + xd[now];
    ny = moved[1] + yd[now];
    for(auto i = ls.begin(); i != ls.end(); ++i) {
      np = *i;
      int x = np.se + nx, y = np.fi + ny;
      if(x >= 0 && x < w && y >= 0 && y < h &&
         mp[y][x] != isyes) {
        i = ls.erase(i);
        --i;
      }
    }
    if(isyes) {
      moved[0] += xd[now];
      moved[1] += yd[now];
      if(!init) now = (3 + now) % 4;
    }
    else {
      if(init) init = 0;
      now = (now + 1) % 4;
    }
  }
}
accuracy: 1
time_ms: 790
memory_kb: 11012
final_rank: 1

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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

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

typedef pair<int,int>P;

char direc[]={'U','R','D','L'};

bool used[1007][1007];

int AX,AY;

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int h, w; cin >> h >> w;
    vector<vector<bool> > g(h + 3, vector<bool> (w + 3, false));
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char input; cin >> input;
            g[i][j] = (input == '#');
        }
    }

    int right = 0,p=0;     //初期位置からどれだけ右に進んだか
    while(true){
        cout << "? R" << endl;p++;
        string res; cin >> res;
        if(res == "YES") right++;
        else break;
    }

    //初期位置からの差分

    set<P>st,ans;
    int sub_i = 0, sub_j = 0;

    int x=504, y=504 , d=2;

    while(1){

        if( st.size() && x==504 && y==504 && d==2 ) break;

        st.insert(P(x,y));
       
        cout<<"? "<<direc[d]<<endl;p++;


        string res;
        cin>>res;
        if(res=="YES"){
            x += dx[d] ;
            y += dy[d] ;
            d=(d+3)%4;
        }
        else d=(d+1)%4;
    }


    for(int sx = 1; sx <= w; sx++){
        for(int sy = 1; sy <= h; sy++)if(!used[sx][sy])
        {

            ans.clear();
            
            if(!g[sy][sx] || g[sy][sx + 1]) continue;

            x = 504;
            y = 504;
            d = 2;
            int cnt=0;
            
            while(true){


                if( cnt && x==504 && y==504 && d==2 ){
                    if(st.size()!=ans.size())break;
                    int ax,ay;
                    int X=(*st.begin()).first-(*ans.begin()).first;
                    int Y=(*st.begin()).second-(*ans.begin()).second;
                    for(set<P>::iterator it=ans.begin(),it2=st.begin();it!=ans.end();it++,it2++){

                        if((*it2).first!=(*it).first+X)goto L;
                        if((*it2).second!=(*it).second+Y)goto L;

                        if((*it2).first==504&&(*it2).second==504) ax=(*it).first,ay=(*it).second;
                    }
                    
                        cout << "! " << ay - 1 -504+sy << " " << ax - 1 - right -504+sx << endl;
                        return 0;
                    
  

                        L:;
                        break;

                }

                cnt++;

                ans.insert(P(x,y));
                used[sx+x-504][sy+y-504]=1;
                
                int nx = x + dx[d];
                int ny = y + dy[d];
                
                if(g[ny + sy - 504][nx + sx- 504]){
                    x = nx ;
                    y = ny ; 
                    d=(d+3)%4; 
                }
                else{
                    d=(d+1)%4;
                }
            }       

        }
    }
    return 0;
}
accuracy: 1
time_ms: 3890
memory_kb: 15400
final_rank: 6

code_snippet: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0 ; i < (int)(n) ; i++)
typedef long long ll;
typedef long double Double;

namespace stream {
    template<class T>
    class Stream {
        std::vector<T> instance;
    public:

        Stream(const std::vector<T> &instance) : instance(instance) {}


        template<class F>
        auto filter(const F &func) {
            return filterPrivate([&](const T &t, int &idx) { return func(t); });
        }

        template<class F>
        auto filterWithIndex(const F &func) {
            return filterPrivate(func);
        }

        template<class F>
        auto map(const F &func) {
            return mapPrivate([&](const T &t, int &idx) { return func(t); });
        }

        template<class F>
        auto mapWithIndex(const F &func) {
            return mapPrivate(func);
        }

        template<class F, class R>
        R reduce(const F &func, const R &init_value) {
            R ans = init_value;
            for (int i = 0; i < instance.size(); i++) {
                ans = func(ans, instance[i]);
            }
            return ans;
        }

        template<class F>
        Stream<T> sort(F cmp) {
            std::vector<T> v = instance;
            std::sort(v.begin(), v.end(), cmp);
            return Stream<T>(v);
        }

        Stream<T> sort() {
            std::vector<T> v = instance;
            std::sort(v.begin(), v.end());
            return Stream<T>(v);
        }

        Stream<T> sort_reversed() {
            std::vector<T> v = instance;
            std::sort(v.rbegin(), v.rend());
            return Stream<T>(v);
        }

        Stream<T> unique() {
            std::unordered_set<T> existing;
            std::vector<T> ans;
            for (auto &&i : instance) {
                if (existing.count(i)) { continue; }
                existing.insert(i);
                ans.push_back(i);
            }
            return Stream<T>(ans);
        }

        unsigned long count() {
            return instance.size();
        }

        T sum() {
            return std::accumulate(instance.begin(), instance.end(), T());
        }

        std::vector<T> collect() {
            return instance;
        }

        std::set<T> toSet() {
            return std::set<T>(instance.begin(), instance.end());
        }

    private:
        template<class F>
        Stream<T> filterPrivate(const F &func) {
            std::vector<T> ans;

            for (int i = 0; i < instance.size(); i++) {
                if (func(instance[i], i)) {
                    ans.push_back(instance[i]);
                }
            }
            return Stream<T>(ans);
        }

        template<class F>
        auto mapPrivate(const F &func) {
            using R = typename std::result_of<F(T, int)>::type;
            std::vector<R> ans;
            for (int i = 0; i < instance.size(); i++) {
                ans.push_back(func(instance[i], i));
            }
            return Stream<R>(ans);
        }

    };

    template<class T>
    Stream<T> _(const std::vector<T> &instance) {
        return Stream<T>(instance);
    };

    template<class T>
    Stream<T> _(const std::set<T> &instance) {
        return Stream<T>(std::vector<T>(instance.begin(), instance.end()));
    };

    template<class T, class U>
    Stream<std::pair<T, U>> _(const std::vector<T> &a, std::vector<U> &b) {
        const unsigned long &size = std::min(a.size(), b.size());
        std::vector<std::pair<T, U>> zipped;
        return _(zipped);
    }

    Stream<int> range(int a, int b) {
        if (a >= b) return Stream<int>(std::vector<int>());
        std::vector<int> ans(b - a);
        for (int i = 0; i < b - a; i++)
            ans[i] = a + i;
        return Stream<int>(ans);
    }

    Stream<int> range(int n) {
        return range(0, n);
    }
}

namespace io {
    template<class T>
    void read(std::vector<T> &v) {
        for (int i = 0; i < v.size(); i++) std::cin >> v[i];
    }

    std::vector<int> readIntArray(int n) {
        std::vector<int> a(n);
        read(a);
        return a;
    }

    long long readLong() {
        long long x;
        std::cin >> x;
        return x;
    }

    int readInt() {
        long long x = readLong();
        assert(x >= INT_MIN && x <= INT_MAX);
        return (int) x;
    }

    std::string readWord() {
        std::string x;
        std::cin >> x;
    }

    template<class T>
    void output(const std::vector<T> &v, char delimiter = ' ') {
        if (v.size() > 0) {
            std::cout << v[0];
            for (int i = 1; i < v.size(); i++) {
                std::cout << delimiter << v[i];
            }
        }
        std::cout << std::endl;
    }

    template<class T>
    void output(const T &v) {
        std::cout << v << std::endl;
    }

    void output(double x) {
        printf("%.10lf\n", x);
    }
};

using namespace std;
using namespace stream;
using namespace io;

struct P {
    P(int x, int y) : x(x), y(y) {}

    bool operator==(const P &rhs) const {
        return x == rhs.x &&
               y == rhs.y;
    }

    bool operator!=(const P &rhs) const {
        return !(rhs == *this);
    }

    bool operator<(const P &rhs) const {
        if (x < rhs.x)
            return true;
        if (rhs.x < x)
            return false;
        return y < rhs.y;
    }

    bool operator>(const P &rhs) const {
        return rhs < *this;
    }

    bool operator<=(const P &rhs) const {
        return !(rhs < *this);
    }

    bool operator>=(const P &rhs) const {
        return !(*this < rhs);
    }

    int x, y;
};


bool ask(char &i, P p, bool force);


bool already[1010][1010];


vector<P> lands;
int H, W;
string dirs = "RULD";

void dfs(const P &cur, const P &prev, char backDir) {
    lands.push_back(cur);
    P neighbors[] = {P(cur.x + 1, cur.y), P(cur.x, cur.y - 1), P(cur.x - 1, cur.y), P(cur.x, cur.y + 1)};

    for (int i = 0; i < 4; i++) {
        if (neighbors[i] == prev) continue;
        if (ask(dirs[i], neighbors[i], false)) {
            dfs(neighbors[i], cur, dirs[(i + 2) % 4]);
        }
    }
    if (backDir != '-') {
        ask(backDir, prev, true);
    }
}

char tmp[128];

bool ask(char &i, P p, bool force) {
    if (!force) {
        if (already[p.y + 505][p.x + 505]) return false;
    }
    printf("? %c\n", i);
    fflush(stdout);
    scanf("%s", tmp);
    already[p.y + 505][p.x + 505] = true;
    return tmp[0] == 'Y';
}

char field[500][500];

vector<P> tmpLands;

void lastDfs(P cur) {

    if (cur.y < 0 || cur.y >= H || cur.x < 0 || cur.x >= W || field[cur.y][cur.x] == '.') return;
    field[cur.y][cur.x] = '.';
    tmpLands.push_back(cur);
    P neighbors[] = {P(cur.x + 1, cur.y), P(cur.x, cur.y - 1), P(cur.x - 1, cur.y), P(cur.x, cur.y + 1)};
    for (int i = 0; i < 4; i++) {
        lastDfs(neighbors[i]);
    }

}

P normalize(vector<P> &l) {
    sort(l.begin(), l.end());
    P p = l[0];
    for (int i = (int) (l.size() - 1); i >= 0; i--) {
//        cout << "(" << l[i].x << " " << l[i].y << ")" << endl << endl;
        l[i] = P(l[i].x - l[0].x, l[i].y - l[0].y);
    }
    return p;
}

int main() {
    lands.reserve(250000);
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            scanf(" %c", &field[i][j]);
        }
    }
    already[+505][+505] = true;
    dfs(P(0, 0), P(-1, -1), '-');


    tmpLands.reserve(250000);

    P offsetOrg = normalize(lands);
//    cout << offsetOrg.y << " " << offsetOrg.x << endl;
    for (int i = H - 1; i >= 0; i--) {
        for (int j = W - 1; j >= 0; j--) {
            if (field[i][j] == '#') {
                tmpLands.clear();
                lastDfs(P(j, i));
                if (tmpLands.size() == lands.size()) {
                    auto offset = normalize(tmpLands);
                    if (tmpLands == lands) {
                        int x = offset.x - offsetOrg.x;
                        int y = offset.y - offsetOrg.y;

                        printf("! %d %d\n", y, x);
                        fflush(stdout);
                        return 0;
                    }
                }
            }
        }
    }


}
accuracy: 1
time_ms: 3960
memory_kb: 30408
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;

string d = "URDL";
const int dy[4] = {-1,0,1,0};
const int dx[4] = {0,1,0,-1};

inline bool query(int idx){
    printf("? %c\n", d[idx]);
    fflush(stdout);

    char s[10];
    scanf("%s",s);
    return s[0]=='Y';
}

const int B = 505;
const int N = 1010;
bool already[N][N], trail[N][N];

void dfs(int y, int x){
    // printf(" (%d %d)\n",y,x);
    rep(i,4){
        if(already[y+dy[i]+B][x+dx[i]+B]) continue;
        bool res = query(i);
        already[y+dy[i]+B][x+dx[i]+B] = true;
        if(res){
            dfs(y+dy[i],x+dx[i]);
            trail[y+dy[i]+B][x+dx[i]+B] = true;
            query(i^2);
        }
    }
}

int main(){
    int h,w;
    cin >>h >>w;
    vector<string> s(h);
    rep(i,h) cin >>s[i];

    trail[B][B] = true;
    already[B][B] = true;
    dfs(0,0);

    int ty = h, tx = w;
    rep(i,N)rep(j,N)if(trail[i][j]){
        int py = i-B, px = j-B;
        if(py < ty){
            ty = py;
            tx = px;
        }
        else if(py == ty) tx = min(tx,px);
    }

    set<pi> fix_trail;
    rep(i,N)rep(j,N)if(trail[i][j]){
        int py = i-B, px = j-B;
        fix_trail.insert({py-ty, px-tx});
    }

    auto IN = [&](int y, int x){
        return 0<=y && y<h && 0<=x && x<w;
    };

    vector<vector<bool>> vis(h,vector<bool>(w));
    rep(i,h)rep(j,w){
        if(s[i][j]=='.' || vis[i][j]) continue;

        queue<pi> que;
        vector<pi> cc;
        vis[i][j] = true;
        que.push({i,j});
        cc.pb({i,j});
        while(!que.empty()){
            pi now = que.front();
            que.pop();
            rep(k,4){
                int ny=now.fi+dy[k], nx = now.se+dx[k];
                if(IN(ny,nx) && s[ny][nx]=='#' && !vis[ny][nx]){
                    vis[ny][nx] = true;
                    que.push({ny,nx});
                    cc.pb({ny,nx});
                }
            }
        }

        int my=h, mx=w;
        for(pi p:cc){
            if(p.fi < my){
                my = p.fi;
                mx = p.se;
            }
            else if(p.fi == my) mx = min(mx,p.se);
        }

        set<pi> fix_cc;
        for(pi p:cc) fix_cc.insert({p.fi-my, p.se-mx});

        if(fix_cc == fix_trail){
            printf("! %d %d\n", i-ty, j-tx);
            fflush(stdout);
            break;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 3920
memory_kb: 51980
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#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)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<short,short> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 505;

char c[] = {'D', 'L', 'U', 'R'};
short dx[] = {1,0,-1,0};
short dy[] = {0,-1,0,1};

short mp[2*MAX_N][2*MAX_N];

void dfs(short x, short y){
    rep(i,4){
        short nx = x + dx[i], ny = y + dy[i];
        if(!mp[nx][ny]){
            cout << "? " << c[i] << "\n";
            string s;
            cin >> s;
            if(s == "YES"){
                mp[nx][ny] = 2;
                dfs(nx, ny);
                cout << "? " << c[(i+2)%4] << "\n";
                cin >> s;
            }else{
                mp[nx][ny] = 3;
            }
        }
    }
}

short h, w;
bool used[MAX_N][MAX_N];
string s[MAX_N];
vector<P> eda;

void dfs(int& id, short x, short y)
{
    used[x][y] = true;
    id++;
    rep(i,4){
        short nx = x + dx[i], ny = y + dy[i];
        if(nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '#'){
            if(!used[nx][ny]){
                dfs(id,nx,ny);
            }
        }
    }
}

bool ch[2*MAX_N][2*MAX_N];
P pos[MAX_N*MAX_N];
int cnt[MAX_N*MAX_N];
vector<short> opr;

void dfs(P p)
{
    ch[p.fi][p.se] = true;
    rep(i,4){
        short nx = p.fi + dx[i], ny = p.se + dy[i];
        short val = mp[nx][ny];
        if(val >= 1 && val <= 2){
            if(!ch[nx][ny]){
                opr.pb(i);
                dfs(P(nx, ny));
                opr.pb((i+2)%4);
            }
        }
    }
}

bool dfs(int& id, P p)
{
    if(id == len(opr)) return true;
    short nx = p.fi + dx[opr[id]], ny = p.se + dy[opr[id]];
    if(nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '#'){
        id++;
        return dfs(id, P(nx, ny));
    }else{
        return false;
    }
}

int main()
{
    mp[MAX_N][MAX_N] = 1;
    cin >> h >> w;
    rep(i,h){
        cin >> s[i];
    }
    int bue = 0;
    rep(i,h){
        rep(j,w){
            if(!used[i][j] && s[i][j] == '#'){
                eda.clear();
                int id = 0;
                dfs(id, i, j);
                cnt[bue] = id;
                pos[bue] = P(i, j);
                bue++;
            }
        }
    }
    dfs(MAX_N, MAX_N);
    P mn = {2*MAX_N, 2*MAX_N};
    int num = 0;
    rep(i,2*MAX_N){
        rep(j,2*MAX_N){
            if(mp[i][j] >= 1 && mp[i][j] <= 2){
                num++, cmn(mn, P(i,j));
            }
        }
    }
    dfs(mn);
    // sar(cnt, bue);
    // svec(opr);
    rep(i,bue){
        int id = 0;
        if(num == cnt[i] && dfs(id, pos[i])){
            cout << "! " << pos[i].fi+MAX_N+-mn.fi << " " << pos[i].se+MAX_N-mn.se << "\n";
            return 0;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 3970
memory_kb: 42604
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
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; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　 |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/




string dir[4] = { "L","D", "R", "U" };
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 } ;
int H, W;
string B[505];
vector<int> history;
//---------------------------------------------------------------------------------------------------
int check(int x, int y, int d) {
    int xx = x + dx[d];
    int yy = y + dy[d];

    if (xx < 0 or W <= xx or yy < 0 or H <= yy) return 0;
    if (B[yy][xx] == '.') return 0;
    return 1;
}
//---------------------------------------------------------------------------------------------------
int cux = 3, cuy = 1;
int dbg = 0;
int ask(int com) {
    printf("? %s\n", dir[com].c_str());
    fflush(stdout);

    if (dbg) {
        int xx = cux + dx[com];
        int yy = cuy + dy[com];

        if (xx < 0 or W <= xx or yy < 0 or H <= yy) return 0;
        if (B[yy][xx] == '.') return 0;
        cux = xx;
        cuy = yy;
        return 1;
    }

    string res; cin >> res;

    if (res == "NO") return 0;
    return 1;
}
//---------------------------------------------------------------------------------------------------
int vis[2010][2010][4];
vector<pair<int, int>> mov;
int lastx, lasty;
void record() {
    while(ask(0)) history.push_back(0);

    vector<pair<int, int>> tmp;

    int x = 1010, y = 1010, d = 1;
    int mix = 1010, miy = 1010;
    while (vis[y][x][d] == 0) {
        vis[y][x][d] = 1;
        tmp.push_back({ x, y });
        chmin(mix, x);
        chmin(miy, y);

        rep(dd, 0, 4) {
            int d2 = (d + 3 + dd) % 4;

            int res = ask(d2);
            if (res) {
                history.push_back(d2);
                x += dx[d2];
                y += dy[d2];
                d = d2;
                break;
            }
        }
    }

    lastx = x - mix;
    lasty = y - miy;
    fore(p, tmp) mov.push_back({ p.first - mix, p.second - miy });
    sort(all(mov));
    mov.erase(unique(all(mov)), mov.end());
}
//---------------------------------------------------------------------------------------------------
int done[501][501][4], used[501][501];
void search() {
    rep(sy, 0, H) rep(sx, 0, W) if (B[sy][sx] == '#') {
        if (done[sy][sx][0] or done[sy][sx][1] or done[sy][sx][2] or done[sy][sx][3]) continue;
        if (B[sy - 1][sx] == '#') continue;
        if (B[sy][sx - 1] == '#') continue;

        vector<pair<int, int>> mov2;

        int x = sx, y = sy, d = 0;
        int mix = sx, miy = sy;
        while (done[y][x][d] == 0) {
            done[y][x][d] = 1;
            if (!used[y][x]) {
                mov2.push_back({ x, y });
                used[y][x] = 1;
            }
            chmin(mix, x);
            chmin(miy, y);

            rep(dd, 0, 4) {
                int d2 = (d + 3 + dd) % 4;

                int res = check(x, y, d2);
                if (res) {
                    x += dx[d2];
                    y += dy[d2];
                    d = d2;
                    break;
                }
            }
        }

        fore(p, mov2) p.first -= mix, p.second -= miy;
        sort(all(mov2));

        if (mov.size() != mov2.size()) continue;

        int n = mov.size();
        int ok = 1;
        rep(i, 0, n) if (mov[i] != mov2[i]) ok = 0;
        if (ok) {
            int x = lastx + mix;
            int y = lasty + miy;
            reverse(all(history));
            fore(com, history) {
                x += dx[(com + 2) % 4];
                y += dy[(com + 2) % 4];
            }
            printf("! %d %d\n", y, x);
            return;
        }
    }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> H >> W;
    rep(y, 0, H) cin >> B[y];

    record();
    search();
}
accuracy: 1
time_ms: 3860
memory_kb: 18940
final_rank: 7

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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

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

typedef pair<int,int>P;

char direc[]={'U','R','D','L'};

int AX,AY;

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int h, w; cin >> h >> w;
    vector<vector<bool> > g(h + 1, vector<bool> (w + 1, false));
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char input; cin >> input;
            g[i][j] = (input == '#');
        }
    }

    int right = 0,p=0;     //初期位置からどれだけ右に進んだか
    while(true){
        cout << "? R" << endl;p++;
        string res; cin >> res;
        if(res == "YES") right++;
        else break;
    }

    //初期位置からの差分

    set<P>st,ans;
    int sub_i = 0, sub_j = 0;

    int x=504, y=504 , d=2;

    while(1){

        if( st.size() && x==504 && y==504 && d==2 ) break;

        st.insert(P(x,y));
       
        cout<<"? "<<direc[d]<<endl;p++;

        if(p>=77077)return 0;

        string res;
        cin>>res;
        if(res=="YES"){
            x += dx[d] ;
            y += dy[d] ;
            d=(d+3)%4;
        }
        else d=(d+1)%4;
    }


    for(int sx = 1; sx <= w; sx++){
        for(int sy = 1; sy <= h; sy++){

            ans.clear();
            
            if(!g[sy][sx] || g[sy][sx + 1]) continue;

            x = 504;
            y = 504;
            d = 2;
            int cnt=0;
            
            while(true){

                if( ans.size() && x==504 && y==504 && d==2 ){
                    
                    if(st == ans){ 
                        cout << "! " << sy - 1 << " " << sx - 1 - right << endl;
                        return 0;
                    }else break;
                }

                if(!st.count(P(x,y)))break;

                ans.insert(P(x,y)); 
                
                int nx = x + dx[d];
                int ny = y + dy[d];
                
                if(g[ny + sy - 504][nx + sx- 504]){
                    x = nx ;
                    y = ny ; 
                    d=(d+3)%4; 
                }
                else{
                    d=(d+1)%4;
                }
            }       

        }
    }
    return 0;
}
accuracy: 0.9411764705882353
time_ms: 630
memory_kb: 5132
final_rank: 17

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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

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

typedef pair<int,int>P;

char direc[]={'U','R','D','L'};

int AX,AY;

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int h, w; cin >> h >> w;
    vector<vector<bool> > g(h + 1, vector<bool> (w + 1, false));
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char input; cin >> input;
            g[i][j] = (input == '#');
        }
    }

    int right = 0,p=0;     //初期位置からどれだけ右に進んだか
    while(true){
        cout << "? R" << endl;p++;
        string res; cin >> res;
        if(res == "YES") right++;
        else break;
    }

    //初期位置からの差分

    set<P>st,ans;
    int sub_i = 0, sub_j = 0;

    int x=504, y=504 , d=2;

    while(1){

        if( st.size() && x==504 && y==504 && d==2 ) break;

        st.insert(P(x,y));
       
        cout<<"? "<<direc[d]<<endl;p++;

        if(p>=77077)return 0;

        string res;
        cin>>res;
        if(res=="YES"){
            x += dx[d] ;
            y += dy[d] ;
            d=(d+3)%4;
        }
        else d=(d+1)%4;
    }


    for(int sx = 1; sx <= w; sx++){
        for(int sy = 1; sy <= h; sy++){

            ans.clear();
            
            if(!g[sy][sx] || g[sy][sx + 1]) continue;

            x = 504;
            y = 504;
            d = 2;
            int cnt=0;
            
            while(true){

                if( ans.size() && x==504 && y==504 && d==2 ){
                    if(st.size()!=ans.size())break;
                    if(st == ans){ 
                        cout << "! " << sy - 1 << " " << sx - 1 - right << endl;
                        return 0;
                    }else break;
                }

                if(!st.count(P(x,y)))break;

                ans.insert(P(x,y)); 
                
                int nx = x + dx[d];
                int ny = y + dy[d];
                
                if(g[ny + sy - 504][nx + sx- 504]){
                    x = nx ;
                    y = ny ; 
                    d=(d+3)%4; 
                }
                else{
                    d=(d+1)%4;
                }
            }       

        }
    }
    return 0;
}
accuracy: 0.9411764705882353
time_ms: 610
memory_kb: 4980
final_rank: 16

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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

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

typedef pair<int,int>P;

char direc[]={'U','R','D','L'};

bool used[1009][1009][4];
bool check[1009][1009][4];

int main(){

    int h, w; cin >> h >> w;
    vector<vector<bool> > g(h + 1, vector<bool> (w + 1, false));
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char input; cin >> input;
            g[i][j] = (input == '#');
        }
    }

    int right = 0;     //初期位置からどれだけ右に進んだか
    while(true){
        cout << "? R" << endl;
        string res; cin >> res;
        if(res == "YES") right++;
        else break;
    }

    //初期位置からの差分

    set<P>st,ans;
    int sub_i = 0, sub_j = 0;

    int x=504, y=504 , d=2;
    string way;

    while(true){

        if( used[x][y][d] ) break;
        st.insert(P(x,y));
        used[x][y][d]=1;

        //cout << x - 504 << " " << y - 504 << " " << direc[d] << endl;        
        cout<<"? "<<direc[d]<<endl;
        string res;
        cin>>res;
        if(res=="YES"){
            x += dx[d] ;
            y += dy[d] ;

            st.insert(P(x,y));
            d=(d+3)%4;
        }
        else d=(d+1)%4;
    }
    //cout << way << endl;

    for(int sx = 1; sx <= w; sx++){
        for(int sy = 1; sy <= h; sy++){

            ans.clear();
            
            //cout << "sx= " << sx << " " << "sy= " << sy << endl;
            //右が陸ならとばす
            if(!g[sy][sx] || g[sy][sx + 1]) continue;
            //cout << "sx= " << sx << " " << "sy= " << sy << endl;            
            //(sx, sy)から左て法をしていく
            x = 504;
            y = 504;
            d = 2;
            memset(check, false, sizeof(check));
            
            while(true){
                ans.insert(P(x,y));  
                //cout << "x = " << x << " " << "y = " << y << endl;
                //ここが正解
               //cout<<x<<' '<<y<<' '<<d<<' '<<sx<<sy<<endl;
               // cout<<ans.size()<<' '<<st.size()<<endl;
                if( check[x][y][d] ){
                    //cout << tmp << endl;
                    if(st == ans){ 
                        cout << "! " << sy - 1 << " " << sx - 1 - right << endl;
                        return 0;
                    }else break;
                }
                check[x][y][d] = 1;
                
                int nx = x + dx[d];
                int ny = y + dy[d];
                /*cout<<"? "<<direc[d]<<endl;
                string res;
                cin>>res;*/
                //cout << "nx=" << nx - 504 + sx << " ny= " << sy + ny - 504 << endl;
                if(g[ny + sy - 504][nx + sx- 504]){
                    x += dx[d] ;
                    y += dy[d] ;
                    ans.insert(P(x,y));  

                        d=(d-1+4)%4; 
                }
                else{
                    d=(d+1)%4;
                }
            }       

        }
    }
    return 0;
}
accuracy: 0.8235294117647058
time_ms: 2150
memory_kb: 9788
final_rank: 20

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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

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

typedef pair<int,int>P;

char direc[]={'U','R','D','L'};

bool used[1009][1009][4];
bool check[1009][1009][4];

int main(){

    int h, w; cin >> h >> w;
    vector<vector<bool> > g(h + 10, vector<bool> (w + 10, false));
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char input; cin >> input;
            g[i][j] = (input == '#');
        }
    }

    int right = 0;     //初期位置からどれだけ右に進んだか
    while(true){
        cout << "? R" << endl;
        string res; cin >> res;
        if(res == "YES") right++;
        else break;
    }

    //初期位置からの差分

    set<P>st,ans;
    int sub_i = 0, sub_j = 0;

    int x=504, y=504 , d=2;
    string way;

    while(true){

        if( used[x][y][d] ) break;
        st.insert(P(x,y));
        used[x][y][d]=1;

        //cout << x - 504 << " " << y - 504 << " " << direc[d] << endl;        
        cout<<"? "<<direc[d]<<endl;
        string res;
        cin>>res;
        if(res=="YES"){
            x += dx[d] ;
            y += dy[d] ;

            st.insert(P(x,y));
            d=(d+3)%4;
        }
        else d=(d+1)%4;
    }
    //cout << way << endl;

    for(int sx = 1; sx <= w; sx++){
        for(int sy = 1; sy <= h; sy++){

            ans.clear();
            
            //cout << "sx= " << sx << " " << "sy= " << sy << endl;
            //右が陸ならとばす
            if(!g[sy][sx] || g[sy][sx + 1]) continue;
            //cout << "sx= " << sx << " " << "sy= " << sy << endl;            
            //(sx, sy)から左て法をしていく
            x = 504;
            y = 504;
            d = 2;
            memset(check, false, sizeof(check));
            
            while(true){
                ans.insert(P(x,y));  
                //cout << "x = " << x << " " << "y = " << y << endl;
                //ここが正解
               //cout<<x<<' '<<y<<' '<<d<<' '<<sx<<sy<<endl;
               // cout<<ans.size()<<' '<<st.size()<<endl;
                if( check[x][y][d] ){
                    //cout << tmp << endl;
                    if(st == ans){ 
                        cout << "! " << sy - 1 << " " << sx - 1 - right << endl;
                        return 0;
                    }else break;
                }
                check[x][y][d] = 1;
                
                int nx = x + dx[d];
                int ny = y + dy[d];
                /*cout<<"? "<<direc[d]<<endl;
                string res;
                cin>>res;*/
                //cout << "nx=" << nx - 504 + sx << " ny= " << sy + ny - 504 << endl;
                if(g[ny + sy - 504][nx + sx- 504]){
                    x += dx[d] ;
                    y += dy[d] ;
                    ans.insert(P(x,y));  

                        d=(d-1+4)%4; 
                }
                else{
                    d=(d+1)%4;
                }
            }       

        }
    }
    return 0;
}
accuracy: 0.8235294117647058
time_ms: 2130
memory_kb: 9684
final_rank: 19

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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

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

typedef pair<int,int>P;

char direc[]={'U','R','D','L'};

bool used[1009][1009][4];
bool check[1009][1009][4];

int main(){

    int h, w; cin >> h >> w;
    vector<vector<bool> > g(h + 2, vector<bool> (w + 2, false));
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char input; cin >> input;
            g[i][j] = (input == '#');
        }
    }

    int right = 0;     //初期位置からどれだけ右に進んだか
    while(true){
        cout << "? R" << endl;
        string res; cin >> res;
        if(res == "YES") right++;
        else break;
    }

    //初期位置からの差分

    set<P>st,ans;
    int sub_i = 0, sub_j = 0;

    int x=504, y=504 , d=2;
    string way;

    while(true){

        if( used[x][y][d] ) break;
        st.insert(P(x,y));
        used[x][y][d]=1;

        //cout << x - 504 << " " << y - 504 << " " << direc[d] << endl;        
        cout<<"? "<<direc[d]<<endl;
        string res;
        cin>>res;
        if(res=="YES"){
            //cout << direc[d] << endl;
            way = way + direc[d];
            x += dx[d] ;
            y += dy[d] ;
            //used[x][y][d]=1;
            st.insert(P(x,y));
            //cout << x - 504 << " " << y - 504 << " " << direc[d] << endl;        
            d=(d+3)%4;
        }
        else{
            //cout << x - 504 << " " << y - 504 << " " << direc[d] << endl;        
            d=(d+1)%4;
        }
        if(x<0||y<0||x>=1008||y>=1008)return 0;
    }
    //cout << way << endl;

    for(int sx = 1; sx <= w; sx++){
        for(int sy = 1; sy <= h; sy++){

            ans.clear();
            
            //cout << "sx= " << sx << " " << "sy= " << sy << endl;
            //右が陸ならとばす
            if(!g[sy][sx] || g[sy][sx + 1]) continue;
            //cout << "sx= " << sx << " " << "sy= " << sy << endl;            
            //(sx, sy)から左て法をしていく
            x = 504;
            y = 504;
            d = 2;
            memset(check, false, sizeof(check));
            string tmp;//cout<<endl;
            
            while(true){
                ans.insert(P(x,y));  
                //cout << "x = " << x << " " << "y = " << y << endl;
                //ここが正解
               //cout<<x<<' '<<y<<' '<<d<<' '<<sx<<sy<<endl;cout<<ans.size()<<' '<<st.size()<<endl;
                if( check[x][y][d] ){
                    //cout << tmp << endl;
                    if(st == ans){ 
                        cout << "! " << sy - 1 << " " << sx - 1 - right << endl;
                        return 0;
                    }else break;
                }
                check[x][y][d] = 1;
                //島の形が違う
                bool flag = false;
                for(int h = 0; h < 4; h++){
                    flag = flag | used[x][y][h];
                }
                if(!flag) break;
                
                int nx = x + dx[d];
                int ny = y + dy[d];
                if(((y + sy - 504)<0)||((x + sx-504<0)))return 0;
                if(((y + sy - 504)>h)||((x + sx - 504>w)))return 0;
                /*cout<<"? "<<direc[d]<<endl;
                string res;
                cin>>res;*/
                //cout << "nx=" << nx - 504 + sx << " ny= " << sy + ny - 504 << endl;
                if(g[ny + sy - 504][nx + sx- 504]){
                    tmp = tmp + direc[d];
                    x += dx[d] ;
                    y += dy[d] ;
                    ans.insert(P(x,y));  

                        d=(d-1+4)%4; 
                }
                else{
                    d=(d+1)%4;
                }
                if(((y + sy - 504)<0)||((x + sx-504<0)))return 0;
                if(((y + sy - 504)>h)||((x + sx - 504>w)))return 0;
            }       

        }
    }
    return 0;
}
accuracy: 0.9411764705882353
time_ms: 1020
memory_kb: 10356
final_rank: 18