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 <queue>
using namespace std;
int w, h, n;
char map[1024][1024];
// 各点到当前工厂的距离
int d[1024][1024];
const int direction[4][2] = {
    { -1, 0 },
    { 1, 0 },
    { 0, -1 },
    { 0, 1 },
};
 
int factory[16][2];
typedef pair<int, int> P;
 
 
 
//************************************
// Method:    bfs
// FullName:  bfs
// Access:    public 
// Returns:   int
// Qualifier:
// Parameter: const int & sx 起点x
// Parameter: const int & sy 起点y
// Parameter: const int & gx 终点x
// Parameter: const int & gy 终点y
//************************************
int bfs(const int& sx, const int& sy, const int& gx, const int& gy)
{
    //memset(d, -1, sizeof(d));
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            d[j][i] = -1;
        }
    }
    queue<P> que;
    que.push(P(sx, sy));
    d[sx][sy] = 0;
    while (que.size())
    {
        const P p = que.front(); que.pop();
        // 如果是终点就结束
        if (p.first == gx && p.second == gy)
        {
            break;
        }
 
        // 四方向漫游
        for (int i = 0; i < 4; ++i)
        {
            int nx = p.first + direction[i][0];
            int ny = p.second + direction[i][1];
            // 是否可以移动，并且该点没有访问过
            if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx][ny] != 'X' && d[nx][ny] == -1)
            {
                que.push(P(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }
 
    return d[gx][gy];
}
 
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
 
    cin >> h >> w >> n;
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            cin >> map[j][i];
        }
    }
 
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            if (map[j][i] == 'S')
            {
                factory[0][0] = j;
                factory[0][1] = i;
                map[j][i] = '.';
            }
            else if (isdigit(map[j][i]))
            {
                int index = map[j][i] - '0';
                factory[index][0] = j;
                factory[index][1] = i;
            }
        }
    }
 
    int step = 0;
    for (int i = 0; i < n; ++i)
    {
        // 按顺序吃遍中华
        step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);
    }
     
    cout << step << endl;
 
    return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> p;

int w, h, n, result, steps;
char parent[1000][1000];
bool visited[1000][1000], map[1000][1000];
p start, last_start, goal, cur, cheese[10];
queue<p> myqueue;

void init(int in){
    start = last_start;

    while(!myqueue.empty()) myqueue.pop();

    for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            parent[i][j] = '0';
            if(map[i][j] == false){
                visited[i][j] = true;
            }else{
                visited[i][j] = false;
            }
            goal = cheese[in];
        }
    }
    return ;
}

int main(){
    cin >> w >> h >> n;

    char input;
    for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            cin >> input;
            if(input == 'X') map[i][j] = false;
            else             map[i][j] = true;
            if(input == 'S')
                last_start = make_pair(i, j);
            if(input != 'X' && input != 'S' && input != '.'){
                cheese[input-'0'] = make_pair(i, j);
                //cout << "cheese " << input-'0' << " : " << i << " " << j << endl;
            }
        }
    }


    result = 0;
    for(int i=1; i<=n; ++i){
        init(i);
        myqueue.push(start);

        while(!myqueue.empty()){
            cur = myqueue.front();
            myqueue.pop();

            if(cur == goal){
                break;
            }

            visited[cur.first][cur.second] = true;

            int x = cur.first, y = cur.second;
            if(x-1 >= 0 && !visited[x-1][y]){
                myqueue.push(make_pair(x-1, y));
                parent[x-1][y] = '1';
            }
            if(y-1 >= 0 && !visited[x][y-1]){
                myqueue.push(make_pair(x, y-1));
                parent[x][y-1] = '2';
            }
            if(x+1 < w && !visited[x+1][y]){
                myqueue.push(make_pair(x+1, y));
                parent[x+1][y] = '3';
            }
            if(y+1 < h && !visited[x][y+1]){
                myqueue.push(make_pair(x, y+1));
                parent[x][y+1] = '4';
            }
        }
        int counter = 0;
        p bcur = goal;
        while(bcur != start){
            counter++;
            if(parent[bcur.first][bcur.second]=='1')
                bcur.first++;
            else if(parent[bcur.first][bcur.second]=='2')
                bcur.second++;
            else if(parent[bcur.first][bcur.second]=='3')
                bcur.first--;
            else if(parent[bcur.first][bcur.second]=='4')
                bcur.second--;
        }
        result += counter;
        //cout << counter << endl;
        last_start = goal;
    }

    cout << result << endl;
    return 0;
}