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 <algorithm>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#define MAXV  10001
using namespace std;
char map[1017][1017];
int m,n,x,d[1017][1017],drc[4][2]={-1,0,1,0,0,1,0,-1};
typedef pair<int,int> Point;
int bfs(int sx,int sy,int ex,int ey)
{
    queue<Point> que;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            d[i][j]=INF;
    d[sx][sy]=0;
    que.push(Point(sx,sy));
    while(!que.empty())
    {
        //cout<<"in"<<endl;
        Point cur = que.front(); que.pop();
        if(cur.first==ex&&cur.second==ey)
        {
            map[cur.first][cur.second]='.';
            break;
        }
        for(int i=0;i<4;i++)
        {
            //cout<<"here"<<endl;
            int nx = cur.first+drc[i][0];
            int ny = cur.second+drc[i][1];

            if((d[nx][ny]==INF)&&map[nx][ny]!='X')
            {
                d[nx][ny]=d[cur.first][cur.second]+1;
                //if(!(nx<0||nx>m-1||ny<0||ny<n-1))
                que.push(Point(nx,ny));
            }
        }
    }
    return d[ex][ey];
}
int main()
{
    memset(d,0,sizeof(d));
    scanf("%d%d%d",&m,&n,&x);
    bool start = false,ed = false;
    int ex[11]={0},ey[11]={0},ans=0,q=1,p=49;
    for(int i =0;i<m;i++)
            scanf("%s",map[i]);
    for(q=1;q<=x;q++,p++)
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            {
                if(map[i][j]=='S')
                {
                    ex[0]=i;
                    ey[0]=j;
                }
                if(map[i][j]==p)
                {
                    ex[q]=i;
                    ey[q]=j;
                }
            }
    for(int i=0;i<x;i++)
        {
            ans+=bfs(ex[i],ey[i],ex[i+1],ey[i+1]);
            //cout<<i<<ex[i]<<"  "<<ey[i]<<endl;
            //cout<<ans<<endl;
            //cout<<i<<endl;
        }
    cout<<ans<<endl;
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int W, H, N;
typedef pair<int, int> P;
queue<P> que;
char maze[1003][1003];
int d[1003][1003];
int pos_x, pos_y;
int sh_x[4] = {0, 0, -1, 1};
int sh_y[4] = {1, -1, 0, 0};
int res = 0;
int strength = 1;

void clear(queue<P> &q)
{
   queue<P> empty;
   swap(q, empty);
}

void solve() {
  que.push(P(pos_x, pos_y));
  memset(d, -1, sizeof(d));
  d[pos_y][pos_x] = 0;

  while(que.size()) {
    P pos = que.front(); que.pop();
    if (maze[pos.second][pos.first] - '0' == strength) {
      //printf("[%d] [%d] %d\n", pos.second, pos.first, res);
      clear(que);
      res += d[pos.second][pos.first];

      strength++;
      if (strength == N+1) {
        break;
      }

      //for (int i=0;i<H;i++) {
      //  for (int j=0;j<W;j++) {
      //    if (maze[i][j] == '#') maze[i][j] = '.';
      //  }
      //}
      memset(d, -1, sizeof(d));
      d[pos.second][pos.first] = 0;

      //while(que.size()) que.pop();
      //maze[pos.second][pos.first] = '#';
      que.push(pos);
      continue;
    }
    for (int i=0;i<4;i++) {
      int new_x = pos.first + sh_x[i];
      int new_y = pos.second + sh_y[i];
      if (new_x >= 0 && new_x < W && new_y >=0 && new_y < H && maze[new_y][new_x] != 'X' && d[new_y][new_x] == -1) {
        //printf("Go to [%d] [%d]\n", new_y, new_x);
        que.push(P(new_x, new_y));
        //if (maze[new_y][new_x] == '.') {
        //  maze[new_y][new_x] = '#';
        //}
        d[new_y][new_x] = d[pos.second][pos.first] + 1;
      }
    }
  }
}

int main() {
  while (1)
  {
    scanf("%d %d %d", &H, &W, &N);
    getchar();
    if (!W && !H) {
      break;
    }
    
    for (int i = 0; i < H; i++)
    {
      fgets(maze[i], 1002, stdin);
    }

    for (int i=0;i<H;i++) {
      for (int j=0;j<W;j++) {
        if (maze[i][j] == 'S') {
          pos_x = j;
          pos_y = i;
        }
      }
    }

    solve();
    printf("%d\n", res);
    strength = 1;
    res = 0;
  }
  return 0;
}

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

typedef pair<int, int> p;
typedef pair<p, int> dat;
const int size = 1000;

int main(){
	int H, W, N;
	int sx, sy, nx, ny, c, res;
	char field[size + 1][size + 1];
	bool checked[size][size];
	queue<dat> que;
	p point, P, goal;
	dat save;
	
	scanf("%d%d%d", &H, &W, &N);
	for(int i = 0; i < H; i++) scanf("%s", field[i]);
	
	for(int y = 0; y < H; y++){
		for(int x = 0; x < W; x++){
			if(field[y][x] == 'S'){
				point.first = x; point.second = y;
				break;
			}
		}
	}
	
	res = 0;
	for(int i = 0; i < N; i++){
		for(int y = 0; y < H; y++){
			for(int x = 0; x < W; x++){
				if(field[y][x] == i + 49){
					goal.first = x; goal.second = y;
					break;
				}
			}
		}
		for(int y = 0; y < H; y++){
			for(int x = 0; x < W; x++){
				checked[y][x] = false;
			}
		}
		
		save.first = point; save.second = 0;
		que.push(save);
		while(!que.empty()){
			dat d = que.front(); que.pop();
			P = d.first; c = d.second;
			
			nx = P.first; ny = P.second;
			
			if(nx == goal.first && ny == goal.second){
				res += c;
				while(!que.empty()) que.pop();
				break;
			}
			//up
			if(ny > 0 && !checked[ny - 1][nx] && field[ny - 1][nx] != 'X'){
				checked[ny - 1][nx] = true;
				point.first = nx; point.second = ny - 1;
				save.first = point; save.second = c + 1;
				que.push(save);
			}
			//down
			if(ny + 1 < H && !checked[ny + 1][nx] && field[ny + 1][nx] != 'X'){
				checked[ny + 1][nx] = true;
				point.first = nx; point.second = ny + 1;
				save.first = point; save.second = c + 1;
				que.push(save);
			}
			//left
			if(nx > 0 && !checked[ny][nx - 1] && field[ny][nx - 1] != 'X'){
				checked[ny][nx - 1] = true;
				point.first = nx - 1; point.second = ny;
				save.first = point; save.second = c + 1;
				que.push(save);
			}
			//right
			if(nx + 1 < W && !checked[ny][nx + 1] && field[ny][nx + 1] != 'X'){
				checked[ny][nx + 1] = true;
				point.first = nx + 1; point.second = ny;
				save.first = point; save.second = c + 1;
				que.push(save);
			}
		}
		
		point.first = goal.first; point.second = goal.second;
	}
	
	printf("%d\n", res);
	
	return 0;
}