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<cstdio>
#include<queue>
using namespace std;
typedef pair<int, int> P;

const int INF=9999999;
char map[1005][1005];
int d[1005][1005];
int n, m, xx;
int res;
int sx, sy;
int gx, gy;
int x[10];
int y[10];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
queue<P> s;
int bfs()
{
    while(!s.empty()){
        s.pop();
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
          d[i][j]=INF;
    s.push(P(sx, sy));
    d[sx][sy]=0;
    while(!s.empty())
    {
        P tmp= s.front();
        s.pop();
        if(tmp.first==gx && tmp.second==gy)
        {
            break;
        }
        for(int i=0; i<4; i++)
        {
            int nx = tmp.first+dx[i];
            int ny = tmp.second+dy[i];
            if(nx>=0 && nx<n && ny>=0 && ny<m && map[nx][ny]!='X' &&d[nx][ny]==INF)
            {
                s.push(P(nx, ny));
                d[nx][ny]=d[tmp.first][tmp.second]+1;
            }
        }
    }
    return d[gx][gy];
}

int main()
{
    while(scanf("%d%d%d", &n, &m, &xx)!=EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%s", map[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
              if(map[i][j]=='S')
              {
                  sx=i; sy=j;
              }
              if(map[i][j]=='1')
              {
                  x[1]=i;
                  y[1]=j;
              }
              if(map[i][j]=='2')
              {
                  x[2]=i;
                  y[2]=j;
              }
              if(map[i][j]=='3')
              {
                  x[3]=i;
                  y[3]=j;
              }
              if(map[i][j]=='4')
              {
                  x[4]=i;
                  y[4]=j;
              }
              if(map[i][j]=='5')
              {
                  x[5]=i;
                  y[5]=j;
              }
              if(map[i][j]=='6')
              {
                  x[6]=i;
                  y[6]=j;
              }
              if(map[i][j]=='7')
              {
                  x[7]=i;
                  y[7]=j;
              }
              if(map[i][j]=='8')
              {
                  x[8]=i;
                  y[8]=j;
              }
              if(map[i][j]=='9')
              {
                  x[9]=i;
                  y[9]=j;
              }

            }
            res=0;
            gx=x[1]; gy=y[1];
            res+=bfs();
            for(int i=2; i<=xx; i++)
            {
                sx=gx; sy=gy;
                gx=x[i]; gy=y[i];
                res+=bfs();
            }
        printf("%d\n", res);

    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;

char ma[1005][1005];int vis[1005][1005];
int w,h,n;

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

struct node
{
    int x,y,time;
};
node start;

void bfs(char ed)
{
    queue<node> que;
    que.push(start);
    vis[start.x][start.y]=1;
    while(!que.empty())
    {
        node no=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int x=no.x+dx[i];
            int y=no.y+dy[i];
            if(vis[x][y]||x>=h||x<0||y>=w||y<0||ma[x][y]=='X')
                continue;
            if(ma[x][y]==ed)
            {
                start.x=x;
                start.y=y;
                start.time=no.time+1;
                return ;
            }
            vis[x][y]=1;
            node no1;
            no1.x=x;no1.y=y;no1.time=no.time+1;
            que.push(no1);
        }
    }
}

int main()
{
    while(~scanf("%d %d %d", & h, & w, & n))
    {
        for(int i=0;i<h;i++)
        {
            scanf("%s",&ma[i]);
            for(int j=0;j<w;j++)
                if(ma[i][j]=='S')
                {
                    start.x=i;
                    start.y=j;
                    start.time=0;
                }

        }
            for(int i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                bfs(i+'1');
            }

            printf("%d\n",start.time);
    }
    return 0; 
}