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>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define trep(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
#define H 1001
#define W 1001
int h, w;
int sx, sy;
int gx, gy;
char c[H][W];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n;
vector<vector<int>> d(H, vector<int>(W, INF));
int bfs(void)
{
    rep(i, h)
    {
        rep(j, w)
        {
            d[i][j] = INF;
        }
    }
    queue<P> q;
    q.push(P(sy, sx));
    d[sy][sx] = 0;
    while (q.size())
    {
        P p = q.front();
        q.pop();
        rep(i, 4)
        {
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];
            if (0 <= ny && ny < h && 0 <= nx && nx < w && c[ny][nx] != 'X' && d[ny][nx] == INF)
            {
                q.push(P(ny, nx));
                d[ny][nx] = d[p.first][p.second] + 1;
            }
        }
    }
    return d[gy][gx];
}
int main()
{
    cin >> h >> w >> n;
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> c[i][j];
            if (c[i][j] == 'S')
            {
                sy = i;
                sx = j;
            }
        }
    }
    int ans = 0;
    trep(i, 1, n + 1)
    {
        rep(j, h)
        {
            rep(k, w)
            {
                if (c[j][k] == (i + '0'))
                {
                    gy = j;
                    gx = k;
                }
            }
        }
        ans += bfs();
        sy = gy;
        sx = gx;
    }
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <stdlib.h>
#include <queue>
#include <cstring>
#include <cstdio>
#define MAX_NUM 1010
using namespace std;
int H, W, N;
bool flag[MAX_NUM][MAX_NUM] = {false};                            //??是否已遍??
int dir[4][2] = { {1, 0}, { -1, 0}, {0, 1}, {0, -1} };
char map[MAX_NUM][MAX_NUM];

struct Node {
    int x;
    int y;
    int step;

    Node(int x, int y, int step): x(x), y(y), step(step) {}
};

int BFS(int sx, int sy, int ex, int ey) {
    queue<Node> que;
    que.push(Node(sx, sy, 0));
    flag[sx][sy] = true;

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

        for(int i = 0; i < 4; i++) {
            int k = cur.x + dir[i][0];
            int v = cur.y + dir[i][1];

            if(k < 0 || k >= H || v < 0 || v >= W || map[k][v] == 'X' || flag[k][v]) {
                continue;
            }

            que.push(Node(k, v, cur.step + 1));
            flag[k][v] = true;

            if(k == ex && v == ey) {
                return cur.step + 1;
            }
        }
    }

    return -1;
}

int main() {
    int sx, sy, arr[10][2];

    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);

    if(cin >> H >> W >> N && H && W) {
        for(int i = 0; i < H; i++) {
            for(int j = 0; j < W; j++) {
                cin >> map[i][j];

                if(map[i][j] == 'S') {
                    map[i][j] = '.';
                    sx = i;
                    sy = j;
                }

                if(isdigit(map[i][j])) {
                    int index = map[i][j] - '0';
                    arr[index][0] = i;
                    arr[index][1] = j;
                }
            }
        }

        arr[0][0] = sx;
        arr[0][1] = sy;
        int res = 0;

        for(int i = 0; i + 1 <= N; i++) {
            memset(flag, false, sizeof(flag));
            res += BFS(arr[i][0], arr[i][1], arr[i + 1][0], arr[i + 1][1]);
        }

        cout << res << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<queue>
using namespace std ;
struct ST
{
	int x ;
	int y ;
	int cnt = 0 ;
} ;
bool came[ 1000 ][ 1000 ] = {{ 0 }} ;
char map[ 1000 ][ 1000 ] ;
int main()
{
	ST m ,s ;
	queue< ST > que ;
	int n ;
	int w ,h ;
	int mx[ 4 ] = { 0 ,0 ,1 ,-1 } ;
	int my[ 4 ] = { 1 ,-1 ,0 ,0 } ;
	cin >> h >> w >> n ;
	char c = '0' ;
	bool a = true ;	
	for( int i = 0 ; i < h ; i++ )
	{
		cin >> map[ i ] ;
		if( a )
		{
			for( int j = 0 ; j < w ; j++ && a )
			{
				if( map[ i ][ j ] == 'S' )
				{
					m.x = j ;
					m.y = i ;
					a = false ;
				}
			}
		}
	}
	que.push( m ) ;
	int cnt = 0 ;
	while( n )
	{
		n-- ;
		c++ ;
		while( !que.empty() )
		{
			m = que.front() ;
			que.pop() ;
			came[ m.y ][ m.x ] = 1 ;
			if( map[ m.y ][ m.x ] == c )
			{
				while( ! que.empty() )
				{
					que.pop() ;
				}
				break ;
			}
			for( int i = 0 ; i < 4 ; i++ )
			{
				if( m.x + mx[ i ] > -1 && m.x + mx[ i ] < w && m.y +  my[ i ] > -1 && m.y + my[ i ] < h )
				{
					s = m ;
					s.x += mx[ i ] ;
					s.y += my[ i ] ;
					s.cnt++ ;
					if( map[ s.y ][ s.x ] != 'X' && came[ s.y ][ s.x ] == 0 )
					{
						que.push( s ) ;
					}
				}
			}
		}
		memset( came ,0 ,sizeof( came ) ) ;
		que.push( m ) ;
	}
	cout << m.cnt << endl ;
	return 0 ;
}