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<queue>
using namespace std;

typedef struct{
 int x;
 int y;
}P;//表示坐标

char graph[1000][1000];
P aim[10];
P s,g;//s起点，g终点
int N,W,H;
int vis[1000][1000]; //用来存储距离
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int bfs(P s,P g);

int main(){
    int i,j,t,step=0;
    cin>>H>>W>>N;
    
    for(i=0;i<H;i++)
    for(j=0;j<W;j++){
        cin>>graph[i][j];
      if(graph[i][j]<='9'&&graph[i][j]>='1'){ 
         t=graph[i][j]-'0';
         aim[t].x=i;
         aim[t].y=j;
      } //把1-N的坐标保存在 aim数组中 
      else if(graph[i][j]=='S'){
          s.x=i;
          s.y=j;
          graph[i][j]='.';
      }//得到最初的起点 
    }
    
    for(i=1;i<=N;i++){
       g.x=aim[i].x;
       g.y=aim[i].y;//1-N ，从小到大，每次把一个 作为终点 
       step+=bfs(s,g);
       s.x=g.x;
       s.y=g.y;//到达这个终点后变终点为起点 
       graph[g.x][g.y]='.';
    }
   
    cout<<step<<endl;
}

int bfs(P s,P g){
    queue<P> que;
      P n;
    
    for(int i=0;i<H;i++)
    for(int j=0;j<W;j++)
     vis[i][j]=-1;
     
     que.push(s);
     vis[s.x][s.y]=0;
     
     while(que.size()){
         
       P p=que.front();
         que.pop();
       if(p.x==g.x&&p.y==g.y)
       break;
       
       for(int i=0;i<4;i++){
           n.x=p.x+dx[i];
           n.y=p.y+dy[i];//向四方漫射 
        if(0<=n.x&&n.x<H&&0<=n.y&&n.y<W&&graph[n.x][n.y]!='X'&&vis[n.x][n.y]==-1){
               que.push(n);
               vis[n.x][n.y]=vis[p.x][p.y]+1;
           }
       }
   }
     return vis[g.x][g.y];
}

C++: #include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#include<math.h>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define close ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define open  #ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout); #endif
using namespace std;
typedef long long ll;
const int MAX_N = 1000000+50;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll INF_ll = 0x7fffffffffffffff;
ll mod = 1e9+7;

/*
不要放弃

	Don't give up
*/

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=10*x+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void Out(int a)
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}

struct Node{
	int x, y, sum, pace;
	int v; 
	//friend bool operator < (Node a,Node b){
	//	return a.pace > b.pace;
	//}
}now,nex;

int H, W, N;
char mapp[1001][1010];
int vis[1001][1010];
int sx,sy;
int dirx[4] = {-1,1,0,0};
int diry[4] = {0,0,-1,1};

//priority_queue<Node> q;
queue<Node> q;

void bfs(){
	memset(vis,0,sizeof(vis));
	now.x = sx;
	now.y = sy;
	now.pace = 0;
	now.sum = N;
	now.v = 1;
	vis[sx][sy] = 1;
	q.push(now);
	while(!q.empty()){
		//now = q.top();
		now = q.front();
		q.pop();
		//cout<<now.x<<"@@@"<<now.y<<"@@@"<<now.sum<<"####"<<now.v<<endl;
		if(now.sum == 0){
			printf("%d\n",now.pace);
			break;
		}
		int x1,y1;
		for(int i = 0; i < 4; i++){
			x1 = now.x + dirx[i];
			y1 = now.y + diry[i];
			if(x1 >= 1 && x1 <= H && y1 >= 1 && y1 <= W && mapp[x1][y1] != 'X'){
				if(mapp[x1][y1] == '.'){
					nex.x = x1;
					nex.y = y1;
					nex.pace = now.pace + 1;
					nex.sum = now.sum;
					nex.v = now.v;
					q.push(nex); 
				}
				else{
					int num = mapp[x1][y1] - '0';
					nex.x = x1;
					nex.y = y1;
					nex.pace = now.pace + 1;
					if(now.v >= num && !vis[x1][y1]){
						nex.sum = now.sum - 1;
						nex.v = now.v + 1;
						vis[x1][y1] = 1;
						q.push(nex);	
					}
				}
			}
		}
	}
}

int main(){
	scanf("%d%d%d",&H,&W,&N);
	for(int i = 1; i <= H; i++){
		getchar();
		for(int j = 1; j <= W; j++){
			scanf("%c",&mapp[i][j]);
			if(mapp[i][j] == 'S'){
				sx = i;
				sy = j;
			}
		}
	}
	bfs(); 
 	return 0;
}

/*
                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############
*/