#include #include #include using namespace std; struct friends{ int y, x; string name; }; int H, W, K, P; vector> space; vector> dp; long move(int now_y, int now_x){ if(dp[now_y][now_x] != -1){ return dp[now_y][now_x]; }else if(now_y == H && now_x == W){ return dp[now_y][now_x] = 1; }else if(now_y == H){ return dp[now_y][now_x] = move(now_y, now_x + 1) * space[now_y][now_x + 1]; }else if(now_x == W){ return dp[now_y][now_x] = move(now_y + 1, now_x) * space[now_y + 1][now_x]; }else{ return dp[now_y][now_x] = move(now_y, now_x + 1) * space[now_y][now_x + 1] + move(now_y + 1, now_x) * space[now_y + 1][now_x]; } } int main(){ cin >> H >> W >> K >> P; space.resize(H + 1, vector(W + 1, true)); friends friendss[K]; for(int i = 0; i < K; i++){ cin >> friendss[i].y >> friendss[i].x >> friendss[i].name; } long ans = -1; int ans_number = -1; dp.resize(H + 1, vector(W + 1)); for(int i = 0; i < (1 << K); i++){ if(__builtin_popcount(i) == P){ for(int j = 0; j < K; j++){ if(~(i >> j) & 1){ space[friendss[j].y][friendss[j].x] = false; } } for(int j = 0; j <= H; j++){ for(int k = 0; k <= W; k++){ dp[j][k] = -1; } } long this_ans = move(0, 0); if(this_ans > ans){ ans = this_ans; ans_number = i; } for(int j = 0; j < K; j++){ space[friendss[j].y][friendss[j].x] = true; } } } cout << ans << endl; for(int i = 0; i < K; i++){ if((ans_number >> i) & 1){ cout << friendss[i].name << endl; } } }