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++: // from : http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=1811250#1
 
#include<iostream>
#include<vector>
#include<queue>
  
using namespace std;
 
#define DEBUG(x) cout<<(#x)<<": "<<(x)<<endl
  
int nowS = 0;
int x, y, n;
int mp[1000][1000] = {};
int k[4] = {1, -1, 0, 0};
int h[4] = {0, 0, 1, -1};
int cx[9] = {};
int cy[9] = {};
queue<int> bufX, bufY, bufD;
  
bool search(int xx, int yy, int dd) {
    bufX.pop();
    bufY.pop();
    bufD.pop();
  
    for (int i = 0; i < 4; i++) {
        int nx = xx+k[i];
        int ny = yy+h[i];
        if (nx < 0 || nx >= x || ny < 0 || ny >= y)//?????????
            continue;
        if (mp[ny][nx] == -1) {//?????
            continue;
        }
        if (cx[nowS] == nx && cy[nowS] == ny) {//??????
            nowS++;
            while (!bufX.empty()) {
                bufX.pop();
                bufY.pop();
                bufD.pop();
            }
            if (nowS == n) {//?????????????????????
                cout << dd + 1 << endl;
                return true;
            }
            else {//
                bufX.push(nx);
                bufY.push(ny);
                bufD.push(dd + 1);
                mp[ny][nx] = nowS + 1;
            }
            return false;
        }
        if(mp[ny][nx] >= 0 && mp[ny][nx] < nowS + 1){
            bufX.push(nx);
            bufY.push(ny);
            bufD.push(dd + 1);
            mp[ny][nx] = nowS + 1;
        }
    }
    return false;
}
  
int main() {
    int ch = 0;
    cin >> y >> x >> n;
    for (int i = 0; i < y; i++) {//??\???
        char c[1001];
        cin >> c;
        for (int j = 0; j < x; j++) {
            if(c[j] == 'X')
                mp[i][j] = -1;
            else if(c[j] == 'S'){
                bufX.push(j);
                bufY.push(i);
                bufD.push(0);
                mp[i][j] = 1;
            }   
            else {
                mp[i][j] = 0;
            }
            if (c[j] >= 48 && c[j] <= 57) {
                int m;
                m = c[j] - 49;
                cx[m] = j;
                cy[m] = i;      
                ch++;
            }
        }
    }
      
  
    while(!search(bufX.front(), bufY.front(), bufD.front())){}//????????????????§?????????
  
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
struct point{
	int x;
	int y;
};
const int MAX = 10000000;
int h,w,n,c[1001][1001];
char board[1002][1002],tboard[1002][1002];
int bfs(int y,int x){
	int dx[] = {0,-1,1,0},dy[] = {-1,0,0,1},tx,ty,t = 1,min = 0;
	queue<point> que;
	point p,tp;
	c[y][x] = 0; 
	p.x = x;
	p.y = y;
	que.push(p);
	while(!que.empty()){
		p = que.front();
		que.pop();
		for(int i = 0;i < 4;++i){
			tx = p.x + dx[i];
			ty = p.y + dy[i];
			tp.x = tx;
			tp.y = ty;
			if(board[ty][tx] == '\0') continue;
			if(board[ty][tx] != '.' && board[ty][tx] != 'X'){	
				if(board[ty][tx] - '0' == t){	
					min += c[p.y][p.x] + 1;
					if(board[ty][tx] - '0' == n){
						return min;
					}
					for(int j = 1;j <= h;++j){
						for(int k = 1;k <= w;++k){
							c[j][k] = MAX;
						}
					}
					++t;
					while(!que.empty()){
						que.pop();
					}
					c[ty][tx] = 0;
					que.push(tp);
					break;
				}
				else {
					c[ty][tx] = c[p.y][p.x] + 1;
					que.push(tp);
				}
			}
			if(board[ty][tx] == '.' && c[ty][tx] == MAX){
				c[ty][tx] = c[p.y][p.x] + 1;
				que.push(tp);
			}	
		}
	}
	return 0;
}
int main(){
	int x,y;
	string str;
	memset(board,'0',sizeof(char));
	for(int i = 0;i < 1001;++i){
		for(int j = 0;j < 1001;++j){
			c[i][j] = MAX;
		}
	}
	cin >> h >> w >> n;
	for(int i = 0;i < h;++i){
		cin >> str;
		for(int j = 0;j < w;++j){
			board[i+1][j+1] = tboard[i+1][j+1] = str[j];
			if(str[j] == 'S'){
				x = j + 1;
				y = i + 1;
			}
		}
	}
	cout << bfs(y,x) << endl;
	return 0;
}

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++)
#define INF 1e+8

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define INF 1e+7

struct P{
    int x;
    int y;
};

int w,h,n;
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(){
    REP(i,n){
        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;
        REP(y,h)REP(x,w)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(i,4){
                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(y,h){
        REP(x,w){
            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;
}