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 <iostream>
#include <queue>
#include <map>
#include <string>

using namespace std;

typedef struct P{
    int x;
    int y;
}P;


int h,w,n;

const int INF = 100000000;

char feld[1010][1010];
P ch[15];
int d[1010][1010];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int sx,sy;
int gx,gy;
int ans=0;

int bfs(){
    for(int i=0;i<n;i++){
        P s_xy=ch[i];
        sx=ch[i].x;
        sy=ch[i].y;
        gx=ch[i+1].x;
        gy=ch[i+1].y;
        for(int y=0;y<h;y++)
            for(int x=0;x<w;x++)
                d[y][x]=INF;
            queue<P> que;
            que.push(s_xy);
            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;
                }
                for(int i=0;i<4;i++){
                    int nx=x+dx[i];
                    int ny=y+dy[i];
                    if(nx<0||nx>=w||ny<0||ny>=h)continue;
                    if(feld[ny][nx]!='X'&&d[ny][nx]==INF){
                        P n;
                        n.x=nx;
                        n.y=ny;
                        d[ny][nx]=d[y][x]+1;
                        que.push(n);
                    }
                }
            }
        }
        return 0;
}

int main(){
    cin>>h>>w>>n;
    char c;
    for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            scanf(" %c",&feld[y][x]);
            c=feld[y][x];
            if(feld[y][x]=='S'){
                ch[0].x=x;
                ch[0].y=y;

            }
            if(c>='1'&&c<='9'){
                ch[c-'0'].x=x;
                ch[c-'0'].y=y;

            }
        }
    }
    bfs();
    cout<<ans<<endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int map[1002][1002], was[1002][1002];
int main(void)
{
	int count = 0, H, W, N, i, j;
	char c=0;
	pair<int, int> q,a[10];
	queue<pair<int, int> > que;
	queue<int> quec;
	quec.push(0);
	scanf("%d %d %d", &H, &W, &N);
	for (i = 0; i <= H + 1; i++)
	{
		map[i][0] = -1;
		map[i][W + 1] = -1;
		was[i][0] = -1;					/*?????????????£???????(???)*/
		was[i][W + 1] = -1;
	}
	for (j = 1; j <= W; j++)
	{
		map[0][j] = -1;
		map[H + 1][j] = -1;           /*?????????????£???????????¨????*/
		was[0][j] = -1;
		was[H + 1][j] = -1;
	}
	for (i = 1; i <= H; i++)
	{
		for (j = 1; j <= W; j++)			/*???????????????(??????????????°???????????????????????????????????¨??°??????????????????*/
		{
			cin >> c;
			if (c == 'X')
				map[i][j] = -1;
			if (c == 'S')
			{
				q.first = i;
				q.second = j;
			}
			if (c == '1')
			{
				a[1].first = i;
				a[1].second = j;
			}
			if (c == '2')
			{
				a[2].first = i;
				a[2].second = j;
			}
			if (c == '3')
			{
				a[3].first = i;
				a[3].second = j;
			}
			if (c == '4')
			{
				a[4].first = i;
				a[4].second = j;
			}
			if (c == '5')
			{
				a[5].first = i;
				a[5].second = j;
			}
			if (c == '6')
			{
				a[6].first = i;
				a[6].second = j;
			}
			if (c == '7')
			{
				a[7].first = i;
				a[7].second = j;
			}
			if (c == '8')
			{
				a[8].first = i;
				a[8].second = j;
			}
			if (c == '9')
			{
				a[9].first = i;
				a[9].second = j;
			}
		}
	}
	/*for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			cout << map[i][j];
		}
		cout << endl;
	}*/
	que.push(q);
	i = 1;
	while (i <= N)
	{
		count = quec.front();
		quec.pop();
		q = que.front();
		que.pop();
		if (q == a[i])
		{
			i++;
			memset(was, 0, sizeof(was));
			while (!que.empty())
				que.pop();
			while (!quec.empty())
				quec.pop();
			quec.push(count);
			que.push(q);
			continue;
		}
		if (map[q.first][q.second + 1] != -1 && was[q.first][q.second + 1] != -1)
		{
			was[q.first][q.second+1] = -1;
			q.second++;
			que.push(q);					/*???????????????*/
			count++;
			quec.push(count);
			count--;
			q.second--;
		}
		if (map[q.first][q.second - 1] != -1 && was[q.first][q.second-1] != -1)
		{
			was[q.first][q.second-1] = -1;
			q.second--;
			que.push(q);
			count++;						/*???????????????*/
			quec.push(count);
			count--;
			q.second--;
		}
		if (map[q.first + 1][q.second] != -1 && was[q.first + 1][q.second] != -1)
		{
			was[q.first + 1][q.second] = -1;
			q.first++;
			que.push(q);
			count++;						/*???????????????*/
			quec.push(count);
			count--;
			q.first--;
		}
		if (map[q.first - 1][q.second] != -1 && was[q.first - 1][q.second] != -1)
		{
			was[q.first][q.second] = -1;
			q.first--;
			que.push(q);
			count++;							/*???????????????*/
			quec.push(count);
			count--;
			q.first++;
		}
	}

	printf("%d\n", count);					/*????????£???????????????????¨??????????*/
	return 0;
}