Language: C++

#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define each(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(),c.end()
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef long long ll;
const int inf=(int)1e9;
const double EPS=1e-9, INF=1e12;

const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
const int inf = (int) 1e9;

int h, w, sy, sx, gy, gx;
string in[100];
set<pair<vi, int> > s;

int p[200], sz[200], size[200], dist[200][200];
int root(int x){ return x == p[x] ? x : (p[x] = root(p[x])); }

int main(){
	
	while(cin >> w >> h, w){
		rep(i, h){
			cin >> in[i];
			rep(j, w){
				if(in[i][j] == 'S') sy = i, sx = j;
				if(in[i][j] == 'G') gy = i, gx = j;
			}
		}
		
		rep(i, 200) p[i] = i, sz[i] = 1;
		rep(i, 200) rep(j, 200) dist[i][j] = inf;
		rep(i, h) rep(j, w) if(in[i][j] != '#'){
			rep(d, 4){
				int y = i + dy[d], x = j + dx[d];
				if(y < 0 || y >= h || x < 0 || x >= w) continue;
				if(in[y][x] != '#'){
					dist[i * w + j][y * w + x] = 1;
				}
			}
		}
		rep(k, 200) rep(i, 200) rep(j, 200)
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
		
		rep(i, h) rep(j, w) if(in[i][j] == 'X'){
			rep(d, 4){
				int y = i + dy[d], x = j + dx[d];
				if(y < 0 || y >= h || x < 0 || x >= w) continue;
				if(in[y][x] == 'X'){
					int a = root(y * w + x), b = root(i * w + j);
					if(a != b) p[b] = a, sz[a] += sz[b];
				}
			}
		}
		map<int, int> of;
		rep(i, h) rep(j, w) if(in[i][j] == 'X'){
			of[root(i * w + j)] = 0;
		}
		int k = 0;
		each(i, of){
			i->second = k++;
			size[i->second] = sz[i->first];
		}
		
		vi v(k);
		s.clear();
		priority_queue<pair<pi, pair<vi, int> > > q;
		
		q.push(mp(mp(-dist[gy * w + gx][sy * w + sx], 0), mp(v, sy * w + sx)));
		
		while(!q.empty()){
			int y = q.top().second.second / w, x = q.top().second.second % w;
			int c = q.top().first.second;
			v = q.top().second.first;
			q.pop();
			
			if(s.count(mp(v, y * w + x))) continue;
			s.insert(mp(v, y * w + x));
			
			if(in[y][x] == 'G'){
				cout << -c << endl;
				break;
			}
			
			rep(d, 4){
				int ny = y + dy[d], nx = x + dx[d];
				if(ny < 0 || ny >= h || nx < 0 || nx >= w || in[ny][nx] == '#') continue;
				vi nv = v;
				if(in[ny][nx] == 'X'){
					int t = of[root(ny * w + nx)];
					nv[t]++;
					if(2 * nv[t] > size[t]) continue;
				}
				q.push(mp(mp(-dist[gy * w + gx][ny * w + nx] + c, c - 1), mp(nv, ny * w + nx)));
			}
		}
	}
}

Compiler output:
a.cc:29:11: error: redefinition of 'const int inf'
   29 | const int inf = (int) 1e9;
      |           ^~~
a.cc:25:11: note: 'const int inf' previously defined here
   25 | const int inf=(int)1e9;
      |           ^~~
a.cc: In function 'int main()':
a.cc:80:25: error: reference to 'size' is ambiguous
   80 |                         size[i->second] = sz[i->first];
      |                         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:35:22: note:                 'int size [200]'
   35 | int p[200], sz[200], size[200], dist[200][200];
      |                      ^~~~
a.cc:110:56: error: reference to 'size' is ambiguous
  110 |                                         if(2 * nv[t] > size[t]) continue;
      |                                                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:35:22: note:                 'int size [200]'
   35 | int p[200], sz[200], size[200], dist[200][200];
      |                      ^~~~