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>

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define INF  numeric_limits<int>::max()
#define NINF numeric_limits<int>::min()

using namespace std;
using ll = long long;

using coordinate = struct {
    int x;
    int y;
};

int H, W, N;
char world_map[1000][1000];
int  cost_map[1000][1000];

bool update_map(const coordinate& target, const int& cost) {
    if(0 <= target.x && target.x < W &&
       0 <= target.y && target.y < H ) {
        if(world_map[target.y][target.x] != 'X' && cost_map[target.y][target.x] == -1) {
            cost_map[target.y][target.x] = cost;
            return true;
        }
    }
    return false;
}

int get_shortest_path_length(const coordinate& s, const coordinate& g) {
    REP(hi, H) {
        REP(wi, W) {
            cost_map[hi][wi] = -1;
        }
    }

    std::queue<coordinate> queue;
    queue.push(s);
    cost_map[s.y][s.x] = 0;

    while(true) {
        coordinate dst;
        coordinate src = queue.front();
        queue.pop();

        int cost = cost_map[src.y][src.x] + 1;

        dst = coordinate{src.x + 1, src.y};
        if(update_map(dst, cost)) {
            queue.push(dst);
        }

        dst = coordinate{src.x - 1, src.y};
        if(update_map(dst, cost)) {
            queue.push(dst);
        }

        dst = coordinate{src.x, src.y + 1};
        if(update_map(dst, cost)) {
            queue.push(dst);
        }

        dst = coordinate{src.x, src.y - 1};
        if(update_map(dst, cost)) {
            queue.push(dst);
        }

        if(cost_map[g.y][g.x] != -1) {
            break;
        }
    }

    return cost_map[g.y][g.x];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    std::cin >> H >> W >> N;

    coordinate pos;
    vector<coordinate> factories(N);

    REP(hi, H) {
        REP(wi, W) {
            std::cin >> world_map[hi][wi];
            if(world_map[hi][wi] == 'S') {
                pos = coordinate{wi, hi};
            }
            else if('1' <= world_map[hi][wi] && world_map[hi][wi] <= '9') {
                factories[(int)(world_map[hi][wi] - '1')] = coordinate{wi, hi};
            }
        }
    }

    int ans = 0;
    for(const auto& factory : factories) {
        ans += get_shortest_path_length(pos, factory);
        pos = factory;
    }

    std::cout << ans << std::endl;

    return 0;
}

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

using namespace std;

typedef pair<int, int> P;

const int INF = 100000000;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;

//&#36755;入
int H, W, N;
char maze[MAX_H][MAX_W + 1];

int sx, sy;
int d[MAX_H][MAX_W];

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

//求以(sx, sy)&#20026;起点到&#36798;目&#26631;c的最短路径&#38271;度
int bfs(char c){
	//初始化
	for(int i = 0; i < H; i ++){
		fill(d[i], d[i] + W, INF);
	}
	d[sx][sy] = 0;
	queue<P> que;
	que.push(P(sx, sy));
	//&#23485;度&#20248;先
	while(!que.empty()){
		P p = que.front();
		que.pop();
		//已找到
		if(maze[p.first][p.second] == c){
			//重置起点
			sx = p.first;
			sy = p.second;
			break;
		}

		for(int i = 0; i < 4; i ++){
			int nx = p.first + dx[i], ny = p.second + dy[i];
			//判断是否可移&#21160;以及是否已&#32463;&#35775;&#38382;&#36807;
			if(0 <= nx && nx < H && 0 <= ny && ny < W && maze[nx][ny] != 'X' && d[nx][ny] == INF){
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
//	printf("%d, %d\n", sx, sy);
	return d[sx][sy];
}

void solve(){
	//找起始点
	for(int i = 0; i < H; i ++){
		for(int j = 0; j < W; j ++){
			if(maze[i][j] == 'S'){
				sx = i;
				sy = j;
				break;
			}
		}
	}
	//&#23485;度&#20248;先搜索
	int ans = 0;
	for(int i = 1; i <= N; i ++){
		ans += bfs('0' + i);
	}
	printf("%d\n", ans);
}

int main(int argc, char const *argv[]){

	while(scanf("%d %d %d", &H, &W, &N)){
		for(int i = 0; i < H; i ++){
			scanf("%s", maze[i]);
		}
		solve();
	}

	return 0;
}

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

#define INF 100000000
#define MAX_H 1000
#define MAX_W 1000

using namespace std;

typedef pair <int,int> P;

int sx,sy,gx,gy;

char maze[MAX_H][MAX_W];
int H,W,N;
int sum;

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

void bfs(int n){
	if (n==N) {
		return;
	}
	else{
		queue<P> que;
		if(n!=0){
			for (int i=0; i<H; i++) {
				for (int j=0; j<W; j++) {
					d[i][j]=INF;
					if (maze[i][j]==n+48) {
						sx=j;
						sy=i;
					}
					if (maze[i][j]==n+49) {
						gx=j;
						gy=i;
					}
				}
			}
		}
		que.push(P(sy,sx));
		d[sy][sx]=0;
		while (que.size()) {
			P p=que.front();
			que.pop();
			if (p.first==gy&&p.second==gx) {
				break;
			}
			for (int i=0; i<4; i++) {
				int nx=p.second+dx[i],ny=p.first+dy[i];
				if (0<=nx&&nx<W&&0<=ny&&ny<H&&maze[ny][nx]!='X'&&d[ny][nx]==INF) {
					que.push(P(ny,nx));
					d[ny][nx]=d[p.first][p.second]+1;
				}
			}
		}
	
	sum+=d[gy][gx];
	
	bfs(n+1);
	return;
	}
}

int main(){
	cin>>H>>W>>N;
	for (int i=0; i<H; i++) {
		for (int j=0; j<W; j++) {
			d[i][j]=INF;
			cin>>maze[i][j];
			if (maze[i][j]=='S') {
				sx=j;
				sy=i;
			}
			if (maze[i][j]=='1') {
				gx=j;
				gy=i;
			}
		}
	}

	bfs(0);
	cout << sum <<endl;
	
}