1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <iostream> #include <queue> #include <list> #include <stack> #define pr pair<ll, ll> #define ll long long #define str string using namespace std; int main() { cin.sync_with_stdio(false); cin.tie(0); ll H,W; cin >> H >> W; vector<str> v(H); str target = "snuke"; for(int i = 0;i < H;i++ ) cin >> v[i]; auto flg = false; int dirs[8][2] = { 0,1,1,0,-1,0,0,-1, 1,1,-1,-1,1,-1,-1,1 }; stack<pair<ll,ll>> res; std::function<bool(int,int,int,int)> dfs = [&](int x, int y, int now, int i) { if(now==4){ res.push({x,y}); flg = true; return true; } auto nx = x + dirs[i][0], ny = y + dirs[i][1]; if(nx >= 0 && ny >= 0 && nx < W && ny < H && v[ny][nx] == target[now+1]) { if(dfs(nx,ny,now+1,i)){ res.push({x,y}); return true; } } return false; }; for(int i = 0;i < H;i++) { for (int j = 0;j < W;j++) { if (v[i][j]=='s') for (int k = 0;k < 8;k++) { dfs(j,i,0,k); if (flg) break; } if (flg) break; } if (flg) break; } if(flg) { while(!res.empty()) { auto now = res.top(); cout << now.second + 1 << " " << now.first + 1 << "\n"; res.pop(); } } }
|