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 <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

char str[1001];
bool field[1001][1001];
pair<int,int> point[10];
int bfs[1001][1001];

int main(){
	int a, b, N;
	int ret = 0;
	scanf("%d%d%d", &a, &b, &N);
	for(int i=0;i<a;i++){
		scanf("%s", str);
		for(int j=0;j<b;j++){
			if(str[j]=='X')
				field[i][j] = false;
			else
				field[i][j] = true;
			switch(str[j]){
				case 'S':
					point[0] = make_pair(i,j);
					break;
				case '1':
					point[1] = make_pair(i,j);
					break;
				case '2':
					point[2] = make_pair(i,j);
					break;
				case '3':
					point[3] = make_pair(i,j);
					break;
				case '4':
					point[4] = make_pair(i,j);
					break;
				case '5':
					point[5] = make_pair(i,j);
					break;
				case '6':
					point[6] = make_pair(i,j);
					break;
				case '7':
					point[7] = make_pair(i,j);
					break;
				case '8':
					point[8] = make_pair(i,j);
					break;
				case '9':
					point[9] = make_pair(i,j);
					break;				
			}
		}
	}
	
	queue<pair<int,int> > Q;
	for(int i=0;i<N;i++){
		for(int m=0;m<a;m++){
			for(int n=0;n<b;n++){
				bfs[m][n] = -1;
			}
		}
		int first = point[i].first;
		int second = point[i].second;
		bfs[first][second] = 0;
		Q.push(point[i]);
		while(Q.size()){
			int row = Q.front().first;
			int col = Q.front().second;
			if(row>0&&field[row-1][col]&&bfs[row-1][col]==-1){
				bfs[row-1][col] = bfs[row][col] + 1;
				Q.push(make_pair(row-1,col));
			}
			if(row<a-1&&field[row+1][col]&&bfs[row+1][col]==-1){
				bfs[row+1][col] = bfs[row][col] + 1;
				Q.push(make_pair(row+1,col));
			}
			if(col>0&&field[row][col-1]&&bfs[row][col-1]==-1){
				bfs[row][col-1] = bfs[row][col] + 1;
				Q.push(make_pair(row,col-1));
			}
			if(col<b-1&&field[row][col+1]&&bfs[row][col+1]==-1){
				bfs[row][col+1] = bfs[row][col] + 1;
				Q.push(make_pair(row,col+1));
			}
			Q.pop();
		}
		ret += bfs[point[i+1].first][point[i+1].second];
	}
	cout << ret << endl;
}

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

const int INF = 2000000;
char fl[1010][1010];
int d[1010][1010];

int bfs(int sx, int sy, int gx, int gy, int mx, int my) {
	typedef pair<int, int>P;
	int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };

	for (int i = 0;i < mx;i++)
		for (int j = 0;j < my;j++)d[i][j] = INF;

	queue<P>que;
	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], ny = p.second + dy[i];

			if (0 <= nx&&nx < my && 0 <= ny&&ny < mx&&fl[nx][ny] != 'X'&&d[nx][ny] == INF) {
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
	return d[gx][gy];
}

int main() {
	int h, w, n;
	cin >> h >> w >> n;
	int al = 0;
	for (int i = 0;i < h;i++) {
		for (int j = 0;j < w;j++) { cin >> fl[i][j]; 
		}
	}
	char a[10] = { 'S','1','2','3','4','5','6','7','8','9' };
	for (int i = 0;i < n;i++) {
		int sx, sy;
		int gx, gy;
		for (int j = 0;j < h;j++) {
			for (int k = 0;k < w;k++) {

				if (fl[j][k]== a[i]) {
					sx = j;
					sy = k;
				}
				if (fl[j][k]== a[i+1]) {
					gx = j;
					gy = k;
				}
			}
		}

		al += bfs(sx, sy, gx, gy,w ,h);
	}
	cout << al << endl;
	return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map> //pair is also included in this library
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define REP(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define fst first
#define snd second
#define MP make_pair //incase c++11 or later is not available

using namespace std;

typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//prefer define statement
//for reason that const int ver. of INF will raise a silly warning (unused variable)
#define INF (INT_MAX/3)
#define LIM_INF (INT_MAX)
//const int INF = INT_MAX/3;

/*------------------------------------------------------------------------------*/

int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, -1, 0, 1};

PII s;

int bfs(vector<vector<char> > f, int H, int W, int n, PII sp){
  int res = 0;
  vector<VI > cost(H, VI(W, INF));
  int ini_y = sp.fst, ini_x = sp.snd;
  cost[ini_y][ini_x] = 0;

  queue<pair<PII, int> > q;
  q.push(MP(MP(ini_y, ini_x), 0));

  while(!q.empty()){
    pair<PII, int> p = q.front();
    q.pop();
    int y = p.fst.fst, x = p.fst.snd, c = p.snd;
    if(f[y][x] == (char)('0' + n)){
      res = c;
      s.fst = y;
      s.snd = x;
      break;
    } else {
      REP(i, 0, 4){
        int ny = y + my[i], nx = x + mx[i];
        if(ny >= 0 && ny < H && nx >= 0 && nx < W &&
           f[ny][nx] != 'X' && cost[ny][nx] > c + 1){
          cost[ny][nx] = c + 1;
          q.push(MP(MP(ny,nx), c + 1));
        }
      }
    }
  }

  return res;
}

int solve(vector<vector<char> > f, int H, int W, int N, PII p){
  int ans = 0;
  s = p;

  REP(i, 1, N+1){
    ans += bfs(f, H, W, i, s);
  }
  return ans;
}

int main(){
  int H, W, N;
  cin >> H >> W >> N;

  vector<vector<char> > f(H, vector<char>(W));
  int y, x;
  REP(i, 0, H){
    REP(j, 0, W){
      cin >> f[i][j];
      if(f[i][j] == 'S'){
        y = i;
        x = j;
      }
    }
  }

  cout << solve(f, H, W, N, MP(y, x)) << endl;
  return 0;
}

C++: //幅優先探索
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<memory>
#define FOR(i,R) for(int i=0;i<R;i++)
#define ZERO(a) memset(a,0,sizeof(a))
using namespace std;
typedef pair<int,int> P;

const int INF=10000;
//init()
char maze[1024][1024];
int H,W,N;
int sx,sy;
int gx,gy;

int e[16][3];
int d[1024][1024];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int bfs(){
	queue<P> que;
	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],ny=p.second+dy[i];
			if(0<=nx&&nx<H&&0<=ny&&ny<W&&maze[nx][ny]!='X'&&d[nx][ny]==INF){
				que.push(P(nx,ny));
				d[nx][ny]=d[p.first][p.second]+1;
			}
		}
	}
	return d[gx][gy];
}

void main(){
	int i,j,k=0,t=0;
	cin>>H>>W>>N;
	FOR(i,H)FOR(j,W){
		cin>>maze[i][j];
		if(maze[i][j]=='S'){
			sx=i;sy=j;
		}else if('0'<=maze[i][j]&&maze[i][j]<='9'){
			k=maze[i][j]-'0';
			e[k][0]=i;e[k][1]=j;
		}
	}
	
	FOR(i,N){
		if(i==0){
			gx=e[1][0];
			gy=e[1][1];
		}else{
			sx=e[i][0];
			sy=e[i][1];
			gx=e[i+1][0];
			gy=e[i+1][1];
		}
		t+=bfs();
	}
	cout<<t<<endl;
}