Language: C++

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct S{
  int y,x,ry,rx,d,rd;
};

struct SS{
  int y,x,d;
};

int main(){
  for(int W,H;cin>>W>>H,W;){
    char g[16][64];
    int y,x;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin>>g[i][j];
	if(g[i][j]=='K'){
	  y=i;
	  x=j;
	}
      }
    }
    int dy[]={-1,0,1,0};
    int dx[]={0,1,0,-1};
    int d;
    for(d=0;g[y+dy[d]][x+dx[d]]=='#';d++);
    vector<S> v;
    vector<SS> vs{{y,x,d}};
    for(int i=0;i<4;i++){
      v.push_back({y,x,y,x,d,i});
    }
    static bool p[16][64][16][64][4][4];
    bool ps[16][64][4]={};
    fill(p[0][0][0][0][4],p[16][0][0][0][4],false);
    bool reach=false,ret=false;
    while(!vs.empty()){
      auto cs=vs.back();
      reach|=g[cs.y][cs.x]=='M';
      vs.pop_back();
      if(ps[cs.y][cs.x][cs.d]++)continue;
      int ny,nx;
      for(ny=cs.y,nx=cs.x;g[ny+dy[cs.d]][nx+dx[cs.d]]!='#';ny+=dy[cs.d],nx+=dx[cs.d]);
      for(int i=1;i<=3;i+=2){
	vs.push_back({ny,nx,(cs.d+i)%4});
      }
    }

    while(!v.empty()){
      S cs=v.back();
      v.pop_back();
      if(p[cs.y][cs.x][cs.ry][cs.rx][cs.d][cs.rd]++)continue;
      int ny,nx;
      for(ny=cs.y,nx=cs.x;g[ny+dy[cs.d]][nx+dx[cs.d]]!='#';ny+=dy[cs.d],nx+=dx[cs.d]);

      for(int rny=cs.ry,rnx=cs.rx;g[rny][rnx]!='#';rny+=dy[cs.rd],rnx+=dx[cs.rd]){
	ret|=g[ny][nx]=='M'&&g[rny][rnx]=='M'&&g[rny-dy[cs.rd]][rnx-dx[cs.rd]]!='#';
	for(int i=1;i<=3;i+=2){
	  if(g[rny+dy[(cs.rd+i+2)%4]][rnx+dx[(cs.rd+i+2)%4]]=='#'){
	    v.push_back({ny,nx,rny,rnx,(cs.d+i)%4,(cs.rd+i)%4});
	  }
  for(int W,H;cin>>W>>H,W;){
    char g[16][64];
    int y,x;
    for(int i=0;i<H;
  for(int W,H;cin>>W>>H,W;){
    char g[16][64];
    int y,x;
    for(int i=0;i<H;
  for(int W,H;cin>>W>>H,W;){
    char g[16][64];
    int y,x;
    for(int i=0;i<H;
  for(int W,H;cin>>W>>H,W;){
    char g[16][64];
    int y,x;
    for(int i=0;i<H;
	}
      }
    }
    cout<<(!reach?"He cannot bring tea to his master.":
	   !ret?"He cannot return to the kitchen.":
	   "He can accomplish his mission.")<<endl;
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:45:29: error: use of an operand of type 'bool' in 'operator++' is forbidden in C++17
   45 |       if(ps[cs.y][cs.x][cs.d]++)continue;
      |          ~~~~~~~~~~~~~~~~~~~^
a.cc:56:49: error: use of an operand of type 'bool' in 'operator++' is forbidden in C++17
   56 |       if(p[cs.y][cs.x][cs.ry][cs.rx][cs.d][cs.rd]++)continue;
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
a.cc:70:3: error: expected primary-expression before 'for'
   70 |   for(int W,H;cin>>W>>H,W;){
      |   ^~~
a.cc:69:21: error: expected ')' before 'for'
   69 |     for(int i=0;i<H;
      |        ~            ^
      |                     )
   70 |   for(int W,H;cin>>W>>H,W;){
      |   ~~~                
a.cc:74:3: error: expected primary-expression before 'for'
   74 |   for(int W,H;cin>>W>>H,W;){
      |   ^~~
a.cc:73:21: error: expected ')' before 'for'
   73 |     for(int i=0;i<H;
      |        ~            ^
      |                     )
   74 |   for(int W,H;cin>>W>>H,W;){
      |   ~~~                
a.cc:78:3: error: expected primary-expression before 'for'
   78 |   for(int W,H;cin>>W>>H,W;){
      |   ^~~
a.cc:77:21: error: expected ')' before 'for'
   77 |     for(int i=0;i<H;
      |        ~            ^
      |                     )
   78 |   for(int W,H;cin>>W>>H,W;){
      |   ~~~                
a.cc:82:9: error: expected primary-expression before '}' token
   82 |         }
      |         ^
a.cc:81:21: error: expected ')' before '}' token
   81 |     for(int i=0;i<H;
      |        ~            ^
      |                     )
   82 |         }
      |         ~            
a.cc:82:9: error: expected primary-expression before '}' token
   82 |         }
      |         ^
a.cc:89:2: error: expected '}' at end of input
   89 | }
      |  ^
a.cc:60:82: note: to match this '{'
   60 |       for(int rny=cs.ry,rnx=cs.rx;g[rny][rnx]!='#';rny+=dy[cs.rd],rnx+=dx[cs.rd]){
      |                                                                                  ^
a.cc:89:2: error: expected '}' at end of input
   89 | }
      |  ^
a.cc:53:22: note: to match this '{'
   53 |     while(!v.empty()){
      |                      ^
a.cc:89:2: error: expected '}' at end of input
   89 | }
      |  ^
a.cc:16:28: note: to match this '{'
   16 |   for(int W,H;cin>>W>>H,W;){
      |                            ^
a.cc:89:2: error: expected '}' at end of input
   89 | }
      |  ^
a.cc:15:11: note: to match this '{'
   15 | int main(){
      |           ^