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;

int main()
{
	queue < pair<int, int>> q;
	pair<int, int> p;

	char **m;
	char c[10] = { "123456789" };
	int **f;
	int n, k, i, i1, c1, i2, sum = 0;

	cin >> n;
	cin >> k;
	cin >> c1;

	m = new char *[n + 2];
	f = new int *[n + 2];

	for (i = 0; i < n + 2; i++){
		m[i] = new char[k + 2];
		f[i] = new int[k + 2];
	}

	for (i = 1; i <= n; i++){
		for (i1 = 1; i1 <= k; i1++){
			cin >> m[i][i1];
		}
	}

	for (i = 0; i < n + 2; i++){
		for (i1 = 0; i1 < k + 2; i1++){
			if (i == 0 || i == n + 1){
				m[i][i1] = 'X';
			}
			else if (i1 == 0 || i1 == k + 1){
				m[i][i1] = 'X';
			}
		}
	}

	for (i = 1; i <= n; i++){
		for (i1 = 1; m[i][i1] != '\0'; i1++){
			if (m[i][i1] == 'S'){
				q.push(make_pair(i, i1));
			}
		}
	}



	pair<int, int> temp;

	temp = q.front();

	for (i = 0; i < c1; i++){

		for (i1 = 0; i1 < n + 2; i1++){
			for (i2 = 0; i2 < k + 2; i2++){
				if (i1 == 0 || i1 == n + 1){
					f[i1][i2] = 1;
				}
				else if (i2 == 0 || i2 == k + 1){
					f[i1][i2] = 1;
				}
				else
					f[i1][i2] = -1;
			}
		}

		f[temp.first][temp.second] = 0;

		while (!q.empty()){
			p = q.front();
			q.pop();

			//上への探索
			if (m[p.first - 1][p.second] != 'X' && f[p.first - 1][p.second] == -1){
				q.push(make_pair(p.first - 1, p.second));
				f[p.first - 1][p.second] = f[p.first][p.second] + 1;
			}

			//下への探索
			if (m[p.first + 1][p.second] != 'X' && f[p.first + 1][p.second] == -1){
				q.push(make_pair(p.first + 1, p.second));
				f[p.first + 1][p.second] = f[p.first][p.second] + 1;
			}

			//左への探索
			if (m[p.first][p.second - 1] != 'X' && f[p.first][p.second - 1] == -1){
				q.push(make_pair(p.first, p.second - 1));
				f[p.first][p.second - 1] = f[p.first][p.second] + 1;
			}

			//右への探索
			if (m[p.first][p.second + 1] != 'X' && f[p.first][p.second + 1] == -1){
				q.push(make_pair(p.first, p.second + 1));
				f[p.first][p.second + 1] = f[p.first][p.second] + 1;
			}

			//ゴールかどうか？
			if ( m[p.first][p.second] == c[i]){
				sum += f[p.first][p.second];
				temp = p;
			}

		}
	}
	
	cout << sum << endl;
	
return 0;
}

C++: #include <iostream>
#include <memory.h>
#include <queue>

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int c[1000][1000];
int dat[1000][1000];
int cost[1000][1000];
int destination = 1;
int startposx, startposy;
int h, w, n;

void bfsToDestination(int x, int y);

int main (void) {

  int i, j;

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

  for(i = 0; i < h; i++) {
    for(j = 0; j < w; j++) {
      dat[j][i] = fgetc(stdin);
      if (dat[j][i] == '\n') dat[j][i] = fgetc(stdin);
      if (dat[j][i] == 'S') {
	startposx = j; startposy = i;
      }
    }
  }

  for(i = 0; i < h; i++) {
    for(j = 0; j < w; j++) {
      printf("%c", dat[j][i]);
    }
    printf("\n");
  }

  for (i = 0; i < n; i++) {
    memcpy(c, dat, sizeof(dat));
    bfsToDestination(startposx, startposy);
  }
  
  std::cout << cost[startposx][startposy] << "\n";
    
  return 0;
}

void bfsToDestination(int x, int y) {

  int i;
  queue<int> qx, qy;

  qx.push(x); qy.push(y);
  c[x][y] = 'X';
  cost[x][y] = 0;

  while(!qx.empty()) {
    qx.pop(); qy.pop();

    for (i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if ((nx >= 0) && (ny >= 0) && (nx < w) && (ny < h )) {
	if (c[nx][ny] != 'X') {
	  qx.push(); qy.push();
	  c[x][y] = 'X';
	  cost[nx][ny] = cost[x][y] + 1;
	  if ((c[nx][ny] - '0') == destination) {
	    destination++;
	    startposx = nx; startposy = ny;
	    return;
	  }
	}
      }
    }
  }

}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[101][101];
long long check[101][101];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){//
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(y.d==cheese[y.a][y.b]){
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			if(n==y.d){
				break;
			}
			else{
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
			}
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&cheese[y.a+q[i]][y.b+p[i]]!=-1&&check[y.a+q[i]][y.b+p[i]]!=1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<y.c<<endl;
	return 0;
}