Language: C++

#include<iostream>
#include<map>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<sstream>
#include<queue>
#include<cassert>
#include<deque>
#include<set>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define all(n) (n).begin(),(n).end()
#define MAX 12

using namespace std;

struct Pox
{
  vector<int> array;
  int cost;
  Pox(vector<int> array=vector<int>(),int cost=inf):array(array),cost(cost){}
  bool operator < (const Pox &a)const
  {
    return cost > a.cost;
  }
};

int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
//int dx[] = {0,1,0,-1};
//int dy[] = {1,0,-1,0};
//int ev[MAX*MAX][MAX*MAX];//評価値 ev[a][b] := aからbまでの最短コスト
char g[MAX][MAX];
int st[2],h,w,ice_number,T;
int ice[MAX*MAX];
int ice_cnt[MAX*MAX];
set<vector<int> > mincost;

void init()
{
  ice_number = 0;
  rep(i,h)rep(j,w)ice[j+i*w] = ice_cnt[j+i*w] = inf;
  //rep(i,h*w)rep(j,h*w)ev[i][j] = inf;
  mincost.clear();
}

void getInput()
{
  rep(i,h)
    {
      getchar();
      rep(j,w)
	{
	  scanf("%c",&g[i][j]);
	  if(g[i][j] == 'S')
	    {
	      st[1] = j + i * w;
	      g[i][j] = '.';
	    }
	  if(g[i][j] == 'G')
	    {
	      st[0] = j + i * w;
	      g[i][j] = '.';
	    }
	}
    }
}

void make_ev()
{
  rep(sp,h*w)
    {
      ev[sp][sp] = 0;
      deque<int> deq;
      deq.push_back(sp);

      while(!deq.empty())
	{
	  int cur = deq.front(); deq.pop_front();
	  rep(i,4)
	    {
	      int nx = cur % w + dx[i];
	      int ny = cur / w + dy[i];
	      if(!(0 <= nx && nx < w && 0 <= ny && ny < h))continue;
	      if(g[ny][nx] == '#')continue;
	      if(ev[sp][nx+ny*w] > ev[sp][cur]+1)
		{
		  ev[sp][nx+ny*w] = ev[sp][cur]+1;
		  deq.push_back(nx+ny*w);
		}
	    }
	}
    }
}

void draw(int cur,int id,int &cnt)
{
  if(ice[cur] != inf)return;
  ice[cur] = id;
  cnt++;
  rep(i,4)
    {
      int nx = cur % w + dx[i];
      int ny = cur / w + dy[i];
      if(!(0 <= nx && nx < w && 0 <= ny && ny < h))continue;
      if(g[ny][nx] != 'X')continue;
      draw(nx+ny*w,id,cnt);
    }
}

void compute()
{

  vector<int> initial_array(ice_number+1,0);
  initial_array[ice_number] = st[0];
  priority_queue<Pox> Q;
  Q.push(Pox(initial_array,0));
  mincost.insert(initial_array);

  int cnt = 0;

  while(!Q.empty())
    {
      Pox pox = Q.top(); Q.pop();
      cnt++;
      //if(cnt > 10 && !Q.empty() && (cnt%60 == 0 || cnt %120 == 0 || cnt % 73 == 0))Q.pop();
      int cost = pox.cost + ev[pox.array[ice_number]][st[1]];
      //if(cost >= ev[st[0]][st[1]]*5)continue;

      if( cnt % 2 == 0 && !Q.empty())
	{
	  Pox pre = pox;
	  pox = Q.top(); Q.pop();
	  Q.push(pre);
	}

      if(cnt >= 100 && cnt % 3 == 0 && Q.size() >= 2)
	{
	  Pox store = pox;
	  Pox store2 = Q.top(); Q.pop();
	  pox = Q.top(); Q.pop();
	  Q.push(store);
	  Q.push(store2);
	}
      /*
      if(cnt >= 100 && cnt % 7 == 0 && Q.size() >= 5)
	{
	  Pox store[3];
	  store[0] = pox;
	  rep(i,2)
	    {
	      store[i+1] = Q.top(); Q.pop();
	    }
	  pox = Q.top(); Q.pop();
	  rep(i,3)Q.push(store[i]);
	}
      */
      /*
      cout << "pox:array:\n";
      rep(i,pox.array.size())
	cout << "array[" << i << "] = " << pox.array[i] << endl;
      cout << "pox:cost:" << pox.cost << endl << endl;
      */
      if(st[1] == pox.array[ice_number])
	{
	  cout << pox.cost << endl;
	  return;
	}

      rep(i,4)
	{
	  int nx = pox.array[ice_number] % w + dx[i];
	  int ny = pox.array[ice_number] / w + dy[i];
	  if(!(0 <= nx && nx < w && 0 <= ny && ny < h))continue;
	  if(g[ny][nx] == '#')continue;

	  vector<int> next = pox.array;	
	  next[ice_number] = nx+ny*w;  
	  if(g[ny][nx] == 'X')
	    {
	      if(ice_cnt[ice[nx+ny*w]] < 2*(pox.array[ice[nx+ny*w]] + 1))continue;
	      next[ice[nx+ny*w]]++;
	    }

	  if(mincost.find(next) == mincost.end())
	    {
	      mincost.insert(next);
	      Q.push(Pox(next,pox.cost+1));
	    }
	}
    }
  assert(false);
}

int main()
{
  while(scanf("%d %d",&w,&h),w|h)
    {
      init();
      getInput();
      //make_ev();

      rep(y,h)rep(x,w)
	if(g[y][x] == 'X' && ice[x+y*w] == inf)
	    {
	      int cnt = 0;
	      draw(x+y*w,ice_number,cnt);
	      ice_cnt[ice_number] = cnt;
	      ice_number++;      
	    }

      compute();

    }
  return 0;
}

Compiler output:
a.cc: In function 'void make_ev()':
a.cc:76:7: error: 'ev' was not declared in this scope
   76 |       ev[sp][sp] = 0;
      |       ^~
a.cc: In function 'void compute()':
a.cc:130:29: error: 'ev' was not declared in this scope
  130 |       int cost = pox.cost + ev[pox.array[ice_number]][st[1]];
      |                             ^~