Go: ///
// File:  0558.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:58:45 2018
//
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type pos struct {
	x, y int
}

var H, W, N int
var graph [][]string

func wfs(p pos, index int) (int, pos) {
	var q []pos
	dists := make([][]int, H)
	for i := range dists {
		dists[i] = make([]int, W)
	}

	q = append(q, p)
	for len(q) > 0 {
		p := q[0]
		dist := dists[p.x][p.y]
		q = q[1:]

		for _, d := range []pos{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} {
			x := p.x + d.x
			y := p.y + d.y
			if 0 <= x && x < H && 0 <= y && y < W && graph[x][y] != "X" && dists[x][y] == 0 {
				dists[x][y] = dist + 1
				q = append(q, pos{x, y})
			}
		}
	}

	for i := range graph {
		for j := range graph[i] {
			if graph[i][j] == strconv.Itoa(index) {
				return dists[i][j], pos{i, j}
			}
		}
	}

	panic("not found")
}

func main() {
	fmt.Scan(&H, &W, &N)

	var start pos

	graph = make([][]string, H)
	for i := range graph {
		var s string
		fmt.Scan(&s)
		graph[i] = strings.Split(s, "")
		for j := range graph[i] {
			if graph[i][j] == "S" {
				start.x = i
				start.y = j
			}
		}
	}

	count := 0
	for i := 1; i <= N; i++ {
		c, s := wfs(start, i)
		start = s
		count += c
	}

	fmt.Println(count)
}

C++: #include<stdio.h>
#include<utility>
#include<queue>
#define INF 0x7FFF
using namespace std;

void solve()
{
	char mage[1000][1000];
	int dist[1000][1000];
	int x_route[4]={1,0,-1,0};//移動方向をループで回わす利点は、近傍の対応がしやすい
	int y_route[4]={0,1,0,-1};
	int start[2];
	int lastPosi[2];
	int h,w,n,hp=1;//hight wide cheese_num
	typedef pair<int ,int> P;
	queue <P> que;

	scanf("%d %d %d\n",&h,&w,&n);
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		{
			scanf("%c",&mage[h][w]);
			if(mage[h][w]=='S')
			{
				start[1]=h;
				start[0]=w;
			}
		}

	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
			dist[i][j]=INF;

	dist[ start[1] ][ start[0] ]=0;
	que.push(P(start[1],start[0]));
	
	while(que.size())
	{

		P temp=que.front();
		que.pop();
		for(int i=0;i<4;i++)
		{
			int nx=temp.first  + x_route[i];
			int ny=temp.second + y_route[i];
			
			if(nx>=0 && ny<h && ny>=0 && ny<w && mage[nx][ny]!='X' && dist[nx][ny]==INF)
			{
				if(mage[nx][ny]-'0'>=0)
				{
					n--;//喰った工場の数を減らす
					hp+=mage[nx][ny]-'0';
					lastPosi[0]=nx;
					lastPosi[1]=ny;
				}
				que.push(P(nx,ny));
				dist[nx][ny]=dist[temp.first][temp.second]+1;
			}
		}
		if(n<=0)
		{
			printf("%d\n",dist[lastPosi[0]][lastPosi[1]]);
			break;
		}
	}
}


int main()
{
	solve();
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#include<deque>
#include<cstdio>
using namespace std;
typedef long long int ll;
const int N = 1000000;
const int mod = 1000000007;
const int INF = 1 << 30;
#define rep(i,n) for(ll i=(ll)0;i<(ll)n;++i)
#define ALL(x) x.begin(),x.end()
#define pp pair<ll,ll>
#define fi first
#define se second
#define pb push_back
ll ppow(ll x, ll n) {
	ll ans = 1;
	while (n > 0) {
		if ((n & 1) == 1)ans = ans * x;
		x = x * x;
		n >>= 1;
		x %= mod;
		ans %= mod;
	}
	return ans;
}
string YN(bool b) { return(b ? "YES" : "NO"); }
string yn(bool b) { return(b ? "Yes" : "No"); }
ll sz, fact[N], inv[N];
void setinv() {
	fact[0] = 1; inv[0] = 1;
	for (int i = 1; i <= sz; i++) {
		fact[i] = (fact[i - 1] * i) % mod;  //階乗を求める
		inv[i] = ppow(fact[i], (ll)mod - 2) % mod; // フェルマーの小定理で逆元を求める
	}
}
ll conv(ll r) {//nCr
	return fact[sz] * inv[r] % mod * inv[sz - r] % mod;
}
int d[1000][1000];
void reset() {
	rep(i, 1000)rep(j, 1000)d[i][j] = INF;
}
int main() {
	int h, w, n;
	pp s, g, pos[10];
	cin >> h >> w >> n;
	char m[1010][1010];
	rep(i, h)rep(j, w) {
		cin >> m[i][j];
		if (m[i][j] == 'S')pos[0] = pp{ i,j };
		if (m[i][j] == char('0' + n - 1))g = pp{ i,j };
		if (m[i][j] >= '0'&&m[i][j] <= '9')pos[m[i][j] - '0'] = pp{ i,j };
	}
	int ans = 0;
	rep(i, n) {
		reset();
		queue<pp>q; q.push(pos[i]);
		d[pos[i].fi][pos[i].se] = 0;
		while (!q.empty()) {
			pp p = q.front(); q.pop();
			if (p == pos[i + 1]) { ans += d[p.fi][p.se]; break; }
			if (p.fi > 0 && m[p.fi - 1][p.se] != 'X'&&d[p.fi - 1][p.se] == INF) { q.push(pp{ p.fi - 1,p.se }); d[p.fi - 1][p.se] = d[p.fi][p.se] + 1; }
			if (p.fi < h - 1 && m[p.fi + 1][p.se] != 'X'&&d[p.fi + 1][p.se] == INF) { q.push(pp{ p.fi + 1,p.se }); d[p.fi + 1][p.se] = d[p.fi][p.se] + 1; }
			if (p.se > 0 && m[p.fi][p.se - 1] != 'X'&&d[p.fi][p.se - 1] == INF) { q.push(pp{ p.fi,p.se - 1 }); d[p.fi][p.se - 1] = d[p.fi][p.se] + 1; }
			if (p.se < w - 1 && m[p.fi][p.se+1] != 'X'&&d[p.fi][p.se+1] == INF) { q.push(pp{ p.fi,p.se+1 }); d[p.fi][p.se+1] = d[p.fi][p.se] + 1; }
		}
	}
	cout << ans << endl;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<queue>


#define CI cin >>
#define CO cout <<
#define E << endl;
#define F(i,max) (for(int i = 0;i < max;++i))

using namespace std;


const int INF = 100000;
int H;
int W;
int N;
int sx;
int sy;
vector<vector<char>> map;

char temp;

typedef pair<int, int> P;

int factory_x[9];
int factory_y[9];
int size_ = 0;

int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, -1, 0, 1 };

void input() {
	CI H >> W >> N;

	map.resize(H);		
	for (int i = 0; i < H; i++) {
		map[i].resize(W);
	}


	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			CI temp;
			if (temp == 'S') {
				sx = j;
				sy = i;
			}
			else {
				if ((int)(temp) - '0' >= 1 && (int)(temp) - '0' <= 9) {
					factory_x[(int)temp - '0' - 1] = j;
					factory_y[(int)temp - '0' - 1] = i;
					if ((int)temp - '0' > size_) {
						size_ = (int)temp - '0';
					}
				}
			}
			map[i][j] = temp;
		}
	}
}

int bfs() {
	queue<P> que;
	
	int n = 0;
	int distance = 0;
	vector<vector<int>> d(H);
	for (int i = 0; i < H; ++i) {
		d[i].resize(W);
	}

	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			d[i][j] = INF;
		}
	}
	que.push(P(sx, sy));
	d[sy][sx] = 0;
	

	while (!que.empty()) {
		P p;
		p = que.front();
		que.pop();

		if (p.first == factory_x[n] && p.second == factory_y[n]) {
			sx = factory_x[n];
			sy = factory_y[n];

			
			distance += d[p.second][p.first];
			
			
			if (n == size_ - 1) {
				break;
			}
			n++;

			for (int i = 0; i < H; ++i) {
				for (int j = 0; j < W; ++j) {
					d[i][j] = INF;
				}
			}
			d[sy][sx] = 0;
			while (que.size() > 0) {
				que.pop();
			}
			que.push(P(sx, sy));
			
			
			continue;
		}

		for (int i = 0; i < 4; ++i) {
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];

			if (0 <= nx && nx < W && 0 <= ny && ny < H && map[ny][nx] != 'X' && d[ny][nx] == INF) {
				que.push(P(nx, ny));
				d[ny][nx] = d[p.second][p.first] + 1;
			}
		}

	}

	return distance;
}

int main(void) {
	input();

	

	CO bfs() E

	return 0;
}