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 <cstdio>
#include <queue>
#include <algorithm>
using namespace::std;
#define INF 1000000
struct point{
    int x,y;
};
int h,w,n,ans=0,i,j,x,y;
char map[1000][1000];
point cheese[10];
int move(int p){
    int count[1000][1000],i,j;
    for (i=0; i<w; i++) {
        for (j=0; j<h; j++) {
            count[i][j]=INF;
        }
    }
    count[cheese[p].x][cheese[p].y]=0;
    queue<point> q;
    q.push(cheese[p]);
    while (!q.empty()) {
        point position=q.front();
        q.pop();
        int a[]={0,1,0,-1},b[]={1,0,-1,0};
        for (i=0; i<4; i++) {
            if (position.x+a[i]>=0&&position.x+a[i]<w&&position.y+b[i]>=0&&position.y+b[i]<h&&map[position.x+a[i]][position.y+b[i]]!='X'&&count[position.x+a[i]][position.y+b[i]]==INF) {
                count[position.x+a[i]][position.y+b[i]]=count[position.x][position.y]+1;
                point t;
                t.x=position.x+a[i];
                t.y=position.y+b[i];
                q.push(t);
            }
        }
    }
    return count[cheese[p+1].x][cheese[p+1].y];
}
int main(){
    scanf("%d%d%d",&h,&w,&n);
    for (i=0; i<h; i++) {
        for (j=0; j<w; j++) {
            scanf(" %c",&map[j][i]);
            if (map[j][i]=='S') {
                cheese[0].x=j;
                cheese[0].y=i;
            }else if (map[j][i]>=49&&map[j][i]<=57){
                cheese[map[j][i]-48].x=j;
                cheese[map[j][i]-48].y=i;
            }
        }
    }
    for (i=0; i<n; i++) {
        ans+=move(i);
    }
    printf("%d\n",ans);
    return 0;
}

C++: 


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<set>
#include<map>

using namespace std;
typedef long long ll;
int mod(ll a){
    int c=a%1000000007;
    if(c>=0)return c;
    else return c+1000000007;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
#define inf 100000000/*10^8*/

/////////////////////////////////


struct ball{
    int i,j,count;
};
int h,w,n;
char room[1005][1005];
i_i goal[11];
bool done[1001][1001];

int di[4]={1,0,-1,0};
int dj[4]={0,1,0,-1};

int  dfs(int k){
    for(int i=0;i<=h-1;i++)for(int j=0;j<=w-1;j++)done[i][j]=false;
    int si=goal[k].first,sj=goal[k].second;
    int gi=goal[k+1].first,gj=goal[k+1].second;
    queue<ball> q;
    ball ba;ba.i=si;ba.j=sj;ba.count=1;
    q.push(ba);
    bool whi=true;
    int ans=0;
    while(whi){
        ball pos=q.front();q.pop();
        done[pos.i][pos.j]=true;
        for(int i=0;i<=3;i++){
            int ii=pos.i+di[i],jj=pos.j+dj[i];
            if(0<=ii&&ii<=h-1&&0<=jj&&jj<=w-1&&room[ii][jj]!='X'&&done[ii][jj]==false){
                if(ii==gi&&jj==gj)ans=pos.count,whi=false;
                else{
                    ba.i=ii;ba.j=jj;ba.count=pos.count+1;
                    q.push(ba);
                }
            }
        }
    }
    return ans;
}
int main(){
    cin>>h>>w>>n;
    for(int i=0;i<=h-1;i++){
        scanf("%s",room[i]);
        for(int j=0;j<=w-1;j++){
            if(room[i][j]=='.');
            else if(room[i][j]=='X');
            else if(room[i][j]=='S')goal[0]=make_pair(i,j);
            else goal[room[i][j]-'0']=make_pair(i,j);
        }
    }
    int ans=0;
    for(int i=0;i<=n-1;i++){
        ans+=dfs(i);
    }
    
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 8;
#define inf 0x3f3f3f3f

struct node
{
    int x,y;
    node(int x = 0,int y = 0):x(x),y(y){}
}s,current,st,nd;

int h,w,n;
int step[maxn][maxn];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
string Map[maxn];
int sum = 0;

int bfs()
{
    for(int i = 0 ; i < h ; ++i){
        for(int j = 0 ; j < w; ++j){
            step[i][j] = inf;
        }
    }
    queue<node> q;
    step[st.x][st.y] = 0;
    q.push(st);
    while(!q.empty())
    {
        current = q.front();
        q.pop();
        if(current.x == nd.x && current.y == nd.y) break;
        for(int i = 0; i < 4; ++i)
        {
            s.x = current.x + dx[i];
            s.y = current.y + dy[i];
            if(s.x >= 0 && s.x < h && s.y >= 0 && s.y < w && Map[s.x][s.y] != 'X' && step[s.x][s.y] == inf)
            {
                step[s.x][s.y] = step[current.x][current.y] + 1;
                q.push(s);
            }
        }
    }
    return step[nd.x][nd.y];
}
int main()
{
    while(cin >> h >> w >> n)
    {
        sum = 0;
        for(int i = 0 ; i < h; ++i) cin >> Map[i];
        for(int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (Map[i][j] == 'S') {
                    st.x = i;
                    st.y = j;
                }
            }
        }

        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < h; ++j)
            {
                for(int k = 0; k < w; ++k)
                {
                    if(Map[j][k] == '1' + i)
                    {
                        nd.x = j; nd.y = k;
                        sum += bfs();
                        st.x = j; st.y = k;
                    }
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

/*
3 3 1
S..
...
..1
4 5 2
.X..1
....X
.XX.S
.2.X.
10 10 9
.X...X.S.X
6..5X..X1X
...XXXX..X
X..9X...X.
8.X2X..X3X
...XX.X4..
XX....7X..
X..X..XX..
X...X.XX..
..X.......
*/