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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
vector<vector<char> > v(2000,vc(2000));
vector<pii>u(1000);
int mx[]={0,0,1,-1};
int my[]={1,-1,0,0};
int h,w,n;
int bfs(pii st,pii g){
    queue<pii>que;
    vvi d(2000,vi(2000,INF));
    que.push(st);
    d[st.first][st.second]=0;
    while(!que.empty()){
        pii p=que.front(); que.pop();
        if(p==g)break;
        rep(i,4){
            int ny=p.first+my[i]; int nx=p.second+mx[i];
            pii np=pii(ny,nx);
            if(0<=ny&&ny<h&&0<=nx&&nx<w&&d[ny][nx]==INF&&v[ny][nx]!='X')
            {
                d[ny][nx]=d[p.first][p.second]+1;
                que.push(np);
            }
        }
    }
    return d[g.first][g.second];
}
int main(){
    pii st;
    cin>>h>>w>>n;
    int ans=0;
    rep(i,h)rep(j,w){
        cin>>v[i][j];
        if(v[i][j]=='S')st=pii(i,j);
            else if(v[i][j]!='X'&&v[i][j]!='.'){
            u[(v[i][j]-48)]=pii(i,j);
            //cout<<v[i][j]-48<<endl;
        }
    }
    u[0]=st;
    for(int i=0;i<n;i++){
        ans+=bfs(u[i],u[i+1]);
    }
    cout<<ans<<endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <sstream>
using namespace std;
typedef pair<int,int> P;

const int max_n=1000+10;
int h,w,n,sx,sy,dx[]={0,0,-1,1},dy[]={-1,1,0,0},d[max_n][max_n],ans;
char c[max_n][max_n];

void bfs(int x,int y,int m){
	if(m>n)return;
	queue<P> q;
	memset(d,0,sizeof(d));
	q.push(P(x,y));
	d[x][y]=1;
	while(!q.empty()){
		P p=q.front();
		q.pop();
		if(c[p.first][p.second]=='0'+m){
		    ans+=d[p.first][p.second]-1;
		    bfs(p.first,p.second,m+1);
		    return;
		}
		for(int i=0;i<4;i++){
			int tx=p.first+dx[i],ty=p.second+dy[i];
			if(tx>=0 && tx<h && ty>=0 && ty<w && c[tx][ty]!='X' && !d[tx][ty]){
				q.push(P(tx,ty));
				d[tx][ty]=d[p.first][p.second]+1;
			}
		}
	}
}

int main(){
    scanf("%d%d%d",&h,&w,&n);
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++){
            cin>>c[i][j];
            if(c[i][j]=='S'){sx=i;sy=j;}
        }
    bfs(sx,sy,1);
    cout<<ans<<endl;
    return 0;
}

C++: #include <stdio.h>  
#include <string.h>  
#include <queue>  
#define MAX_W 1000  
#define MAX_H 1000  
#define MAX_N 9  
  
using namespace std;  
  
char m[MAX_H][MAX_W + 1];  
int w, h, n;  
int gx[MAX_N + 1], gy[MAX_N + 1];  
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};  
  
int bfs(int sx, int sy, int gx, int gy)  
{  
    int d[MAX_H][MAX_W];  
    memset(d, -1, sizeof(d));  
    queue<int> que;  
    que.push(sx * w + sy);  
    d[sx][sy] = 0;  
          
    while(que.size())  
    {  
        int x = que.front()/w, y = que.front()%w;  
        que.pop();  
        if(x == gx && y == gy)  
            break;  
  
        for(int di = 0; di < 4; ++di){  
            int nx = x + dx[di], ny = y + dy[di];  
            if(nx >= 0 && nx < h && ny >= 0 && ny < w && 'X' != m[nx][ny] && -1 == d[nx][ny]){  
                d[nx][ny] = d[x][y] + 1;  
                que.push(nx * w + ny);  
            }  
        }  
    }  
    return d[gx][gy];  
}  
  
void solve()  
{  
    int ans = 0;  
    for(int i = 0; i < n; i ++)  
        ans += bfs(gx[i], gy[i], gx[i + 1], gy[i + 1]);  
    printf("%d\n", ans);  
}  
  
int main()  
{  
    scanf("%d%d%d", &h, &w, &n);  
    for(int i = 0; i < h; i ++)  
        scanf("%s", m[i]);  
          
    for(int i = 0; i < h; i ++)  
        for(int j = 0; j < w; j ++){  
            if('1' <= m[i][j] && '9' >= m[i][j])  
                gx[m[i][j] - '0'] = i, gy[m[i][j] - '0'] = j;  
            else if('S' == m[i][j])  
                gx[0] = i, gy[0] = j;  
        }  
  
    if(w || h)  
        solve();  
    return 0;  
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#define min(x,y) ((x)<(y)?(x):(y))
#define INFI 65535

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1,0,-1,0};
int ver[] = {0,-1,0,1};
int ans;


void clear();
void show();
P bfs(int cheese,P n);

int main(void) {
    scanf("%d %d %d", &H, &W, &N);
    P s,e;
    for (int i = 0; i<H; i++) {
        getchar();
        for (int j = 0; j<W; j++) {
            Maps[i][j] = getchar();
            if (Maps[i][j]=='S') s.first = i, s.second = j;
        }
    }

    Maps[s.first][s.second] = '.';
    
    for (int i = 1; i<=N; i++) {
        clear();
        s = bfs(i, s);
    }
    
    printf("%d\n", ans);
    return 0;
}

P bfs(int cheese,P n) {
    queue<P> que;
    que.push(n);
    dist[n.first][n.second] = 0;
    
    P top;
    while (que.size()) {
        top = que.front();
        if (Maps[top.first][top.second]-'0'==cheese) {
            ans += dist[top.first][top.second];
            break;
        }
        
        que.pop();
        for (int i = 0; i<4; i++) {
            int nv = top.first+ver[i], nh = top.second+hor[i];
            if (nv>=0 && nv<H && nh>=0 && nh<W && dist[nv][nh]==INFI && Maps[nv][nh]!='X') {
                que.push(P(nv,nh));
                dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second]+1);
            }
        }
    }
    return top;
}

void clear() {
    for (int i = 0; i<H; i++)
        for (int j = 0; j<W; j++)
            dist[i][j] = INFI;
}