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<queue>
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)

using namespace std;


struct P{
    int x;
    int y;
};

int w,h,n;
const int INF = 1e+8;
char feld[1010][1010];
int d[1010][1010];
int sx,sy,gx,gy;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans=0;
P che[15];


int bfs(){
    for(int i=0;i<n,i++){
        P sp=che[i];
        sx=che[i].x;
        sy=che[i].y;
        gx=che[i+1].x;
        gy=che[i+1].y;
        queue<P> que;
        for(int y=0;y<h;y++)for(int x=0;x<w;x++)d[y][x]=INF;
        que.push(sp);
        d[sy][sx]=0;
        while(!que.empty()){
            P p=que.front();
            que.pop();
            int x=p.x;
            int y=p.y;
            if(x==gx&&y==gy){
                ans+=d[gy][gx];
                break;
            }
            REP(int i=0;i<4;i++){
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx<0||ny<0&&nx>=w&&ny>=h)continue;
                if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                    P next;
                    next.x=nx;
                    next.y=ny;
                    que.push(next);
                    d[ny][nx]=d[y][x]+1;
                }
            }
        }
    }
    return 0;
}

int main(){
    cin>>h>>w>>n;
    REP(int y=0;y<h;y++){
        REP(int x=0;x<w;x++){
            scanf(" %c",&feld[y][x]);
            if(feld[y][x]=='S'){
                che[0].x=x;
                che[0].y=y;
            }else if(feld[y][x]>='1'&&feld[y][x]<='9'){
                char c=feld[y][x];
                che[c-'0'].x=x;
                che[c-'0'].y=y;
            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

int h,w,n;
const int MAX_N=1005;
char map[MAX_N][MAX_N];
struct point{
    int x,y;
    int step;
    point(){
        x=0; y=0; step=0;
    }
    point(int xx, int yy, int ss){
        x=xx;
        y=yy;
        step=ss;
    }
};
struct point p[16];

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

int ok(int x, int y){
    if(x>=0 && x<h && y>=0 && y<w) return 1;
    return 0;
}

int bfs(int next){
    int nextcheese = next +1; 
    
    queue<point> q;
    point cur(p[next].x, p[next].y,0);
    q.push(cur);
    while(q.size()){
        cur = q.front(); q.pop(); 
        int x=cur.x, y=cur.y;
        int nx, ny;
        for(int i=0; i<4; i++){
            nx = x+dx[i]; ny=y+dy[i];
            if(ok(nx, ny)){
                if((map[nx][ny]-'0')==nextcheese) return ++cur.step;
                if(map[nx][ny]=='.'){
                    point temp(nx, ny, cur.step+1);
                    q.push(temp);
                }
            }
        }
    }
}

void solve(){
    cin>>h>>w>>n;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            cin>>map[i][j];
        }
    }
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            /*char ch=map[i][j];
            switch(ch){
                case '.': break;
                case 'X': break;
                case 'S': p[0].x = i; p[0].y = j; map[i][j]='.';break;
                default:
                int t=(ch-'0');
                p[t].x = i; p[t].y = j;
            }*/
            if(map[i][j]=='S'){
                p[0].x=i; p[0].y = j;
                map[i][j]='.';
            }
            else if(isdigit(map[i][j])){
                int index=map[i][j]-'0';
                p[index].x=i; p[index].y=j;
            }
        }
    }
    int step=0;
    for(int i=0; i<n; i++){
        step+=bfs(i);
    }
    printf("%d\n", step);
}


int main(){
    solve();
}

C++: //
//  AOJ 0558 Chees
//
//  Created by TaoSama on 2015-02-20
//  Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

struct Point {
	int x, y;
	Point(int x = 0, int y = 0): x(x), y(y) {}
} c[15];
int n, m, k, dp[1005][1005];
int d[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1};
char a[1005][1005];

int bfs(int x) {
	memset(dp, 0x3f, sizeof dp);
	queue <Point> q; q.push(c[x - 1]);
	dp[c[x - 1].x][c[x - 1].y] = 0;
	while(!q.empty()) {
		Point cur = q.front(); q.pop();
		for(int i = 0; i < 4; ++i) {
			Point nxt(cur.x + d[i][0], cur.y + d[i][1]);
			if(nxt.x < 1 || nxt.x > n || nxt.y < 1 || nxt.y > m
			        || a[nxt.x][nxt.y] == 'X') continue;
			if(dp[nxt.x][nxt.y] == INF) {
				dp[nxt.x][nxt.y] = dp[cur.x][cur.y] + 1;
				q.push(nxt);
			}
		}
	}
	return dp[c[x].x][c[x].y];
}
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(cin >> n >> m >> k) {
		for(int i = 1; i <= n; ++i) {
			for(int j = 1; j <= m; ++j) {
				cin >> a[i][j];
				if(a[i][j] == 'S')
					c[0].x = i, c[0].y = j;
				if(isdigit(a[i][j]))
					c[a[i][j] - '0'].x = i, c[a[i][j] - '0'].y = j;
			}
		}
		int ans = 0;
		for(int i = 1; i <= k; ++i)
			ans += bfs(i);
		cout << ans << endl;
	}
	return 0;
}