#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>
#include <string>
#include <utility>
using namespace std;
const int dx[] = { 1,0,-1,0,0 };
const int dy[] = { 0,1,0,-1,0 };
const string dir = "ESWNX";

int H, W, T, N;
string maps[22];
vector<tuple<int, int, int>> iwashi;
int iwashiMap[22][22];
int nextIwashiMap[22][22];
int distanceMap[22][22];

const int simulateSize = 5;
int simulate(int t, int px, int py, int moveIndex, int depth, int score, int paralyzed, char* lastMove) {

    // save
    int save_iwashimap[22][22];
    for (int i=0; i<H; i++) for (int j=0; j<W; j++) save_iwashimap[i][j] = iwashiMap[i][j];
    vector<tuple<int, int, int>> save_iwashi;
    for (auto item : iwashi) {
        save_iwashi.push_back(item);
    }

    //simulate start
    char moveName[] = "NESWX";
    char will = moveName[moveIndex];
    // player move
    if (will == 'N') py -= 1;
    if (will == 'S') py += 1;
    if (will == 'E') px += 1;
    if (will == 'W') px -= 1;

    // iwashi move
    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++)     nextIwashiMap[i][j] = 0;
    for (int i = 0; i < H; i++) for (int j; j < W; j++) {
        if (maps[i][j] == '#')  continue;
        if (nextIwashiMap[i][j] > 0) {
            nextIwashiMap[i][j] += iwashiMap[i][j];
            continue;
        }
        queue<pair<int, int>> q;
        for (int y = 0; y < H; y++)     for (int x = 0; x < W; x++) {
            if (maps[y][x] != '#' && iwashiMap[y][x] > 0 && !(y == i && x == j)) {
                distanceMap[y][x] = 0;
                q.push(make_pair(y, x));
            }
            else        distanceMap[y][x] = W * H;
        }
        if (paralyzed == 0) {
            distanceMap[py][px] = 0;
            q.push(make_pair(py, px));
        }
        while (!q.empty()) {
            int x = q.front().second, y = q.front().first;
            q.pop();
            for (int k = 0; k < 4; k++) {
                int nx = x + dx[k];
                int ny = y + dy[k];
                if (maps[ny][nx] == '#')        continue;
                if (distanceMap[y][x] + 1 < distanceMap[ny][nx]) {
                    distanceMap[ny][nx] = distanceMap[y][x] + 1;
                    q.push(make_pair(ny, nx));
                }
            }
        }
        for (int k = 0; k < 5; k++) {
            int ny = i + dy[k];
            int nx = j + dx[k];
            if (maps[ny][nx] == '#')    continue;
            if (distanceMap[i][j] > distanceMap[ny][nx] || k == 4) {
                nextIwashiMap[ny][nx] += iwashiMap[i][j];
                break;
            }
        }
    }

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++)     iwashiMap[i][j] = nextIwashiMap[i][j];

    // iwashi つちからはえてくるんだ
    while ((!iwashi.empty()) && get<0>(iwashi[0]) == t) {
        int x, y, hogeT;
        tie(hogeT, x, y) = iwashi[0];
        iwashiMap[y][x]++;
        iwashi.erase(iwashi.begin());
    }
    // scoreing
    if (paralyzed > 0) {
        paralyzed--;
    }
    else if (0 < iwashiMap[py][px] && iwashiMap[py][px] <= 5) {
        score += iwashiMap[py][px];
        iwashiMap[py][px] = 0;
    }
    else if (iwashiMap[py][px] > 5) {
        paralyzed += 5;
        iwashiMap[py][px] = 0;
    }

    // time update
    t++;

    // simulate end
    // next move

    if (depth < simulateSize) {
        //search

        int movable = 0b0000; // NESW
        if (maps[py][px] == '#') cerr << "stuck in wall" << endl;
        if (maps[py][px+1] != '#') movable |= 0b0100;
        if (maps[py][px-1] != '#') movable |= 0b0001;
        if (maps[py+1][px] != '#') movable |= 0b0010;
        if (maps[py-1][px] != '#') movable |= 0b1000;

        int subscore[5] = {-1,-1,-1,-1,0}; //NESWX

        if (movable & 0b1000) { //N
            subscore[0] = simulate(t, px, py, 0, depth+1, score, paralyzed, lastMove);
        }
        if (movable & 0b0100) { //E
            subscore[1] = simulate(t, px, py, 1, depth+1, score, paralyzed, lastMove);
        }
        if (movable & 0b0010) { //S
            subscore[2] = simulate(t, px, py, 2, depth+1, score, paralyzed, lastMove);
        }
        if (movable & 0b0001) { //W
            subscore[3] = simulate(t, px, py, 3, depth+1, score, paralyzed, lastMove);
        }
        subscore[4] = simulate(t, px, py, 4, depth+1, score, paralyzed, lastMove);

        int max_subscore = -1;
        int max_index = -1;
        for (int i=0; i<5; i++) {
            if (max_subscore < subscore[i]) {
                max_index = i;
                max_subscore = subscore[i];
            }
        }
        // decide
        char nextwill = moveName[max_index];
        score += max_subscore;
        *lastMove = nextwill;
    }
    else {
        // no search
    }

    // restore
    for (int i=0; i<H; i++) for (int j=0; j<W; j++) iwashiMap[i][j] = save_iwashimap[i][j];
    iwashi.clear();
    for (auto item : save_iwashi) {
        iwashi.push_back(item);
    }
    return score;

}

int main() {
    cin >> H >> W >> T >> N;
    int px, py; cin >> px >> py;
    px--;       py--;
    for (int i = 0; i < H; i++) cin >> maps[i];
    for (int i = 0; i < N; i++) {
        int x, y, t;    cin >> x >> y >> t;
        y--;    x--;
        iwashi.push_back(make_tuple(t, x, y));
    }
    sort(iwashi.begin(), iwashi.end());
    while (get<0>(iwashi[0]) == 0) {
        int x, y, t;
        tie(t, x, y) = iwashi[0];
        iwashiMap[y][x]++;
        iwashi.erase(iwashi.begin());
    }
    string ret = "";
    int score = 0;
    int paralyzed = 0;

    char will;
    for (int t = 1; t < T; t++) {
        // player move(ここを実装してください)
        //search

        int movable = 0b0000; // NESW
        if (maps[py][px] == '#') cerr << "stuck in wall" << endl;
        if (maps[py][px+1] != '#') movable |= 0b0100;
        if (maps[py][px-1] != '#') movable |= 0b0001;
        if (maps[py+1][px] != '#') movable |= 0b0010;
        if (maps[py-1][px] != '#') movable |= 0b1000;

        int subscore[5] = {-1,-1,-1,-1,0}; //NESWX

        char lastMove;
        if (movable & 0b1000) { //N
            subscore[0] = simulate(t, px, py, 0, 1, score, paralyzed, &lastMove);
        }
        if (movable & 0b0100) { //E
            subscore[1] = simulate(t, px, py, 1, 1, score, paralyzed, &lastMove);
        }
        if (movable & 0b0010) { //S
            subscore[2] = simulate(t, px, py, 2, 1, score, paralyzed, &lastMove);
        }
        if (movable & 0b0001) { //W
            subscore[3] = simulate(t, px, py, 3, 1, score, paralyzed, &lastMove);
        }
        subscore[4] = simulate(t, px, py, 4, 1, score, paralyzed, &lastMove);

        int max_subscore = -1;
        int max_index = -1;
        for (int i=0; i<5; i++) {
            if (max_subscore < subscore[i]) {
                max_index = i;
                max_subscore = subscore[i];
            }
        }
        // decide
        char moveName[] = "NESWX";
        char will = moveName[max_index];

        // player move
        if (will == 'N') py -= 1;
        if (will == 'S') py += 1;
        if (will == 'E') px += 1;
        if (will == 'W') px -= 1;
        ret += will;


        // iwashi move
        for (int i = 0; i < H; i++)     for (int j = 0; j < W; j++)     nextIwashiMap[i][j] = 0;
        for (int i = 0; i < H; i++)     for (int j; j < W; j++) {
            if (maps[i][j] == '#')      continue;
            if (nextIwashiMap[i][j] > 0) {
                nextIwashiMap[i][j] += iwashiMap[i][j];
                continue;
            }
            queue<pair<int, int>> q;
            for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) {
                if (maps[y][x] != '#' && iwashiMap[y][x] > 0 && !(y == i && x == j)) {
                    distanceMap[y][x] = 0;
                    q.push(make_pair(y, x));
                }
                else    distanceMap[y][x] = W * H;
            }
            if (paralyzed == 0) {
                distanceMap[py][px] = 0;
                q.push(make_pair(py, px));
            }
            while (!q.empty()) {
                int x = q.front().second, y = q.front().first;
                q.pop();
                for (int k = 0; k < 4; k++) {
                    int nx = x + dx[k];
                    int ny = y + dy[k];
                    if (maps[ny][nx] == '#')    continue;
                    if (distanceMap[y][x] + 1 < distanceMap[ny][nx]) {
                        distanceMap[ny][nx] = distanceMap[y][x] + 1;
                        q.push(make_pair(ny, nx));
                    }
                }
            }
            for (int k = 0; k < 5; k++) {
                int ny = i + dy[k];
                int nx = j + dx[k];
                if (maps[ny][nx] == '#')        continue;
                if (distanceMap[i][j] > distanceMap[ny][nx] || k == 4) {
                    nextIwashiMap[ny][nx] += iwashiMap[i][j];
                    break;
                }
            }
        }
        for (int i = 0; i < H; i++)     for (int j = 0; j < W; j++)     iwashiMap[i][j] = nextIwashiMap[i][j];

        // iwashi つちからはえてくるんだ
        while ((!iwashi.empty()) && get<0>(iwashi[0]) == t) {
            int x, y, hogeT;
            tie(hogeT, x, y) = iwashi[0];
            iwashiMap[y][x]++;
            iwashi.erase(iwashi.begin());
        }
        // scoreing
        if (paralyzed > 0) {
            paralyzed--;
        }
        else if (0 < iwashiMap[py][px] && iwashiMap[py][px] <= 5) {
            score += iwashiMap[py][px];
            iwashiMap[py][px] = 0;
        }
        else if (iwashiMap[py][px] > 5) {
            paralyzed += 5;
            iwashiMap[py][px] = 0;
        }
    }
    cout << ret << endl;
    return 0;
}

Battle History

ConfigScoreDate
10 x 10 tiny0.762018/11/24 17:15:26
20 x 20 little0.2642018/11/24 17:15:26
20 x 20 little0.7682018/11/24 17:15:26
20 x 20 little0.4082018/11/24 17:15:26
20 x 20 little0.52018/11/24 17:15:26
20 x 20 little0.3762018/11/24 17:15:26
20 x 20 little0.6882018/11/24 17:15:26
20 x 20 much0.078333333332018/11/24 17:15:26
20 x 20 much0.088666666672018/11/24 17:15:26
20 x 20 much0.082018/11/24 17:15:26
20 x 20 much0.073333333332018/11/24 17:15:26
20 x 20 much0.070333333332018/11/24 17:15:26
20 x 20 much0.067333333332018/11/24 17:15:26
challenge0.1792018/11/24 17:15:26
challenge0.1872018/11/24 17:15:26
challenge0.1672018/11/24 17:15:26