Language: C++

#include<stdio.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<set>
#include<limits>
#include <complex>
using namespace std;

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	Mod(int n) : num((n % mod + mod) % mod) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	assert(b.num != 0);
	return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
	assert(b.num != 0);
	return a = a * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
//
//
//int move(string st, int& now, int way) {
//	if (now!=st.size()&&mp[st[now]] == way) {
//		now++;
//		return now;
//	}
//	else if (now != 0&& mp[st[now-1]] == (way + 2) % 4) {
//		now--;
//		return now;
//	}
//	return now;
//}

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

long long int hash_[1000011], pmo_[1000011];
long long int hash2_[1000011], pmo2_[1000011];

long long int rhash_[1000011], rpmo_[1000011];
long long int rhash2_[1000011], rpmo2_[1000011];

struct RollingHash {
	static const long long int mul0 = 10009, mul1 = 10007;
	static const long long int add0 = 1000010007, add1 = 1003333331;
	string s; int l;
	void init(string s) {
		this->s = s; l = s.size();
		hash_[0] = 0; pmo_[0] = 1;
		hash2_[0] = 0; pmo2_[0] = 1;
		for (int i = 0; i < l; ++i) {
			pmo_[i + 1] = pmo_[i] * mul0;
			pmo2_[i + 1] = pmo2_[i] * mul1;
		}
		for (int i = 0; i < l; ++i) {
			hash_[i + 1] = (hash_[i] * mul0 + s[i]);
			hash2_[i + 1] = (hash2_[i] * mul1 + s[i]);
		}
	}
	pair<long long int, long long int >  hash(int l, int r) { // s[l..r]
		return make_pair(hash_[r + 1] - hash_[l] * pmo_[r + 1 - l],
			hash2_[r + 1] - hash2_[l] * pmo2_[r + 1 - l]);

	}
};
struct RollingHash2 {
	static const long long int mul0 = 10009, mul1 = 10007;
	static const long long int add0 = 1000010007, add1 = 1003333331;
	string s; int l;
	void init(string s) {
		this->s = s; l = s.size();
		rhash_[0] = 0; rpmo_[0] = 1;
		rhash2_[0] = 0; rpmo2_[0] = 1;
		for (int i = 0; i < l; ++i) {
			rpmo_[i + 1] = rpmo_[i] * mul0;
			rpmo2_[i + 1] = rpmo2_[i] * mul1;
		}
		for (int i = 0; i < l; ++i) {
			rhash_[i + 1] = (rhash_[i] * mul0 + s[i]);
			rhash2_[i + 1] = (rhash2_[i] * mul1 + s[i]);
		}
	}
	pair<long long int, long long int >  hash(int l, int r) { // s[l..r]
		return make_pair(rhash_[r + 1] - rhash_[l] * rpmo_[r + 1 - l],
			rhash2_[r + 1] - rhash2_[l] * rpmo2_[r + 1 - l]);

	}
};

vector<pair<int, int>> ps;
void wr(int a, int b) {
	//cout << a+1 << " " << b+1 << endl;
	ps.push_back({ a + 1,b + 1 });
}
//5 15~

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

int memo[16][64][16][64][4];


vector <vector<bool>>mp;

struct aa {
	int fx;
	int fy;
	int tx;
	int ty;
	int way;
};

vector<aa>stack;

bool cangogoal;

bool check(int fx, int fy, int tx, int ty, int way) {
	memo[fx][fy][tx][ty][way]=1;
	if (tx == gx&&ty == gy) {
		cangogoal = true;
	}else if (fx == gx&&fy == gy&&tx == gx&&ty == gy) {
		return true;
	}
	else{

		int nfy = fy + dy[way];
		int nfx = fx + dx[way];
		while (1) {
			
			if (dp[nfy][nfx]) {
				break;
			}
			nfy = nfy + dy[way];
			nfx = nfx + dx[way];

		}

		int nty = ty;
		int ntx = tx;
		while (1) {

			if (dp[nty + dy[tw + 1]][ntx + dx[tw + 1]]) {
				int away = (tw + 3) % 4;
				stack.push_back(aa{ nfx,nfy,ntx,nty,away });
			}

			if (dp[nty + dy[tw + 3]][ntx + dx[tw + 3]]) {
				int away = (tw + 1) % 4;
				stack.push_back(aa{ nfx,nfy,ntx,nty,away });
			}

			if (dp[nty][ntx]) {
				break;
			}
			nty = nty + dy[way];
			ntx = ntx + dx[way];
		}
		if (dp[nty + dy[tw + 1]][ntx + dx[tw + 1]]) {
			int away = (tw + 3) % 4;
			if (!memo[nfx][nfy][ntx][nty][away])
				stack.push_back(aa{ nfx,nfy,ntx,nty,away });
		}

		if (dp[nty + dy[tw + 3]][ntx + dx[tw + 3]]) {
			int away = (tw + 1) % 4;

			if (!memo[nfx][nfy][ntx][nty][away])
				stack.push_back(aa{ nfx,nfy,ntx,nty,away });
		}

	}
	return false;
}

int main() {
	while (1) {
		cangogoal = false;
		memset(dp, 0, sizeof(dp));
		int W, H; cin >> W >> H;
		vector<string>stmp;
		for (int i = 0; i < H; ++i) {
			string st; cin >> st;
			stmp.push_back(st);
		}

		mp.resize(H,vector<bool>(false));

		int sx, sy, gx, gy;

		for (int i = 0; i < W; ++i) {
			for (int j = 0; j < H; ++j) {
				if (stmp[j][i] == '.') {
					mp[j][i] = 0;
				}
				else if (stmp[j][i] == '#') {
					mp[j][i] = 1;
				}
				else if (stmp[j][i] == 'K') {
					sx = i; sy = j;
					mp[j][i] = 0;
				}
				else {
					gx = i; gy = j;
					mp[j][i] = 0;
				}
			}
		}
		int sway;
		for (int i = 0; i < 4; ++i) {
			if (mp[sy + dy[i]][sx + dx[i]] == 0) {
				sway = i;
			}
		}
		
		stack.push_back(aa{ sx,sy,sway,sx,sy,sway });
		bool isok = false;
		while (!stack.empty()) {
			aa atop(stack.back());
			stack.pop_back();
			if (check(atop)) {
				isok = true;
				break;
			}
		}
		if (isok) {
			cout << "He can accomplish his mission." << endl;
		}
		else if (cangogoal) {
			cout << "He cannot return to the kitchen." << endl;

		}
		else {
			cout << "He cannot bring tea to his master." << endl;
		}
	}

	return 0;
}

Compiler output:
a.cc:158:5: error: conflicting declaration 'int dx [7]'
  158 | int dx[7] = { -1,0,1,0,-1,0,1 };
      |     ^~
a.cc:95:5: note: previous declaration as 'int dx [4]'
   95 | int dx[4] = { -1,0,1,0 };
      |     ^~
a.cc:159:5: error: conflicting declaration 'int dy [7]'
  159 | int dy[7] = { 0,1,0,-1,0,1,0 };
      |     ^~
a.cc:96:5: note: previous declaration as 'int dy [4]'
   96 | int dy[4] = { 0,1,0,-1 };
      |     ^~
a.cc: In function 'bool check(int, int, int, int, int)':
a.cc:180:19: error: 'gx' was not declared in this scope; did you mean 'fx'?
  180 |         if (tx == gx&&ty == gy) {
      |                   ^~
      |                   fx
a.cc:180:29: error: 'gy' was not declared in this scope; did you mean 'fy'?
  180 |         if (tx == gx&&ty == gy) {
      |                             ^~
      |                             fy
a.cc:191:29: error: 'dp' was not declared in this scope; did you mean 'mp'?
  191 |                         if (dp[nfy][nfx]) {
      |                             ^~
      |                             mp
a.cc:203:29: error: 'dp' was not declared in this scope; did you mean 'mp'?
  203 |                         if (dp[nty + dy[tw + 1]][ntx + dx[tw + 1]]) {
      |                             ^~
      |                             mp
a.cc:203:41: error: 'tw' was not declared in this scope; did you mean 'tx'?
  203 |                         if (dp[nty + dy[tw + 1]][ntx + dx[tw + 1]]) {
      |                                         ^~
      |                                         tx
a.cc:205:33: error: reference to 'stack' is ambiguous
  205 |                                 stack.push_back(aa{ nfx,nfy,ntx,nty,away });
      |                                 ^~~~~
In file included from /usr/include/c++/14/stack:63,
                 from a.cc:18:
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:208:29: error: 'dp' was not declared in this scope; did you mean 'mp'?
  208 |                         if (dp[nty + dy[tw + 3]][ntx + dx[tw + 3]]) {
      |                             ^~
      |                             mp
a.cc:208:41: error: 'tw' was not declared in this scope; did you mean 'tx'?
  208 |                         if (dp[nty + dy[tw + 3]][ntx + dx[tw + 3]]) {
      |                                         ^~
      |                                         tx
a.cc:210:33: error: reference to 'stack' is ambiguous
  210 |                                 stack.push_back(aa{ nfx,nfy,ntx,nty,away });
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:213:29: error: 'dp' was not declared in this scope; did you mean 'mp'?
  213 |                         if (dp[nty][ntx]) {
      |                             ^~
      |                             mp
a.cc:219:21: error: 'dp' was not declared in this scope; did you mean 'mp'?
  219 |                 if (dp[nty + dy[tw + 1]][ntx + dx[tw + 1]]) {
      |                     ^~
      |                     mp
a.cc:219:33: error: 'tw' was not declared in this scope; did you mean 'tx'?
  219 |                 if (dp[nty + dy[tw + 1]][ntx + dx[tw + 1]]) {
      |                                 ^~
      |                                 tx
a.cc:222:33: error: reference to 'stack' is ambiguous
  222 |                                 stack.push_back(aa{ nfx,nfy,ntx,nty,away });
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:225:21: error: 'dp' was not declared in this scope; did you mean 'mp'?
  225 |                 if (dp[nty + dy[tw + 3]][ntx + dx[tw + 3]]) {
      |                     ^~
      |                     mp
a.cc:225:33: error: 'tw' was not declared in this scope; did you mean 'tx'?
  225 |                 if (dp[nty + dy[tw + 3]][ntx + dx[tw + 3]]) {
      |                                 ^~
      |                                 tx
a.cc:229:33: error: reference to 'stack' is ambiguous
  229 |                                 stack.push_back(aa{ nfx,nfy,ntx,nty,away });
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc: In function 'int main()':
a.cc:239:24: error: 'dp' was not declared in this scope; did you mean 'mp'?
  239 |                 memset(dp, 0, sizeof(dp));
      |                        ^~
      |                        mp
a.cc:276:17: error: reference to 'stack' is ambiguous
  276 |                 stack.push_back(aa{ sx,sy,sway,sx,sy,sway });
      |                 ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:276:59: error: too many initializers for 'aa'
  276 |                 stack.push_back(aa{ sx,sy,sway,sx,sy,sway });
      |                                                           ^
a.cc:278:25: error: reference to 'stack' is ambiguous
  278 |                 while (!stack.empty()) {
      |                         ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:279:33: error: reference to 'stack' is ambiguous
  279 |                         aa atop(stack.back());
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:280:25: error: reference to 'stack' is ambiguous
  280 |                         stack.pop_back();
      |                         ^~~~~
/usr/include/c++/14/bits/stl_stack.h:99:11: note: candidates are: 'template<class _Tp, class _Sequence> class std::stack'
   99 |     class stack
      |           ^~~~~
a.cc:174:11: note:                 'std::vector<aa> stack'
  174 | vector<aa>stack;
      |           ^~~~~
a.cc:281:35: error: cannot convert 'aa' to 'int'
  281 |                         if (check(atop)) {
      |                                   ^~~~
      |                                   |
      |                                   aa
a.cc:178:16: note:   initializing argument 1 of 'bool check(int, int, int, int, int)'
  178 | bool check(int fx, int fy, int tx, int ty, int way) {
      |            ~~~~^~