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*****/
#include <iostream>
#include <fstream>
#include <queue>

/*****マクロ定義*****/
#define START 0
#define EMPTY 10

/*****名前空間*****/
using namespace std;

/*****グローバル変数置き場*****/
char Field[1001][1001];
bool Visited[1001][1001];
int TimeF[1001][1001];

/*****その他関数置き場*****/
int bfs(int sx,int sy,int gx,int gy){
	queue<int> Qx;
	queue<int> Qy;
	int x_now;
	int y_now;
	int x_next;
	int y_next;
	int dx[] = {0,1,0,-1};
	int dy[] = {-1,0,1,0};
	Qx.push(sx);
	Qy.push(sy);
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			Visited[i][j] = false;
			TimeF[i][j] = 0;
		}
	}

	Visited[sy][sx] = true;
	TimeF[sy][sx] = 0;
	while(!(Qx.empty() && Qy.empty())){
		x_now = Qx.front();Qx.pop();
		y_now = Qy.front();Qy.pop();
		//cout << TimeF[y_now][x_now] << endl;
		if(x_now == gx && y_now == gy)	break;
		for(int i=0;i<4;i++){
			x_next = x_now + dx[i];
			y_next = y_now + dy[i];
			if(Field[y_next][x_next] == 'X' || Visited[y_next][x_next]);
			else{
				Qx.push(x_next);
				Qy.push(y_next);
				Visited[y_next][x_next] = true;
				TimeF[y_next][x_next] = TimeF[y_now][x_now] + 1;
			}
		}
	}
	return TimeF[gy][gx];
}
/*****main関数*****/
int main(){
	/*****変数置き場*****/
	int total_time = 0;
	char row[1001];
	int height;
	int width;
	int n;
	int x[] = {0,0,0,0,0,0,0,0,0,0};
	int y[] = {0,0,0,0,0,0,0,0,0,0};
	/*****ファイルオープン*****/	
	ofstream fout("output.txt");
	ifstream fin("input.txt");
	if(!fout || !fin){
		cout << "Can't open the file.\n";
		return;
	}
	
	/*****処理部*****/
	cin >> height >> width >> n;
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			Field[i][j] = 'X';
			Visited[i][j] = false;
			TimeF[i][j] = 0;
		}
	}
	for(int i=0;i<height;i++){
		cin >> row;
		for(int j=0;j<width;j++){
			Field[i+1][j+1] = row[j];
		}
	}
	/*for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			cout << Field[i][j];
		}
		cout << endl;
	}*/
	for(int i=0;i<height+1;i++){
		for(int j=0;j<width+1;j++){
			switch (Field[i][j]){
			case 'S':
				x[START] = j;
				y[START] = i;
				break;
			case '1':
				x[1] = j;
				y[1] = i;
				break;
			case '2':
				x[2] = j;
				y[2] = i;
				break;
			case '3':
				x[3] = j;
				y[3] = i;
				break;
			case '4':
				x[4] = j;
				y[4] = i;
				break;
			case '5':
				x[5] = j;
				y[5] = i;
				break;
			case '6':
				x[6] = j;
				y[6] = i;
				break;
			case '7':
				x[7] = j;
				y[7] = i;
				break;
			case '8':
				x[8] = j;
				y[8] = i;
				break;
			case '9':
				x[9] = j;
				y[9] = i;
				break;
			}
		}
	}
	/*for(int i=0;i<10;i++){
		cout << x[i] << " ";
	}
	cout << endl;
	for(int i=0;i<10;i++){
		cout << y[i] << " ";
	}
	cout << endl;*/
	for(int i=0;i<n;i++){
		total_time += bfs(x[i],y[i],x[i+1],y[i+1]);
	}
	/*for(int i=0;i<20;i++){
		for(int j=0;j<20;j++){
			cout << Visited[i][j];
		}
		cout << endl;
	}*/
	cout << total_time << endl;
	/*****処理終了後*****/
	fout.close();
	fin.close();
	//delete[] pField;
	//delete[] pVisited;
	return 0;
}

C++: #include <cstdio>
#include <map>
#include <queue>
#include <string>

using namespace std;

const int INF = -1;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;

int H, W, N;
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W]; // ?????????????????\??´?????§??????????????°, 1~N??§?????????????????§????????°??????+1
int sx, sy, gx, gy;
int power = 1; // ??????????????????
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

typedef pair<int, int> P;

void reset_d() {
  for (int i = 0; i < H; i++)
    for(int j = 0; j < W; j++) d[i][j] = INF;
}

int bfs(int c) {
  queue<P> que;
  reset_d();
  d[sx][sy] = c;
  que.push(P(sx, sy));
  while(que.size()) {
    P p = que.front(); que.pop();
    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] == INF) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
        if (field[nx][ny] - '0' == power) {
          sx = nx;
          sy = ny;
          power++;
          return d[nx][ny];
        }
      }
    }
  }
  return INF;
}

void solve() {
  int c = 0;
  for (int i = 0; i < N; i++) {
    c = bfs(c);
  }
  printf("%d\n", c);
}


int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", field[i]);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (field[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }
  solve();
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>
using namespace std;

typedef pair<int, int> P;

const int INF = 10000000;

int H, W, N;
char maze[1000][1000];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
P cheeze[10];
int **d;

int main(){
	cin >> H >> W >> N;
	for (int i = 0; i<H; i++){
		for (int j = 0; j<W; j++){
			cin >> maze[i][j];
			if (maze[i][j] == 'S') cheeze[0] = make_pair(i, j);
			int n = maze[i][j] - '0';
			for (int k = 1; k <= N; k++){
				if (n == k) cheeze[n] = make_pair(i, j);
			}
		}
	}
	int ans = 0;
	int num = 1;
	while (num <= N){
		queue<P> que;
		int sx = cheeze[num - 1].first;
		int sy = cheeze[num - 1].second;
		int gx = cheeze[num].first;
		int gy = cheeze[num].second;
		
		d = new int*[H];
		for (int i = 0; i < H; i++){
			d[i] = new int[W];
		}
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				d[i][j] = INF;
			}
		}
		que.push(P(sx, sy));
		d[sx][sy] = 0;
		
		while (que.size()){
			P p = que.front();
			que.pop();
			if (p.first == gx&&p.second == gy) break;

			for (int i = 0; i < 4; i++){
				int nx = p.first + dx[i];
				int ny = p.second + dy[i];
				if (0<=nx&&nx < H&&ny >= 0 && ny < W&&d[nx][ny] == INF&&maze[nx][ny] != 'X'){
					d[nx][ny] = d[p.first][p.second] + 1;
					que.push(P(nx, ny));
				}
			}
		}
		ans += d[gx][gy];
		for (int i = 0; i < H; i++){
			delete[] d[i];
		}
		delete[] d;
		num++;
	}
	cout << ans << endl;
	return 0;
}