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 <queue>
#include <cstring>
using namespace std;
#define MAX 1005

char JOI[MAX][MAX];
int h,w,n;
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};

pair<int, int> goal[10];
bool check(int a,int b){
    return (a>=1&&a<=h&&b>=1&&b<=w);
}

int step[MAX][MAX];
int bfs(pair<int, int> st, pair<int, int> en){
    queue<pair<int,int> > q;
    q.push(st);
    memset(step, -1, sizeof(step));
    step[st.first][st.second]=0;
    while(!q.empty()){
        pair<int, int> S=q.front(); q.pop();
        int x=S.first;  int y=S.second;
        int stp=step[x][y];
        for(int i=0; i<4; i++){
            int cx=x+dx[i]; int cy=y+dy[i];
            if(check(cx, cy)){
                if(JOI[cx][cy]!='X'&&step[cx][cy]==-1){
                    if(cx==en.first&&cy==en.second) return stp+1;
                    q.push(make_pair(cx, cy));
                    step[cx][cy]=stp+1;
                }
            }
        }
    }
}
int main(){
    cin>>h>>w>>n;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            cin>>JOI[i][j];
            if(JOI[i][j]=='S') goal[0]=make_pair(i, j);
            else if(JOI[i][j]>='0'&&JOI[i][j]<='9'){
                int x=JOI[i][j]='0';
                goal[x]=make_pair(i, j);
            }
        }
    }
    int ans=0;
    for(int i=0; i<n; i++){
        ans+=bfs(goal[i],goal[i+1]);
    }
    cout<<ans<<endl;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
//#include <map>
#include <set>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef pair<int, int> P;


typedef struct
{
	int i;
	int j;
	int hp;
} STATE;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

char map[1024][1024];
int H, W, N;
int si, sj, gi, gj;
int d[1024][1024][10];

int search()
{
	queue<STATE> que;
	
	for (int i = 1; i <= H; i++){
		for (int j = 1; j <= W; j++){
			for (int k = 0; k <= N; k++){
				d[i][j][k] = INF;
			}
		}
	}
	
	STATE tmp = {si, sj, 0};
	que.push(tmp);
	d[si][sj][0] = 0;
	
	while (que.size()){
		STATE st = que.front();
		que.pop();
		
		printf("%d %d %d\n", st.i, st.j, st.hp);
		
//		if (st.hp == N) return (d[st.i][st.j][st.hp]);
		if (st.hp == N) break;
				
		for (int i = 0; i < 4; i++){
			int ni = st.i + dy[i];
			int nj = st.j + dx[i];
			int nhp = st.hp + (st.hp == map[ni][nj]);
			
			if (map[ni][nj] != 'X' && d[ni][nj][nhp] == INF){
				STATE tmp = {ni, nj, nhp};
				que.push(tmp);
				d[ni][nj][nhp] = d[st.i][st.j][st.hp] + 1;
			}
		}
	}
	
	return (d[gi][gj][N]);
}

int main()
{
	for (int i = 0; i < 1024; i++){
		for (int j = 0; j < 1024; j++){
			map[i][j] = 'X';
		}
	}
	
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 1; i <= H; i++){
		for (int j = 1; j <= W; j++){
			scanf(" %c", &map[i][j]);
			
			if (map[i][j] == 'S'){
				si = i;
				sj = j;
			}
			if (map[i][j] == '0' + N){
				gi = i;
				gj = j;
			}
			if (map[i][j] >= '1' && map[i][j] <= '9'){
				map[i][j] -= '1';
			}
		}
	}
	
	printf("%d\n", search());
}

C++: #include <iostream>
#include <stdlib.h>
#include <queue>
#include <cstring>
#define MAX_NUM 1010
using namespace std;
int H, W, N;
int flag[MAX_NUM][MAX_NUM] = {0};    

class Node
{
public:
	Node();
	Node(int x, int y);
	~Node();
	int x;
	int y;
};

Node::Node()
{
}

Node::Node(int x, int y) {
	this->x = x;
	this->y = y;
}

Node::~Node()
{
}
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int BFS(char map[][MAX_NUM], int sx, int sy, int ex, int ey) {
	queue<Node*> que;
	que.push(new Node(sx, sy));
	flag[sx][sy] = 0;                                                    //将改位置???已遍?
	while (!que.empty())
	{
		Node* cur = que.front();
		que.pop();
		if (cur->x == ex && cur->y == ey)
		{
			return flag[cur->x][cur->y];
		}
		else
		{
			for (int i = 0; i < 4; i++)                                    //将?位置的四个方向可以
			{
				int k = cur->x + dir[i][0];
				int v = cur->y + dir[i][1];
				if (k>=0&&k<H&&v>=0&&v<W&&map[k][v]!='X'&&!flag[k][v])
				{
					que.push(new Node(k, v));
					flag[k][v] = flag[cur->x][cur->y] + 1;
				}
			}
		}
	}
	return -1;

}

int main()
{
	int sx, sy, arr[10][2];
	char map[MAX_NUM][MAX_NUM];
	while (cin >> H >> W >> N && H && W)
	{
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				cin >> map[i][j];
				if (map[i][j] == 'S')
				{
					map[i][j] = '.';
					sx = i;
					sy = j;
				}
				if (isdigit(map[i][j]))
				{
					int index = map[i][j] - '0';
					arr[index][0] = i;
					arr[index][1] = j;
				}
			}
		}
		memset(flag, 0, sizeof(flag));
		int res = 0;
		res += BFS(map, sx, sy, arr[1][0], arr[1][1]);
		for (int i = 1; i+1 <= N; i++)
		{
			memset(flag, 0, sizeof(flag));
			res += BFS(map, arr[i][0], arr[i][1], arr[i + 1][0], arr[i + 1][1]);
		}
		cout << res << endl;
	}
	return 0;
}

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


using namespace std;

const int INF = -1;

typedef pair<int, int> P;

const int MAX_H = 1000;
const int MAX_W = 1000;
char field[MAX_H][MAX_W + 1];
int d[MAX_H][MAX_W];
int H, W, N;
int sx, sy;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int bfs(){

  int time = 0;

  for (int i = 1; i <= N; i++) {

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; 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 (field[p.first][p.second]-'0' == i) {
          time += d[p.first][p.second];
          sx = p.first;
          sy = p.second;
          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 && field[nx][ny] != 'X'&& d[nx][ny] == INF){
            que.push(P(nx, ny));
            d[nx][ny] = d[p.first][p.second] + 1;
          }
        }
      }
  }
  return time;
}


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

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S'){
        sx = i;
        sy = j;
      }
    }
  }

  cout << bfs() << endl;
}