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 <iostream>
#include <queue>
#define ROP(i,n) for(int i=0;i<n;i++)
#define ROPP(i,n) for(int i=1;i<=n;i++)
#define INF 999999999
using namespace std;
typedef pair<int,int> P;
int N,M,G,gx[10],gy[10];
char MAP[100][100+1];
int DX[4]={1,0,-1,0},DY[4]={0,1,0,-1},FAR[100][100+1];
int BFS(int u){
  queue<P> que;
  ROPP(i,N){ROPP(j,M) FAR[j][i]=INF;}
  que.push(P(gx[u],gy[u]));
  FAR[gx[u]][gy[u]]=0;
  while(que.size()){
    P p=que.front();que.pop();
    if(p.first==gx[u+1]&&p.second==gy[u+1]) break;
    ROP(i,4){
      int nx=p.first+DX[i],ny=p.second+DY[i];
      if(1<=nx&&nx<=M&&1<=ny&&ny<=N
        &&MAP[nx][ny]!='X'&&FAR[nx][ny]==INF){
        que.push(P(nx,ny));
        FAR[nx][ny]=FAR[p.first][p.second]+1;
      }
    }
  }
  return FAR[gx[u+1]][gy[u+1]];
}
void solve(){
  int res=0;
  ROP(i,G){
    res+=BFS(i);
  }
  printf("%d\n",res);
}
int main(){
  scanf("%d %d %d",&N,&M,&G);
  ROPP(i,N){
    ROP(j,M+1){
      scanf("%c",&MAP[j][i]);
      if(MAP[j][i]=='S') gx[0]=j,gy[0]=i;
      ROPP(k,G) {char l='0'+k;
      if(MAP[j][i]==l) gx[k]=j,gy[k]=i;}
    }
  }
  solve();
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#define MAX 1000

using namespace std;

const int INF = 1000000;

typedef pair<int,int> P;

int n,m;
char cheese[MAX+1][MAX+1];
 
int bfs(int sy,int sx,int gy,int gx){
    
  int dx[4]={1,0,-1,0};
  int dy[4]={0,1,0,-1};
  int d[MAX][MAX];
  queue<P> q;
     
    for( int i = 0 ; i < n ; i++ )
        for( int j = 0 ; j < m ; j++ )
            d[i][j] = INF;
     
    q.push( P(sy , sx) );
    d[sy][sx] = 0;
     
    while( q.size() ){
        P p = q.front(); 
	q.pop();

        if(p.first == gy && p.second == gx) break;
	
        for(int i = 0 ; i < 4 ; i++){
	  int x = p.first + dx[i];
	  int y = p.second + dy[i];
          
	  if( 0 <= x && x < n &&
	      0 <= y && y < m &&
	      d[x][y] == INF &&
	      cheese[x][y]!='X'){
	    q.push( P(x , y) );
	    d[x][y] = d[p.first][p.second] + 1;
	  }
        }
    }
    
    return d[gy][gx];
}

int main(void){

    int num;
 
    cin >> n >> m >> num;
    vector<P> v(num + 1);
     
    for( int i = 0 ; i < n ; i++ ){
        for( int j = 0 ; j < m ; j++ ){
            cin >> cheese[i][j];
            if( cheese[i][j]=='S'){
                v[0].first = i;
                v[0].second = j;
            }
            if('1' <= cheese[i][j] && cheese[i][j] <= '9'){
                v[cheese[i][j]-'0'].first = i;
                v[cheese[i][j]-'0'].second = j;
            }
        }
    }
     
    int sum = 0;
    for(int i = 1 ; i < num+1 ; i++){
        sum += bfs(v[i-1].first,v[i-1].second,v[i].first,v[i].second);
    }
    cout << sum << endl;
     
    return 0;
}

C++: 

#include <iostream>
#include <utility>
#include <queue>
using namespace std;

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W, N;

const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int d[MAX_H][MAX_W];
int mytime;

void bfs() {
	queue<Pair> que;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			d[i][j] = INF;
		}
	}
	char muscle = '1';
	que.push(Pair(sx, sy));
	d[sx][sy] = 0;
	while (que.size()) {
		Pair p = que.front(); que.pop();
		if (int(muscle) - 48 - 1 == N) 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 && plant[nx][ny] != 'X' && d[nx][ny] == INF) {
				que.push(Pair(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
			if (muscle == plant[nx][ny]) {
				while (que.size()) { que.pop(); }
				que.push(Pair(nx, ny));
				muscle = muscle + 1;
				mytime += d[nx][ny];
				for (int i = 0; i < H; i++) {
					for (int j = 0; j < W; j++) {
						d[i][j] = INF;
					}
				}
				d[nx][ny] = 0;
				break;
			}
		}
	}
}

void solve() {
	mytime = 0;
	bfs();
	//printf("%d\n", mytime);
	 cout << mytime << endl;

}

int main() {
	 cin >> H >> W >> N;
	//scanf("%d%d%d", &H, &W, &N);
	for (int i = 0; i < H; i++) {
		//scanf("%s", plant[i]);
		for (int j = 0; j < W; j++) {
			   cin >> plant[i][j];
			if (plant[i][j] == 'S') { sx = i; sy = j; }
		}
	}
	solve();
	system("pause");
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<utility>
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
int d[1020][1020]={{0}};
char a[1020][1020]={{0}};
int h,w,x,y,z=0,ans=0;
char life = '1',n;
using namespace std;
struct P{
    int x;
    int y;
    int z;
};
void bfs(int x,int y,int ans);
void reset();
int main(){

    cin>>h>>w>>n;
    reset();

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>a[i][j];
            if(a[i][j]=='S'){
                x = i;
                y = j;
            }
        }
    }
    bfs(x,y,0);



    return 0;
}
void bfs(int x,int y,int ans){
    queue <P> q;
    P p;
    int Q = 1,z = 0;

    p.x = x;
    p.y = y;
    p.z = z;
    q.push(p);
    d[x][y]=0;
    while(!q.empty()){
        p = q.front();
        if(p.x==-1){
            q.pop();
            Q++;
            continue;
        }/*
        cout<<"----"<<endl;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cout<<" ";
                if(d[i][j]==100000)cout<<'x';
                else cout<<d[i][j];
            }
            cout<<"  "<<z<<endl;
        }
        cout<<endl;
*/
        q.pop();
        x = p.x;
        y = p.y;
        z = p.z;
        if(a[x][y]==life){

            ans += z;
            if(life==n){
                cout<<ans<<endl;
                return;
            }
            life++;
            reset();
            d[x][y]=0;
            bfs(x,y,ans);
            return;
        }
        else{
            d[x][y] = z;
        }
        if(x!=0&&d[x+dx[0]][y+dy[0]]==100000&&a[x+dx[0]][y+dy[0]]!='X'){
            p.x = x+dx[0],p.y = y+dy[0],p.z = z+1;
            q.push(p);
        }
        if(y!=w-1&&d[x+dx[1]][y+dy[1]]==100000&&a[x+dx[1]][y+dy[1]]!='X'){
            p.x = x+dx[1],p.y = y+dy[1],p.z = z+1;
            q.push(p);
        }
        if(x!=h-1&&d[x+dx[2]][y+dy[2]]==100000&&a[x+dx[2]][y+dy[2]]!='X'){
            p.x = x+dx[2],p.y = y+dy[2],p.z = z+1;
            q.push(p);
        }
        if(y!=0&&d[x+dx[3]][y+dy[3]]==100000&&a[x+dx[3]][y+dy[3]]!='X'){
            p.x = x+dx[3],p.y = y+dy[3],p.z = z+1;
            q.push(p);
        }
        p.x = p.y = p.z = -1;
        q.push(p);
    }
    return;

}
void reset(){
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            d[i][j]=100000;
        }
    }
    return;
}