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 <bits/stdc++.h>
using namespace std;

struct P{
    int x, y, dist;
    P(){}
    ~P(){}
    P(const P& r) : x(r.x), y(r.y), dist(r.dist) {}
    P(int a, int b, int d) : x(a), y(b), dist(d) {}
};

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


int H, W, N;
char field[1000][1010], cpy[1000][1010];

queue<P> que;

int main(){
    while(scanf("%d%d%d", &H, &W, &N) != EOF){
        for(int x = 0; x < H; ++x){
            scanf(" %s", field[x]);
        }
        P loc[10];
        for(int x = 0; x < H; ++x){
            for(int y = 0; y < W; ++y){
                if(field[x][y] == 'S'){
                    loc[0] = P(x, y, 0);
                }
                if('1' <= field[x][y] && field[x][y] <= '9'){
                    loc[field[x][y]-'0'] = P(x, y, 0);
                }
            }
        }
        int ans = 0;
        for(int n = 0; n < N; ++n){
            for(int x = 0; x < H; ++x){
                for(int y = 0; y < W; ++y){
                    cpy[x][y] = field[x][y];
                }
            }
            while(!que.empty()) que.pop();
            que.push(loc[n]);
            cpy[loc[n].x][loc[n].y] = 'X';
            while(!que.empty()){
                P p = que.front();
                que.pop();
                if(p.x == loc[n+1].x && p.y == loc[n+1].y){
                    ans += p.dist;
                    break;
                }
                for(int i = 0; i < 4; ++i){
                    int nx = p.x + dx[i], ny = p.y + dy[i];
                    if(nx < 0 || H <= nx) continue;
                    if(ny < 0 || W <= ny) continue;
                    if(cpy[nx][ny] == 'X') continue;
                    que.push(P(nx, ny, p.dist+1));
                    cpy[nx][ny] = 'X';
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char f[1010][1010];
int d[1010][1010];
P pos[20];
int H,W,N;
int bfs(int n)
{
	for(int i=0;i<1010;i++)
	{
		for(int j=0;j<1010;j++)
		{
			d[i][j]=INF;
		}
	}
	queue<P> q;
	P s;
	s.fi=pos[n].fi;
	s.sec=pos[n].sec;
	d[s.fi][s.sec]=0;
	q.push(s);
	while(!q.empty())
	{
		P t;
		t=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int nx=t.fi+dx[i],ny=t.sec+dy[i];
			if(nx<0||nx>=H||ny<0||ny>=W)continue;
			if(d[nx][ny]!=INF||f[nx][ny]=='X')continue;
			d[nx][ny]=d[t.fi][t.sec]+1;
			if(nx==pos[n+1].fi&&ny==pos[n+1].sec)return d[nx][ny];
			q.push(P(nx,ny));
		}
	}
}
int main()
{
	cin >> H >> W >> N;
	for(int i=0;i<H;i++)
	{
		for(int j=0;j<W;j++)
		{
			cin >> f[i][j];
			if(f[i][j]=='S')pos[0]=P(i,j);
			else if(f[i][j]!='.'&&f[i][j]!='X')pos[f[i][j]-'0']=P(i,j);
		}
	}
	int ans=0;
	for(int i=0;i<N;i++)
	{
		ans+=bfs(i);
		//cout << bfs(i) << endl;
	}
	cout << ans << endl;
	return 0;
}

C++: 
#include<cstdio>
#include<utility>
#include<queue>
using namespace std;
const static int MAX_H = 1010, MAX_W = 1010;
static char maze[MAX_H][MAX_W];
static int state[MAX_H][MAX_W];
static int H, W;
static int dx[4] = { 0,1,0,-1 };
static int dy[4] = { -1,0,1,0 };
typedef pair<int, int > point;
int bfs(point start,point end) {
	for (int i = 0; i < H; ++i)
		for (int j = 0; j < W; ++j)
			state[i][j] = -1;
	queue<point> que;
	que.push(start);
	state[start.first][start.second] == 0;
	while (que.size()) {
		point now = que.front();
		que.pop();
		if (now.first == end.first&&now.second == end.second) {
			break;
		}
		for (int i = 0; i < 4; i++) {
			int nx = now.first + dx[i];
			int ny = now.second + dy[i];
			if (nx >= 0 && nx < H&&ny >= 0 && ny < W&&state[nx][ny] == -1 && maze[nx][ny] != 'X') {
				que.push(point(nx, ny));
				state[nx][ny] = state[now.first][now.second] + 1;
			}
		}
	}
	return state[end.first][end.second];
}
int main() {
	int n;
	scanf("%d %d %d", &H, &W, &n);
	getchar();
	int k = 0;
	point cheese[12];
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			scanf("%c", &maze[i][j]);
		}
		getchar();
	}
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (maze[i][j] != 'X'&&maze[i][j] != '.') {
				if (maze[i][j] == 'S')
					cheese[0] = point(i, j);
				else
					cheese[maze[i][j] - '0'] = point(i, j);
			}
		}
	}
	int ret = 0;
	for (int i = 0; i < n; i++) {
		ret += bfs(cheese[i], cheese[i + 1]);
		ret++;
	}
	printf("%d\n", ret);
}