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<string.h>
#include<algorithm>
using namespace std;
char a[1001][1001];
int v[1001][1001],n,h,w,sx,sy,ans,s;
int dir[4][2]={-1,0,1,0,0,1,0,-1};
char b[1001];
int check(int x,int y)
{
    if(x>=0&&x<h&&y>=0&&y<w)
        return 1;
    return 0;
}
void dfs(int x,int y,char c,int cnt)
{
    int t1,t2;
    if(cnt>=1000)
        return;
    for(int i=0;i<4;i++)
    {
       // printf("%d\n",cnt);
        t1=x+dir[i][0];
        t2=y+dir[i][1];
        if(check(t1,t2)&&v[t1][t2]==0&&a[t1][t2]!='X')
        {

            v[t1][t2]=1;
            dfs(t1,t2,c,cnt+1);
            v[t1][t2]=0;
        }
         if(a[t1][t2]==c&&check(t1,t2))
        {
            ans=min(ans,cnt+1);
            sx=t1;
            sy=t2;
            //printf("%d, ",ans);
        }
    }

}
int main()
{
    int  i,j,k,m,l;
    while(~scanf("%d %d %d",&h,&w,&n))
    {
        s=0;
        getchar();
        memset(v,0,sizeof(v));
        for(i=0;i<h;i++)
        {
            scanf("%s",&a[i]);
        }
        for(i=0;i<n;i++)
        {
            b[i]=i+1+'0';
        }
        for(i=0;i<h;i++)
        {
            for(j=0;j<w;j++)
            {
                if(a[i][j]=='S')
                {
                    sx=i;
                    sy=j;
                }
            }
        }

        //printf("%d %d\n",sx,sy);

        for(i=0;i<n;i++)
        {
            ans=100000;
            //printf("%d %d %c\n",sx,sy,b[i]);

            dfs(sx,sy,b[i],0);
            s+=ans;
        }
        printf("%d\n",s);
    }
    return 0;
}

C++: #include <queue>
#include <cstdio>
#include <algorithm>

#define MAX_H 1000
#define MAX_DIS 1000000

using namespace std;

typedef pair<int,int> P;

int H,W,N,si,sj;
char bd[MAX_H][MAX_H];
int dis[MAX_H][MAX_H];

int vx[] = {1,-1,0,0},vy[] = {0,0,1,-1};

void solve(){
	queue<P> que;
	int sum=0,n=1,val=0;
	
	while(n<=N){
	
		que.push(P(si,sj));
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				dis[i][j] = MAX_DIS;
			}
		}
		dis[si][sj] = val;

		int qx,qy;
		while(1){
			P v = que.front(); que.pop();
			qx = v.first; qy = v.second;
			
			if(bd[qx][qy] == '0'+(char)n) break;
			for(int i=0;i<4;i++){
				int pi,pj;
				pi = qx+vx[i];
				pj = qy+vy[i];
				if(-1<pi&&pi<H&&-1<pj&&pj<W&&bd[pi][pj]!='X'&&dis[qx][qy]+1<dis[pi][pj]){
					dis[pi][pj] = dis[qx][qy]+1;
					que.push(P(pi,pj));
				}
			}
		}
		si = qx; sj = qy;
		val = dis[qx][qy];
		while(!que.empty()) que.pop();
		n++;
	}
	printf("%d\n",val);
}
	


int main(){
	scanf("%d %d %d",&H,&W,&N);
	
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			char c;
			scanf(" %c",&c);
			bd[i][j] = c;
			if(c=='S'){
				si = i; sj = j;
			}
		}
	}
	solve();
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
int w, h, n;
char map[1024][1024];
// 各点到当前工厂的距&#31163;
int d[1024][1024];
const int direction[4][2] = {
    { -1, 0 },
    { 1, 0 },
    { 0, -1 },
    { 0, 1 },
};
 
int factory[16][2];
typedef pair<int, int> P;
 
 
 
//************************************
// Method:    bfs
// FullName:  bfs
// Access:    public 
// Returns:   int
// Qualifier:
// Parameter: const int & sx 起点x
// Parameter: const int & sy 起点y
// Parameter: const int & gx &#32456;点x
// Parameter: const int & gy &#32456;点y
//************************************
int bfs(int sx, const int sy, const int gx, const int gy)
{
    //memset(d, -1, sizeof(d));
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            d[j][i] = -1;
        }
    }
    queue<P> que;
    que.push(P(sx, sy));
    d[sx][sy] = 0;
    while (que.size())
    {
        const P p = que.front(); que.pop();
        // 如果是&#32456;点就&#32467;束
        if (p.first == gx && p.second == gy)
        {
            break;
        }
 
        // 四方向漫游
        for (int i = 0; i < 4; ++i)
        {
            int nx = p.first + direction[i][0];
            int ny = p.second + direction[i][1];
            // 是否可以移&#21160;，并且&#35813;点没有&#35775;&#38382;&#36807;
            if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx][ny] != 'X' && d[nx][ny] == -1)
            {
                que.push(P(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }
 
    return d[gx][gy];
}
 
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
 
    cin >> h >> w >> n;
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            cin >> map[j][i];
        }
    }
 
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            if (map[j][i] == 'S')
            {
                factory[0][0] = j;
                factory[0][1] = i;
                map[j][i] = '.';
            }
            else if (isdigit(map[j][i]))
            {
                int index = map[j][i] - '0';
                factory[index][0] = j;
                factory[index][1] = i;
            }
        }
    }
 
    int step = 0;
    for (int i = 0; i < n; ++i)
    {
        // 按&#39034;序吃遍中&#21326;
        step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);
    }
     
    cout << step << endl;
 
    return 0;
}