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 <utility>
#include <queue>
#include <algorithm>
using namespace std;

bool ary[1010][1010];
bool sag[1010][1010];
int goalx[11],goaly[11];
int main(){
	queue<pair<pair<int,int>,int> > qu;
	pair<pair<int,int>,int> pa;
	int H,W,N,YY,XX,TT,keka;
	char ch;
	keka=0;
	cin>>H>>W>>N;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin>>ch;
			if(ch>='1'&&ch<='9'){
				goaly[ch-'0']=i;
				goalx[ch-'0']=j;
			}
			if(ch=='S'){
				goaly[0]=i;
				goalx[0]=j;
			}
			ary[i][j]=(ch=='X');
		}
	}

	for(int sta=0;sta<N;sta++){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				sag[i][j]=false;
			}
		}
		qu.push(make_pair(make_pair(goaly[sta],goalx[sta]),0));
		while(!qu.empty()){
			pa=qu.front();
			qu.pop();
			YY=pa.first.first;
			XX=pa.first.second;
			TT=pa.second;
			if(YY==goaly[sta+1]&&XX==goalx[sta+1]){
				//cout<<sta<<" "<<TT<<endl;//
				keka+=TT;
				while(!qu.empty())qu.pop();
				break;
			}

			if(YY>0&&!ary[YY-1][XX]&&!sag[YY-1][XX]){
				qu.push(make_pair(make_pair(YY-1,XX),TT+1));
				sag[YY-1][XX]=true;
			}
			if(YY<H-1&&!ary[YY+1][XX]&&!sag[YY+1][XX]){
				qu.push(make_pair(make_pair(YY+1,XX),TT+1));
				sag[YY+1][XX]=true;
			}
			if(XX>0&&!ary[YY][XX-1]&&!sag[YY][XX-1]){
				qu.push(make_pair(make_pair(YY,XX-1),TT+1));
				sag[YY][XX-1]=true;
			}
			if(XX<W-1&&!ary[YY][XX+1]&&!sag[YY][XX+1]){
				qu.push(make_pair(make_pair(YY,XX+1),TT+1));
				sag[YY][XX+1]=true;
			}
		}
	}
	cout<<keka<<endl;
	return 0;
}

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

using namespace std;

typedef pair<int,int> pairi;

int h,w,n;
char map[1002][1002];
int visited[1002][1002];

int x,y;

main(){
  scanf("%d%d%d", &h, &w, &n);

  for(int i=0;i<h+2;i++){
    for(int j=0;j<w+2;j++){
      map[i][j] = 'X';
      if(i == 0 || i == h+1 || j == 0 || j == w+1) continue;
      scanf(" %c", &map[i][j]);
      if(map[i][j] == 'S') x = i, y = j;
    }
  }

  queue<pairi> q;
  char target;

  int r = 0;

  for(int i=0;i<n;i++){
    target = '1'+i;

    memset(visited, 0, sizeof(visited));

    q.push(pairi(x,y));
    visited[x][y] = 1;

    while(!q.empty()){
      x = q.front().first;
      y = q.front().second;
      q.pop();

      if(map[x][y] == target){
	r += visited[x][y]-1;
	break;
      }

      if(map[x+1][y] != 'X' && visited[x+1][y] == 0){
	q.push(pairi(x+1,y));
	visited[x+1][y] = visited[x][y]+1;
      }

      if(map[x-1][y] != 'X' && visited[x-1][y] == 0){
	q.push(pairi(x-1,y));
	visited[x-1][y] = visited[x][y]+1;
      }

      if(map[x][y+1] != 'X' && visited[x][y+1] == 0){
	q.push(pairi(x,y+1));
	visited[x][y+1] = visited[x][y]+1;
      }

      if(map[x][y-1] != 'X' && visited[x][y-1] == 0){
	q.push(pairi(x,y-1));
	visited[x][y-1] = visited[x][y]+1;
      }
    }
  }

  printf("%d\n", r);
}

C++: #include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#define MAXV  10001
using namespace std;
char map[1017][1017];
int m,n,x,d[1017][1017],drc[4][2]={-1,0,1,0,0,1,0,-1};
typedef pair<int,int> Point;
int bfs(int sx,int sy,int ex,int ey)
{
    queue<Point> que;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            d[i][j]=INF;
    d[sx][sy]=0;
    que.push(Point(sx,sy));
    while(!que.empty())
    {
        //cout<<"in"<<endl;
        Point cur = que.front(); que.pop();
        if(cur.first==ex&&cur.second==ey) break;
        for(int i=0;i<4;i++)
        {
            //cout<<"here"<<endl;
            int nx = cur.first+drc[i][0];
            int ny = cur.second+drc[i][1];
            if((d[nx][ny]==INF)&&map[nx][ny]!='X')
            {
                d[nx][ny]=d[cur.first][cur.second]+1;
                que.push(Point(nx,ny));
            }
        }
    }
    return d[ex][ey];
}
int main()
{
    char p='1';
    scanf("%d%d%d",&m,&n,&x);
    bool start = false,ed = false;
    int ex[11]={0},ey[11]={0},ans=0,q=1;
    for(int i =0;i<m;i++)
            scanf("%s",map[i]);
    for(int i=0;i<m&&q<=x;i++)
        for(int j=0;j<n&&q<=x;j++)
            {
                if(map[i][j]=='S')
                {
                    ex[0]=i;
                    ey[0]=j;
                }
                if(map[i][j]==p)
                {
                    ex[q]=i;
                    ey[q]=j;
                    p++;
                    q++;
                }
            }
    for(int i=0;i<x;i++)
        {
            ans+=bfs(ex[i],ey[i],ex[i+1],ey[i+1]);
            //cout<<i<<endl;
        }
    cout<<ans<<endl;
    return 0;
}