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 <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
//#include <map>
#include <iomanip>
#include <vector>
#include <queue>

using namespace std;

const int INF = 100000000;

typedef pair<int, int> P;	//縦、横

int H, W, N;
char maze[1000][1000];
int sx, sy;
int gx, gy;
int d[1000][1000];		//各点までの最短距離の配列
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int cnt;


int bfs()
{
	queue<P> que;
	
	for (int i=0; i<H; i++)
	{
		for (int j=0; j<W; j++)
		{
			d[i][j] = INF;
		}
	}
	
	que.push(P(sy, sx));
	d[sy][sx] = 0;
	
	while (que.size())
	{
		P p = que.front();
		que.pop();
		if (p.first == gy && p.second == gx) break;
		
		for (int i=0; i<4; i++)
		{
			int nx = p.second + dx[i];
			int ny = p.first + dy[i];
			
			if (0<=nx && nx<W && 0<=ny && ny<H && maze[ny][nx]!='X' && d[ny][nx]==INF)
			{
				que.push(P(ny, nx));
				d[ny][nx] = d[p.first][p.second] + 1;
			}
		}
	}
	
	return d[gy][gx];
}


int main()
{
	//入力
	cin >> H >> W >> N;
	for (int i=0; i<H; i++)
	{
		cin >> maze[i];
	}
	
	//巣と１番目のチーズ工場（ゴール）の設定
	cnt = 0;
	for (int k=1; k<=N; k++)
	{
		for (int i=0; i<H; i++)
		{
			for (int j=0; j<W; j++)
			{
				if (k == 1)
				{
					if (maze[i][j] == 'S')
					{
						sx = j;
						sy = i;
					} else if (maze[i][j] == '1') {
						gx = j;
						gy = i;
					}
				} else {
					if (maze[i][j] == '1' + k - 2)
					{
						sx = j;
						sy = i;
					} else if (maze[i][j] == '1' + k - 1) {
						gx = j;
						gy = i;
					}
				}
			}
		}
		
		cnt += bfs();
	}
	
	cout << cnt << endl;
	
	/*
	cout << "cnt = " << cnt << endl;
	
	//テスト出力
	for (int i=0; i<H; i++)
	{
		cout << maze[i] << endl;
	}
	*/
	
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> p;
int n,m;
int xx[10]={1,0,-1,0},yy[10]={0,1,0,-1};
int x[20],y[20];
char a[1100][1100];
int b[1100][1100];
bool d[1100][1100];
int bfs(int f,int e)
{
	memset(b,0,sizeof(b));
	memset(d,false,sizeof(d));
	queue<p> q;
	q.push(p(x[f],y[f]));
	b[x[f]][y[f]]=0;
	d[x[f]][y[f]]=true;
	while(q.size())
	{
		p k=q.front();
		if(k.first==x[e]&&k.second==y[e])
		{
			return b[x[e]][y[e]];
		}
		q.pop();
		for(int i=0;i<4;i++)
		{
			int nx=k.first+xx[i];
			int ny=k.second+yy[i];
			if(!d[nx][ny]&&nx>=0&&nx<n&&ny>=0&&ny<m&&a[nx][ny]!='X')
			{
				q.push(p(nx,ny));
				b[nx][ny]=b[k.first][k.second]+1;
				d[nx][ny]=true;
			}
		}
	}
}
int main()
{
	int s;
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		getchar();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				scanf("%c",&a[i][j]);
				if(a[i][j]=='S')
				{
					x[0]=i;
					y[0]=j;
					a[i][j]=i;
				}
				else if(a[i][j]>='1'&&a[i][j]<='9')
				{
					x[a[i][j]-'0']=i;
					y[a[i][j]-'0']=j;
					a[i][j]='.';
				}
			}
			getchar();
		}
		ll ss=0;
		for(int i=0;i<s;i++)
		{
			ss+=bfs(i,i+1);
		}
		cout<<ss<<endl;
	}
	return 0;
}