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<iomanip>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<complex>
#include<set>
#include<map>
#include<list>
#include<vector>
#include<deque>
using namespace std;
typedef long long ll;
typedef vector<int>vec;
typedef vector<vec>mat;
const int INF = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const double eps = 1e-7;
int dirX[4] = { 1,0,-1,0 };
int dirY[4] = { 0,1,0,-1 };
struct point {
	int x; int y; 
	point() { x = y =0; }
	point(int X, int Y) { x = X; y = Y; }
	point(const point &p) { x = p.x; y = p.y;  }
	bool operator==(const point&C) {
		return{x==C.x&&y==C.y};
	}
};
struct cheese :public point {
	int hard;
	cheese() { x = y = hard = 0; }
	cheese(int X, int Y, int H) { x = X; y = Y; hard = H; }
	bool operator<(const cheese&C) {
		return hard < C.hard;
	}
};
int H, W, N;
bool a[1001][1001]; bool v[1001][1001];//here???
vector<cheese>goal;
bool isFound=0;;
void dfs(int t,point P,int curD,int maxD){
	if(v[P.x][P.y])return;
	v[P.x][P.y]=1;
	if(P==goal[t+1]){
		isFound=1;
		return;
	}
	else if(curD<maxD){
		for(int i=0;i<4;i++){
			int tx = P.x + dirX[i];int ty = P.y + dirY[i];
			if (tx >= 1 && tx <= H&&ty >= 1 && ty <= W&&a[tx][ty]==0&&v[tx][ty] == 0)
						dfs(t,point(tx,ty),curD+1,maxD);
		}
	}
	v[P.x][P.y]=0;
	return;
}
int main() {
	ifstream ifs;
	ifs.open("D:\\documents\\programDatas\\AOJ0558.txt");
	cin >> H >> W >> N;
	memset(a, 0, sizeof(a)); memset(v, 0, sizeof(v));
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			char temp;
			scanf("%c",&temp);
			if(temp=='\n')scanf("%c",&temp);
			if (temp == 'S')goal.push_back(cheese(i, j, 0));
			else if (temp >= '1'&&temp <= '9')goal.push_back(cheese(i, j, (int)(temp - 48)));
			else if (temp == '.')a[i][j] = 0;
			else if (temp == 'X')a[i][j] = 1;
		}
	}
	sort(goal.begin(), goal.end());
	//for debug
	/*for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			cout << setw(3) << a[i][j];
		}
		cout << endl;
	}
	for (int i = 0; i <= N; i++) {
		cout << goal[i].x << " " << goal[i].y << " " << goal[i].hard << " " << endl;
	}
	cout << endl;
	*/
	//IDS
	int total = 0;//total steps
	for (int t = 0; t < N; t++) {//x for start point and x+1 for end point
		int maxD=abs(goal[t+1].x-goal[t].x)+abs(goal[t+1].y-goal[t].y);
		while(1){
			dfs(t,goal[t],0,maxD);
			if(isFound){
				isFound=0;//reset
				break;
			}
			maxD++;
		}
		total+=maxD;
		memset(v,0,sizeof(v));
	}
	cout << total << endl;
	return 0;
}

C++: # include<bits/stdc++.h>
using namespace std;

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

int N,M;
int sx,sy;
int gx,gy;
char maze[1005][1005];

int d[1005][1005];

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

int bfs(){
    queue<P> que;
    for (int i=0;i<=N;i++)
        for (int j=0;j<=M;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 < N && 0<= ny && ny < M && 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];
}

int main(){
    int res = 0;
    int num;
    cin >> N >> M >> num;
    for (int i =0;i<N;i++){
        cin >> maze[i];
    }
    int checkx[10],checky[10];
    for (int i = 0; i< N; i++){
        for (int j = 0; j< M; j++){
            if (maze[i][j]=='S') {checkx[0]=i;checky[0]=j;}
            if (maze[i][j]=='1') {checkx[1]=i;checky[1]=j;}
            if (maze[i][j]=='2') {checkx[2]=i;checky[2]=j;}
            if (maze[i][j]=='3') {checkx[3]=i;checky[3]=j;}
            if (maze[i][j]=='4') {checkx[4]=i;checky[4]=j;}
            if (maze[i][j]=='5') {checkx[5]=i;checky[5]=j;}
            if (maze[i][j]=='6') {checkx[6]=i;checky[6]=j;}
            if (maze[i][j]=='7') {checkx[7]=i;checky[7]=j;}
            if (maze[i][j]=='8') {checkx[8]=i;checky[8]=j;}
            if (maze[i][j]=='9') {checkx[9]=i;checky[9]=j;}
        }
    }

    for (int i =0;i<num;i++){
        sx = checkx[i];
        sy = checky[i];
        gx = checkx[i+1];
        gy = checky[i+1];
        res += bfs();
    }
    cout << res << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>Pt;
#define fr first
#define sc second
Pt st[10];
int h,w,n,dx[]={0,1,0,-1},dy[]={1,0,-1,0},used[1000][1000];
char mas[1000][1000],tmp[]="S123456789";
int bfs(Pt,Pt);
int main(){
  cin >> h >> w >> n;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mas[i][j];
      for(int k=0;k<=n;k++){
	if(mas[i][j] == tmp[k]) st[k] = Pt(i,j);
      }
    }
  }
  int cnt = 0;
  for(int i=1;i<=n;i++){
    memset(used,-1,sizeof(used));
    cnt += bfs(st[i-1],st[i]);
  }
  cout << cnt << endl;
}

int bfs(Pt S,Pt G){
  queue<Pt> que;
  que.push(S);
  used[S.fr][S.sc] = 0;
  while(!que.empty()){
    Pt p = que.front();
    que.pop();
    if(p == G) break;
    for(int i=0;i<4;i++){
      int nx = p.fr + dx[i],ny = p.sc + dy[i];
      if(nx>=0 && nx<h && ny>=0 && ny<w && used[nx][ny]==-1 && mas[nx][ny]!='X'){
	used[nx][ny] = used[p.fr][p.sc] + 1;
	que.push(Pt(nx,ny));
      }
    }
  }
  return used[G.fr][G.sc];
}