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>

using namespace std;

const int INF = 1000000;
const int MAX_N = 1003;
const int MAX_M = 1003;
char map[MAX_N][MAX_M];
int n, m,k, dis[MAX_N][MAX_M];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

struct state {
    int x, y;
} p[11];

int bfs(int sx, int sy, int gx, int gy) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            dis[i][j] = INF;
    queue<state>que;
    state start;
    start.x = sx, start.y = sy;
    dis[start.x][start.y] = 0;
    que.push(start);
    while (que.size()) {
        state cur = que.front();
        que.pop();
        if (cur.x == gx && cur.y == gy)return dis[cur.x][cur.y];
        for (int i = 0; i < 4; i++) {
            int nx = cur.x + dx[i], ny = cur.y + dy[i];
            if (dis[nx][ny] == INF && map[nx][ny] != 'X') {
                dis[nx][ny] = dis[cur.x][cur.y] + 1;
                state news;
                news.x = nx, news.y = ny;
                que.push(news);
            }
        }
    }
    return -1;
}

void solve() {
    
    int ans = 0;
    for (int i = 0; i <= k - 1; i++) {
        ans += bfs(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);
    }
    printf("%d\n", ans);
}

int main() {
    while (scanf("%d%d%d",&n,&m,&k)) {
        for (int i = 0; i < n; i++)
            scanf("%s", map[i]);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 'S')
                    p[0].x = i, p[0].y = j;
                if (map[i][j] != 'X' && map[i][j] != 'S') {
                    int id = map[i][j] - '0';
                    p[id].x = i;
                    p[id].y = j;
                }
            }
        solve();
    }
    return 0;
}

C++: #include"bits/stdc++.h"
using namespace std;
#define ff first.first
#define fs first.second
#define se second
typedef pair<int,int> P;
typedef pair<P,int> PP;
int Map[1000][1000];
int main(void)
{
	queue<PP> que;
	clock_t start, end;
	int q;
	int h,w,n,x,y,sum;
	int cnt; //ねずみの体力
	int OP,TT;
	int i,j;
	int A[2][4]={{1,0,-1,0},{0,1,0,-1}};
	char T[1000][1000];
	
	cin>>h>>w>>n;
	cnt=1; sum=0;
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cin>>T[i][j];
			if(T[i][j]=='S') x=i,y=j; //スタート地点
		}
	}
	
	//現在日時を取得
	//start=clock();
	
	//map作成
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			if(T[i][j]=='X') Map[i][j]=-1;
			else Map[i][j]=0;
		}
	}

	que.push(PP(P(0,x),y));
	while(!que.empty()){
		PP q=que.front(); que.pop();
		//printf("q.fs=%d q.se=%d\n",q.fs,q.se);
		for(i=0;i<4;i++){
			OP=q.fs+A[0][i],TT=q.se+A[1][i];
			//printf("OP=%d TT=%d\n",OP,TT);
			//printf("OP=%d h=%d TT=%d w=%d Map[OP][TT]=%d\n",OP,h,TT,w,Map[OP][TT]);
			if(OP<h&&0<=OP&&TT<w&&0<=TT&&
				Map[OP][TT]!=-1&&Map[OP][TT]!=cnt){//進んだとき範囲を超えずXでない場合
				//Map[q.fs][q.se]=cnt;
				Map[OP][TT]=cnt;
				que.push(PP(P(q.ff+1,OP),TT));
				if(T[OP][TT]-48==cnt){
					sum+=q.ff+1; cnt++;
					que.push(PP(P(0,OP),TT));
					while(que.size()!=1) que.pop();
					break;
				}
			}
		}
		//map確認
		/*for(int k=0;k<h;k++){
			for(int l=0;l<w;l++) cout<<Map[k][l]<<" ";
			cout<<endl;
		}
		cout<<endl;*/
		if(cnt>n) break;
	}
	
	  //経過時間を取得
	//end = clock();
	//printf("time=%f\n",(double)(end-start)/CLOCKS_PER_SEC);
	
	cout<<sum<<endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

int h,w,n;
const int MAX_N=1005;
char map[MAX_N][MAX_N];
struct point{
    int x,y;
    int step;
    point(){
        x=0; y=0; step=0;
    }
    point(int xx, int yy, int ss){
        x=xx;
        y=yy;
        step=ss;
    }
};
struct point p[16];

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

int ok(int x, int y){
    if(x>=0 && x<h && y>=0 && y<w) return 1;
    return 0;
}

int bfs(int next){
    int nextcheese = next +1; 
    
    queue<point> q;
    point cur(p[next].x, p[next].y,0);
    q.push(cur);
    while(q.size()){
        cur = q.front(); q.pop(); 
        int x=cur.x, y=cur.y;
        int nx, ny;
        for(int i=0; i<4; i++){
            nx = x+dx[i]; ny=y+dy[i];
            if(ok(nx, ny)){
                if((map[nx][ny]-'0')==nextcheese) return ++cur.step;
                if(map[nx][ny]=='.'){
                    point temp(nx, ny, cur.step+1);
                    q.push(temp);
                }
            }
        }
    }
}

void solve(){
    cin>>h>>w>>n;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            cin>>map[i][j];
        }
    }
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            char ch=map[i][j];
            switch(ch){
                case '.': break;
                case 'X': break;
                case 'S': p[0].x = i; p[0].y = j; map[i][j]='.';break;
                default:
                int t=(ch-'0');
                p[t].x = i; p[t].y = j;
            }
        }
    }
    int step=0;
    for(int i=0; i<n; i++){
        step+=bfs(i);
    }
    printf("%d\n", step);
}


int main(){
    solve();
}