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>
#include<algorithm>
#include<cstring>
using namespace std;
char field[1000][1000];
struct date{
    int x,y,cnt;
};
int main(){
	int h,w,n;
	cin >> h >> w >> n;
	date start;
	int ans = 0;
	for(int i = 0; i < h; i++){
	    cin >> field[i];
	    for(int j = 0; j < w; j++){
            if(field[i][j] == 'S'){
                start.x = j,start.y = i,start.cnt = 0;
            }
	    }
	}
	for(int i = 1; i <= n; i++){
	    queue<date> Q;
        Q.push(start);
        while(!Q.empty()){
            date d = Q.front();
            Q.pop();
            if(field[d.y][d.x] == i + '0'){
                start.x = d.x,start.y = d.y,start.cnt = d.cnt;
                break;
            }
            int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
            for(int j = 0; j < 4; j++){
                int px = d.x+dx[j],py = d.y + dy[j];
                if(px < 0 || py < 0 || px >= w || py >= h)continue;
                if(field[py][px] == 'X')continue;
                date next = {px,py,d.cnt+1};
                Q.push(next);
            }
        }
        ans = start.cnt;
	}

	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <cstdlib>
#include <typeinfo>


using namespace std;

const int SUP = -1;

typedef pair<int, int> P;

const int MAX_H = 1000;
const int MAX_W = 1000;
char field[MAX_H][MAX_W + 1];
int d[MAX_H][MAX_W];
int H, W, N;
int sx, sy;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int bfs(){

  int time = 0;

  for (int i = 1; i <= N; i++) {

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) d[i][j] = SUP;

      queue<P> que;

      que.push(P(sx, sy));
      d[sx][sy] = 0;


      while (que.size()){

        P p = que.front(); que.pop();

        if (field[p.first][p.second]-'0' == i) {
          time += d[p.first][p.second];
          sx = p.first;
          sy = p.second;
          break;
        }

        for (int i = 0; i < 4; i++) {

          int nx = p.first + dx[i], ny = p.second + dy[i];

          if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X'&& d[nx][ny] == SUP){
            que.push(P(nx, ny));
            d[nx][ny] = d[p.first][p.second] + 1;
          }
        }
      }
  }
  return time;
}


int main(int argc, char const *argv[]) {
  cin >> H >> W >> N;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S'){
        sx = i;
        sy = j;
      }
    }
  }

  cout << bfs() << endl;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[1001][1001];
long long check[1001][1001];
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(-1==cheese[y.a][y.b]){
			continue;
		}
		if(y.d==cheese[y.a][y.b]){
			if(n==y.d) break;
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			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&&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;
}