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<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
int main(void)
{
	char p[2];
	queue<pair<int,int> > q;
	pair<int,int> g,s,w;
	int x,y,n,m,a=0,f[1002][1002],l=1,l2;
	scanf("%d %d %d",&x,&y,&l2);
	for(n=0;n<=1002;n++){
		for(m=0;m<=1002;m++){
			if(n==0||n>x||m==0||m>y){
				f[n][m]=10;
			}
			else{
				scanf("%c",&p);
				if(p[1]=='S'){
					f[n][m]=0;
					s.first=n;
					s.second=m;
					q.push(s);
				}
				else if(p[1]=='X'){
					f[n][m]=10;
				}
				else if(p[1]=='.'){
					f[n][m]=0;
				}
				else{
					f[n][m]=p[1];
				}
			}
		}
	}
	while(!q.empty()){
		s=q.front();
		q.pop();
		if(f[s.first+1][s.second]<=l){
			w.first=s.first+1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second+1]<=l){
			w.first=s.first;
			w.second=s.second+1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first-1][s.second]<=l){
			w.first=s.first-1;
			w.second=s.second;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}

		if(f[s.first][s.second-1<=l]){
			w.first=s.first;
			w.second=s.second-1;
			a++;
			if(f[w.first][w.second]>0){
				l++;
			}
			else if(f[w.first][w.second]==l2){
				break;
			}
			else{
				q.push(w);
			}
		}
	}
	printf("%d\n",a);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std ;

#define inf 99999999
typedef pair<int,int> pii ;

int h ,w ,n ,minute ;
char maze[100][100] ;
queue<pii> q ;
int data[100][100] ;

int bfs( int N )
{
    int y[4] = { -1, 0, 1,  0 } ;
    int x[4] = {  0, 1, 0, -1 } ;
    pii Pcmp ;
    int PcmpY = 0 ;
    int PcmpX = 0 ;

    for ( int i = 0 ; i < h ; i++ )
    {
        for ( int j = 0 ; j < w ; j++ )
        {
            if ( ( N == 0 && maze[i][j] == 'S' ) || ( N != 0 && maze[i][j] == '0'+N ) )
            {
                data[i][j] = 0 ;
                q.push( pii(i,j) ) ;
            }
            else data[i][j] = inf ;
        }
    }

    while ( q.size() )
    {
        Pcmp = q.front() ;
        q.pop() ;
        if ( maze[Pcmp.first][Pcmp.second] == '0'+N+1 ) break ;

        for ( int i = 0 ; i < 4 ; i++ )
        {
            PcmpY = Pcmp.first + y[i] ;
            PcmpX = Pcmp.second + x[i] ;
            if ( PcmpY >= 0 && PcmpY <= h-1 && PcmpX >= 0 && PcmpX <= w-1 )
            {
                if ( maze[PcmpY][PcmpX] != 'X' && data[PcmpY][PcmpX] == inf )
                {
                    q.push( pii( PcmpY,PcmpX ) ) ;
                    data[PcmpY][PcmpX] = data[Pcmp.first][Pcmp.second] + 1 ;
                }
            }
        }
        /* [debug]
        for ( int i = 0 ; i < h ; i++ )
        {
            printf("[debug]%3d:",i);
            for ( int j = 0 ; j < w ; j++ )
            {
                printf("%8d,",data[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        */
    }

    while ( q.size() ) q.pop() ;
    return data[Pcmp.first][Pcmp.second] ;
}

int main()
{
    cin >> h >> w >> n ;
    for ( int i = 0 ; i < h ; i++ )
    {
        cin >> maze[i] ;
    }
    for ( int i = 0 ; i < n ; i++ )
    {
        minute += bfs(i) ;
    }
    printf("%d\n",minute) ;
    return 0 ;
}

C++: //AOJ 0558
#include <iostream>
#include <queue>
#include <fstream>
using namespace std;

int h, w, n;
char map[1024][1024];
//到当前工厂距&#31163;
int d[1024][1024];
int direction[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
int factory[16][2];
typedef pair<int, int>point;

int bfs(int sx, int sy, int gx, int gy){
	for (int i = 0; i < h; ++i)
		for (int j = 0; j < w; ++j)
			d[i][j] = -1;
	queue<point>q;
	q.push(point(sx, sy));
	d[sx][sy] = 0;
	while (!q.empty()){
		point cur = q.front();
		q.pop();
		if (cur == point(gx, gy))
			break;
		for (int i = 0; i < 4; ++i){
			int nx = cur.first + direction[i][0];
			int ny = cur.second + direction[i][1];
			if (nx >= 0 && ny >= 0 && nx < h&&ny < w
				&&map[nx][ny]!='X'&&d[nx][ny]==-1){
				q.push(point(nx, ny));
				d[nx][ny] = d[cur.first][cur.second] + 1;
			}
		}
	}
	return d[gx][gy];
}
int main(){
	//ifstream in("E:\\in.txt");
	while (cin >> h >> w >> n){
		if (!h || !w || !n)
			return 0;
		for (int i = 0; i < h; ++i){
			for (int j = 0; j < w; ++j){
				cin >> map[i][j];
				if (map[i][j] == 'S'){
					factory[0][0] = i;
					factory[0][1] = j;
					map[i][j] = '.';
				}
				else if (isdigit(map[i][j])){
					int index = map[i][j] - '0';
					factory[index][0] = i;
					factory[index][1] = j;
				}
			}
		}
		int step = 0;
		for (int i = 0; i < n; ++i){
			step += bfs(factory[i][0], factory[i][1], 
				factory[i + 1][0], factory[i + 1][1]);
		}
		cout << step << "\n";
	}
	return 0;
}