Language: C++

#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <bitset>

using namespace std;

int h, w, n;
char map[30][30];
string ans[] = { "NO", "YES" };

int getlen( char tmap[30][30], char ttmap[30][30], char target, int x, int y, int dx, int dy, int depth, bool mode) {
	if( x < 0 || y >= w || x < 0 || y >= h ) return depth;
	if( tmap[y][x] != target ) return depth;
	if( mode == true ) ttmap[y][x] = '.';
	return getlen( tmap, ttmap, target, x + dx, y + dy, dx, dy, depth + 1, mode);
}

//posは左側の座標
bool search(int pos[2]){
	if( pos[1] >= w - 1 ) return false;
	bool ret = true, flg = true;
	char tmap[30][30];
	memcpy( tmap, map, sizeof(map) );

	//入れ替え
	if( tmap[pos[0]][pos[1]] != tmap[pos[0]][pos[1] + 1] ) {
		tmap[pos[0]][pos[1]] = tmap[pos[0]][pos[1]] ^ tmap[pos[0]][pos[1] + 1];
		tmap[pos[0]][pos[1] + 1] = tmap[pos[0]][pos[1]] ^ tmap[pos[0]][pos[1] + 1];
		tmap[pos[0]][pos[1]] = tmap[pos[0]][pos[1]] ^ tmap[pos[0]][pos[1] + 1];
	}
	
	while( flg ) {
		flg = false;
		//落下
		for(int i = 0; i < w; i++) {
			int tmp = 0;
			char ch[30];
			for(int j = 0; j < h; j++) {
				if( tmap[j][i] != '.' ) {
					ch[tmp] = tmap[j][i];
					tmp ++;
				}	
			}
			for(int j = h - 1; j >= 0; j--) {
				if( tmp > 0 ) {
					tmap[j][i] = ch[tmp - 1];
					tmp --;
				} else {
					tmap[j][i] = '.';
				}
			}
		}
		int dis[2][30][30] = {};
		char ttmap[30][30];
		memcpy( ttmap, tmap, sizeof( tmap ) );
		//消滅チェック
		
		for(int j = 0; j < h; j++) {
			for(int i = 0; i < w; i++) {
				if( tmap[j][i] == '.' ) continue;
				dis[1][j][i] = getlen( tmap, tmap, tmap[j][i], i, j, -1, 0, 0, false ) + getlen( tmap, tmap, tmap[j][i], i, j, 1, 0, 0, false );
				dis[0][j][i] = getlen( tmap, tmap, tmap[j][i], i, j, 0, -1, 0, false ) + getlen( tmap, tmap, tmap[j][i], i, j, 0, 1, 0, false );
			}
		}

		//消滅
		for(int i = 0; i < w; i++) {
			for(int j = 0; j < h; j++) {
				if( tmap[j][i] == '.' ) continue;
				if( dis[1][j][i] >= n + 1 ) {
					flg = true;
					getlen( tmap, ttmap, tmap[j][i], i, j, -1, 0, 0, true );
					getlen( tmap, ttmap, tmap[j][i], i, j, 1, 0, 0, true );
				}
				if( dis[0][j][i] >= n + 1) {
					flg = true;
					getlen( tmap, ttmap, tmap[j][i], i, j, 0, -1, 0, true );
					getlen( tmap, ttmap, tmap[j][i], i, j, 0, 1, 0, true );
				}
			}
		}

		memcpy( tmap, ttmap, sizeof( tmap ) );
	}

	//全消しチェック
	for(int i = 0; i < w; i++) {
		for(int j = 0; j < h; j++) {
			if( tmap[j][i] != '.' ) ret = false;
		}
		if( !ret ) break;
	}

	return ret;
}

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);
	
	cin >> h >> w >> n;
	for(int i = 0; i < h; i++)
		cin >> map[i];

	int pos[2] = {0, 0};
	bool flg = false;

	for(pos[0] = 0; pos[0] < h; pos[0] ++) {
		for(pos[1] = 0; pos[1] < w; pos[1] ++) {
			flg |= search( pos );
		}
	}

	cout << ans[flg] << endl;

	//while(1){}
	return 0;
}

Compiler output:
a.cc: In function 'bool search(int*)':
a.cc:25:9: error: 'memcpy' was not declared in this scope
   25 |         memcpy( tmap, map, sizeof(map) );
      |         ^~~~~~
a.cc:6:1: note: 'memcpy' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    5 | #include <bitset>
  +++ |+#include <cstring>
    6 |