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 "stdio.h"
#include "queue"
#include "utility"
#include "climits"

using namespace std;

typedef pair<int, int>P;

int H, W, N;
char field[1001][1001];
int dis[1001][1001];
P target[10];
P start;
P Now;
P dir[4] = { make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1) };
queue<P>Q;
int ans;

int main() {
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 0; i < H; i++) {
		scanf("%s", field[i]);
	}
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (field[i][j] >= '1'&&field[i][j] <= '9') {
				target[field[i][j] - '0'].first = i;
				target[field[i][j] - '0'].second = j;
			}
			if (field[i][j] == 'S') {
				target[0].first = i;
				target[0].second = j;
			}
		}
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < H; j++) {
			for (int k = 0; k < W; k++) {
				dis[j][k] = INT_MAX;
			}
		}
		dis[target[i].first][target[i].second] = 0;
		Q.push(target[i]);
		while (!Q.empty()) {
			Now = Q.front();
			for (int j = 0; j < 4; j++) {
				if (Now.first + dir[j].first >= 0 && Now.first + dir[j].first < H&&Now.second + dir[j].second >= 0 && Now.second + dir[j].second < W) {
					if (field[Now.first + dir[j].first][Now.second + dir[j].second] != 'X') {
						if (dis[Now.first][Now.second] + 1 < dis[Now.first + dir[j].first][Now.second + dir[j].second]) {
							Q.push(make_pair(Now.first + dir[j].first, Now.second + dir[j].second));
							dis[Now.first + dir[j].first][Now.second + dir[j].second] = dis[Now.first][Now.second] + 1;
						}
					}
				}
			}
			Q.pop();
		}
		ans += dis[target[i + 1].first][target[i + 1].second];
	}
	printf("%d\n", ans);
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1000][1000],C[11]={"0123456789"};
int D[1000][1000];
int A[1000][1000],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		//printf("a");
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();
		A[S.y][S.x]=1;
		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		//printf("%d\n",A[S.y][S.x]);
		if(A[S.y][S.x]==1) continue;
		//printf("b");
		if(s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
			S.x-=x[l];
			S.y-=y[l];
		}
		else if(s[S.y][S.x]=='.'){}
		else{
			if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				printf("%d\n",S.Z);
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;
			}

		} 
		P.push(S);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int xv[]={0,1,0,-1};
int yv[]={1,0,-1,0};
char map[1002][1002];
int flag[1002][1002];
int main(){
	int H,W,N;
	cin>>H>>W>>N;
	
	for(int i=0;i<=W+1;i++)map[i][0]=map[i][H+1]='X';
	for(int i=0;i<=H+1;i++)map[0][i]=map[W+1][i]='X';
	int pos[10][2];
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			cin>>map[j][i];
			if(map[j][i]=='S')pos[0][0]=j,pos[0][1]=i;
			if('0'<map[j][i]&&map[j][i]<='9')pos[map[j][i]-'0'][0]=j,pos[map[j][i]-'0'][1]=i;
		}
	}
	int ans=0;
	for(int n=1;n<=N;n++){
		
		memset(flag,-1,sizeof(flag));
		queue<int>X;
		queue<int>Y;
		X.push(pos[n-1][0]);
		Y.push(pos[n-1][1]);
		flag[pos[n-1][0]][pos[n-1][1]]=0;
		for(int x,y;!X.empty();X.pop(),Y.pop()){
			x=X.front();
			y=Y.front();
			if(map[x][y]==(n+'0')){
				ans+=flag[x][y];
				break;
			}
			for(int i=0;i<4;i++){
				x=X.front()+xv[i];
				y=Y.front()+yv[i];
				if(map[x][y]!='X'&&flag[x][y]<0){
					flag[x][y]=(flag[X.front()][Y.front()]+1);
					X.push(x);
					Y.push(y);
				}
				
				
			}
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}

C++: /*
* @Author: key
* @Date:   2016-03-07 20:53:16
* @Last Modified by:   key
* @Last Modified time: 2016-03-07 22:13:15
*/

#include <iostream>
#include <queue>
#define MAX 1005
#define INF 100000000

using namespace std;

typedef struct{
    int x;
    int y;
    int state;
} Mouse;

int H,W,N;
int start_x,start_y;
char ar[MAX][MAX];
int offset_x[4] = {1,0,-1,0};
int offset_y[4] = {0,1,0,-1};
int ans[MAX][MAX];

void init(){
    for(int i = 0; i < H; i++)
        for(int j = 0; j < W; j++)
            ans[i][j] = -INF;
}

void bfs(){

    Mouse m;
    m.x = start_x;
    m.y = start_y;
    m.state = 1;

    init();

    ans[start_y][start_x] = 0;

    queue<Mouse> q;
    q.push(m);

    while(q.size()){
        Mouse m1 = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int dx = m1.x + offset_x[i];
            int dy = m1.y + offset_y[i];
            bool flag = true;
            if(dx >= 0 && dx < W && dy >= 0 && dy < H && ar[dy][dx] != 'X' && ans[dy][dx] == -INF){
                Mouse m2;
                m2.x = dx;
                m2.y = dy;

                ans[dy][dx] = ans[m1.y][m1.x] + 1;

                if(m1.state == (ar[dy][dx] - '0')){
                    int temp = ans[m1.y][m1.x] + 1;
                    init();
                    ans[dy][dx] = temp;
                    m2.state = m1.state + 1;

                    if(m1.state == N){
                        cout << ans[dy][dx] << endl;
                        return;
                    }
                    ar[dy][dx] == '.';
                    while(!q.empty()){
                        q.pop();
                    }
                    flag = false;
                }else{
                    m2.state = m1.state;
                }
                q.push(m2);
                if(!flag) break;
            }
        }
    }




}



int main(){

    while(cin >> H >> W >> N){
        for(int i = 0; i < H; i++){
            cin >> ar[i];
            for(int j = 0; j < W; j++){
                if(ar[i][j] == 'S'){
                    start_y = i;
                    start_x = j;
                }
            }
        }
        bfs();
        break;
    }

 return 0;
}