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 <cstdio>
#include <cstdlib>
#include <queue>

char map[1000][1000];
int check[1000][1000];
int cx[10];
int cy[10];

int main(){
	int i,j,k,l,h,w,ch,pd=0;
	int tex,tey,ted;
	std::queue<int> qx;
	std::queue<int> qy;
	std::queue<int> qd;

	scanf("%d %d %d",&h,&w,&ch);
	scanf("%c",&map[0][0]);
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++){
			scanf("%c",&map[i][j]);
			if(map[i][j]=='S'){cy[0]=i; cx[0]=j;}
			if(map[i][j]=='1'){cy[1]=i; cx[1]=j;}
			if(map[i][j]=='2'){cy[2]=i; cx[2]=j;}
			if(map[i][j]=='3'){cy[3]=i; cx[3]=j;}
			if(map[i][j]=='4'){cy[4]=i; cx[4]=j;}
			if(map[i][j]=='5'){cy[5]=i; cx[5]=j;}
			if(map[i][j]=='6'){cy[6]=i; cx[6]=j;}
			if(map[i][j]=='7'){cy[7]=i; cx[7]=j;}
			if(map[i][j]=='8'){cy[8]=i; cx[8]=j;}
			if(map[i][j]=='9'){cy[9]=i; cx[9]=j;}
		}
	}
	for(i=0;i<h;i++){
		for(j=0;j<=w;j++)check[i][j]=0;
	}

	i=0;
	while(i<ch){
		qx.push(cx[i]); qy.push(cy[i]); qd.push(0);
		++i;
		while(!qx.empty()){
			tex=qx.front(); tey=qy.front(); ted=qd.front();
			printf("%d,%d,%d\n",tex,tey,ted);
			qx.pop(); qy.pop(); qd.pop();
			check[tey][tex]=1;
			if(tey==cy[i] && tex==cx[i]){
				pd+=ted;
				while(!qx.empty())qx.pop();
				while(!qy.empty())qy.pop();
				while(!qd.empty())qd.pop();
				for(k=0;k<h;k++){
					for(l=0;l<=w;l++)check[k][l]=0;
				}
				printf("%d\n",pd);
				break;
			}
			++ted;
			if(tex+1<w && map[tey][tex+1]!='X' && check[tey][tex+1]==0){
				check[tey][tex+1]=1;
				qx.push(tex+1); qy.push(tey); qd.push(ted);
			}
			if(tex-1>=0 && map[tey][tex-1]!='X' && check[tey][tex-1]==0){
				check[tey][tex-1]=1;
				qx.push(tex-1); qy.push(tey); qd.push(ted);
			}
			if(tey+1<h && map[tey+1][tex]!='X' && check[tey+1][tex]==0){
				check[tey+1][tex]=1;
				qx.push(tex); qy.push(tey+1); qd.push(ted);
			}
			if(tey-1>=0 && map[tey-1][tex]!='X' && check[tey-1][tex]==0){
				check[tey-1][tex]=1;
				qx.push(tex); qy.push(tey-1); qd.push(ted);
			}
		}
	}

	printf("%d\n",pd);
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;



int main(){
  int h,w,n,ans;
  scanf("%d%d%d",&h,&w,&n);
  
  char tem[h][w];
  int sx, sy;
  for(int i=0;i<h;i++){
    scanf("%s", tem[i]);
  for(int j=0;j<w;j++){
    if('S'==tem[i][j]){sx=i; sy=j;}
  }
  }
  // printf("%d %d\n",sx,sy);
ans=0;
for(int j=1;j<=n;j++){
  // printf("%d %d %d\n",sx,sy,ans);
    queue<int> colx;
    queue<int> coly;
    queue<int> colt;
    queue<int> hx;
    queue<int> hy;
    queue<char> hc;
    colx.push(sx);
    coly.push(sy);
    colt.push(ans);
   // printf("%d %d\n",sx,sy);
  while(1){
    int x, y, time;
    x=colx.front();
    y=coly.front();
    time=colt.front();
    colx.pop();
    coly.pop();
    colt.pop();

    // printf("%d %d?????¢?´¢\n",x,y);
    // printf("%d????????????????????????%d???\n", j, time);

    //???
    if(x!=0){
      if(((int)tem[x-1][y]-48)==j){
sx=x-1;sy=y;ans=time+1;
 while(!hx.empty()){
      tem[hx.front()][hy.front()]=hc.front();
      hx.pop();hy.pop();hc.pop();
}
 break;
}
      else if(tem[x-1][y]!='X'&&tem[x-1][y]!='@'){
        colx.push(x-1);
        coly.push(y);
        colt.push(time+1);
    hx.push(x-1);
    hy.push(y);
    hc.push(tem[x-1][y]);
    tem[x-1][y]='@';
      }
    }
      //???
    if(y!=0){
      if(((int)tem[x][y-1]-48)==j){sx=x;sy=y-1;ans=time+1;  
	while(!hx.empty()){
      tem[hx.front()][hy.front()]=hc.front();
      hx.pop();hy.pop();hc.pop();
} break;}
      else if(tem[x][y-1]!='X'&&tem[x][y-1]!='@'){
        colx.push(x);
        coly.push(y-1);
        colt.push(time+1);
    hx.push(x);
    hy.push(y-1);
    hc.push(tem[x][y-1]);
    tem[x][y-1]='@';
      }
    }
      //???
    if(y!=w-1){
      if(((int)tem[x][y+1]-48)==j){sx=x;sy=y+1;ans=time+1;     
	while(!hx.empty()){
      tem[hx.front()][hy.front()]=hc.front();
      hx.pop();hy.pop();hc.pop();
} break;}
      else if(tem[x][y+1]!='X'&&tem[x][y+1]!='@'){
        colx.push(x);
        coly.push(y+1);
        colt.push(time+1);
    hx.push(x);
    hy.push(y+1);
    hc.push(tem[x][y+1]);
    tem[x][y+1]='@';
      }
    }
      //???
    if(x!=h-1){
      if(((int)tem[x+1][y]-48)==j){sx=x+1;sy=y;ans=time+1;    
	while(!hx.empty()){
      tem[hx.front()][hy.front()]=hc.front();
      hx.pop();hy.pop();hc.pop();
}
 break;
}
      else if(tem[x+1][y]!='X'&&tem[x+1][y]!='@'){
        colx.push(x+1);
        coly.push(y);
        colt.push(time+1);
    hx.push(x+1);
    hy.push(y);
    hc.push(tem[x+1][y]);
    tem[x+1][y]='@';
      }
    }
  }
}

printf("%d\n",ans);

return 0;


}

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

#define mp make_pair
#define f first
#define s second

using namespace std;

typedef pair<int,int> P;

const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int h,w;
string grid[1000];
int d[1000][1000];
P v[1001];

int bfs(P S,int n){

  queue<P>que;
  que.push(S);
  d[S.f][S.s]=0;

  while(!que.empty()){
    P now=que.front();
    que.pop();

    for(int i=0;i<4;i++){
      if(0<=now.f+dx[i] && 0<=now.s+dy[i] &&
	 now.f+dx[i]<h && now.s+dy[i]<w
	 && grid[now.f+dx[i]][now.s+dy[i]]!='X'){

	if(d[now.f][now.s]+1<d[now.f+dx[i]][now.s+dy[i]]){
	  d[now.f+dx[i]][now.s+dy[i]]=d[now.f][now.s]+1;
	  que.push(P(now.f+dx[i],now.s+dy[i]));
	}
      }
    }
  }
  return d[v[n+1].f][v[n+1].s];
}

int main(void){
  int n;

  cin >> h >> w >> n;

  for(int i=0;i<h;i++)cin >> grid[i];

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(grid[i][j]=='S')v[0]=mp(i,j);
      if(grid[i][j]!='X'&&grid[i][j]!='.')
	v[grid[i][j]-'0']=mp(i,j);
    }
  }

  int ans=0;
  for(int i=0;i<n;i++){
    for(int j=0;j<h;j++)
      for(int k=0;k<w;k++)d[j][k]=100000000;
    ans+=bfs(v[i],i);
  }

  cout << ans << endl;

  return 0;
}