Language: C++

#include <iostream>
#include <string>
#include <queue>
#include <utility>
#include <cstdlib>

using namespace std;
struct Node {
        int step, s, x, y;
};
struct NodeComparator {
        bool operator()(const Node &a, const Node &b) const {
                return a.step > b.step;
        }
};

int memo[2][64][34];
int field[64][34];
int dx[] = {  0,  0,  1, 0, 1, 2, 0, 1, 0 };
int dy[] = { -2, -1, -1, 0, 0, 0, 1, 1, 2 };
int dcount = 9;

int main(){
        while(true){
                int w, h;
                cin >> w >> h;
                if(w == 0 || h == 0){ break; }
                memset(field, -1, sizeof(field));
                memset(memo, -1, sizeof(memo));
                priority_queue<Node, vector<Node>, NodeComparator> q;
                for(int i = 0; i < h; ++i){
                        for(int j = 0; j < w; ++j){
                                string panel;
                                cin >> panel;
                                if(panel == "X"){
                                }else if(panel == "S"){
                                        Node n = { 0, 0, j + 2, i + 2 };
                                        q.push(n);
                                        n.s = 1;
                                        q.push(n);
                                        memo[0][i + 2][j + 2] = memo[1][i + 2][j + 2] = 0;
                                }else if(panel == "T"){
                                        field[i + 2][j + 2] = -2;
                                }else{
                                        field[i + 2][j + 2] = atoi(panel.c_str());
                                }
                        }
                }
                int answer = -1;
                while(!q.empty() && answer < 0){
                        Node n = q.top(); q.pop();
                        if(n.step <= memo[n.s][n.y][n.x]){
                                int bx = n.x, by = n.y, sign;
                                if(n.s){
                                        --bx; sign = -1;
                                }else{
                                        ++bx; sign = 1;
                                }
                                for(int i = 0; i < dcount; ++i){
                                        Node next = {
                                                n.step, 1 - n.s,
                                                bx + dx[i] * sign, by + dy[i]
                                        };
                                        if(field[next.y][next.x] == -1){
                                                continue;
                                        }else if(field[next.y][next.x] == -2){
                                                answer = next.step;
                                                break;
                                        }
                                        next.step += field[next.y][next.x];
                                        if(
                                                memo[next.s][next.y][next.x] == -1 ||
                                                memo[next.s][next.y][next.x] > next.step
                                        ){
                                                memo[next.s][next.y][next.x] = next.step;
                                                q.push(next);
                                        }
                                }
                        }
                }
                cout << answer << endl;
        }
        return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:28:17: error: 'memset' was not declared in this scope
   28 |                 memset(field, -1, sizeof(field));
      |                 ^~~~~~
a.cc:6:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    5 | #include <cstdlib>
  +++ |+#include <cstring>
    6 |