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>
using namespace std;
int q[1000001][3];
bool vis[1001][1001];
char c[1001][1001];
int main(){
	int h,w,n;
	cin>>h>>w>>n;
	int x,y;
	int ex,ey;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>c[i][j];
			if(c[i][j]=='S'){
				y=i;
				x=j;
			}
			if(c[i][j]=='1'){
				ey=i;
				ex=j;
			}
			if(c[i][j]=='X'){
				vis[i][j]=false;
			}
			else{
				vis[i][j]=true;
			}
		}
	}
	int ans=0;
	for(int r=0;r<n;r++){
	char skey=r+'0';
	char dkey=r+'1';
	if(r>0){
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]==skey){
					y=i;
					x=j;
				}
				if(c[i][j]==dkey){
					ey=i;
					ex=j;
				}
				if(c[i][j]=='X'){
					vis[i][j]=false;
				}
				else{
					vis[i][j]=true;
				}
			}
		}
	}
	int head=0,tail=1;
	q[0][0]=x;
	q[0][1]=y;
	q[0][2]=0;
	vis[y][x]=false;
	int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
	int cnt=0;
	while(true){
		int x=q[head][0];
		int y=q[head][1];
		int len=q[head][2];
		if(x==ex&&y==ey){
			cnt=len;
			break;
		}
		for(int i=0;i<4;i++){
			if(x+dx[i]>=0&&x+dx[i]<w&&y+dy[i]>=0&&y+dy[i]<h){
				if(vis[y+dy[i]][x+dx[i]]==true){
					q[tail][0]=x+dx[i];
					q[tail][1]=y+dy[i];
					q[tail][2]=len+1;
					tail++;
					vis[y+dy[i]][x+dx[i]]=false;
				}
			}
		}
		head++;
	}
	ans+=cnt;
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include<iostream>
#include<cctype>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cassert>
#include<cctype>
#include<cstring>
#include<utility>
#include<cmath>
const int inf=0x7fffffff;
typedef long long int ll;
using namespace std;
const bool test=0;
int n,w,h;
int const N=1000+10;
char A[N][N];
char V[N][N];
char s[N];
int _h,_w;
int mov[2][4]={
	0,0,-1,1,
	-1,1,0,0
};
void show(){
	if(test){
		for(int i=1;i<=_h;i++){
			for(int j=1;j<=_w;j++){
				printf("%2d",A[i][j]);
			}
			printf("\n");
		}

	}
}
const int H=0,W=1;
struct point{
	int h;
	int w;
	int cnt;
	point(int h=1,int w=1,int cnt=inf):h(h),w(w),cnt(cnt){}
};
int cor(char x,int i,int j){
	switch(x){
		case 'S':
			h=i;
			w=j;
			return 0;
			break;
		case 'X':
			return -1;
		case '.':
			return 0;
		default:
			return x-'0';
	}
}
int search(int goal){
	memset(V,0,sizeof(V));
	int cnt=0;
	queue<point> q;
	q.push(point(h,w,cnt));
	point p;
	if(test){cout<<"root"<<cnt<<' '<<h<<' '<<w<<endl;
		show();
	}
	while(q.size()){
		p=q.front();
		q.pop();
		h=p.h;
		w=p.w;
		cnt=p.cnt;
		if(V[h][w]!=0)continue;
		if(A[h][w]==goal){
			return cnt;
		}
		if(test)cout<<"q.size "<<q.size()<<" cnt "<<cnt<<' '<<h<<' '<<w<<' '<<(int)V[h][w]<<" goal "<<goal<<endl;
		for(int c=0;c<4;c++){
			int tmph=h+mov[H][c];
			int tmpw=w+mov[W][c];
			if(!V[tmph][tmpw]&&A[h][w]!=-1)q.push(point(tmph,tmpw,cnt+1));
		}
		V[h][w]=1;
	}
	assert(q.size());
}
void solve(){
	int sum=0;
	for(int i=1;i<=n;i++){
		sum+=search(i);
	}
	cout<<sum<<endl;
}
int main()
{
	scanf("%d%d%d", &h,&w,&n);
	_h=h;
	_w=w;
	memset(A,-1,sizeof(A));
	for(int i=1;i<=_h;i++){
		scanf("%s",s);
		for(int j=1;j<=_w;j++){
			A[i][j]=cor(s[j-1],i,j);
		}
	}
	show();
	solve();
	return 0;
}
////aoj0558.cc
////generated automatically at Fri Sep  9 18:27:24 2016
////by xsthunder

C++: #include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;

struct OZAWA
{
	int x;
	int y;
	int c;
};
int Flag[1000][1000];
char MAP[1000][1000];
int main(void)
{
	int H,W,N,Nc=0,X[10],Y[10];
	OZAWA ba,v;
	queue<OZAWA> Q;
	int mv[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
	char Cheese[10] ={'1','2','3','4','5','6','7','8','9'};
	memset(MAP,'X',sizeof(MAP));
	memset(Flag,0,sizeof(Flag));
	scanf("%d %d %d",&H,&W,&N);
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			//printf("%d %d ",i,j);
			cin >> MAP[i][j];
			//printf("Q");
			if(MAP[i][j] == 'S'){
				ba.x = j;
				ba.y = i;
				ba.c = 0;
				Flag[i][j] = 1;
				Q.push(ba);
				//printf("Sin\n");
			}
			for(int ii=0;ii<9;ii++){
				if(MAP[i][j] == Cheese[ii]){

					X[ii] = j;
					Y[ii] = i;
					//printf("\n@@@\n");
				}
			}
		}
	}
	//printf("S %d %d\n",ba.x,ba.y);
	for(int ii=0;ii<2;ii++)
		//printf("%d %d\n",X[ii],Y[ii]);
	while(!Q.empty()){
		//printf("QA\n");
		ba = Q.front();
		Q.pop();
		if(ba.x==X[Nc] && ba.y==Y[Nc]){
			Nc++;
			memset(Flag,0,sizeof(Flag));
			while(!Q.empty()){
				Q.pop();
			}
			Q.push(ba);
		}
		if(Nc == N) break;
		for(int i=0;i<4;i++){
			if(MAP[ba.y+mv[i][1]][ba.x+mv[i][0]]!='X' && Flag[ba.y+mv[i][1]][ba.x+mv[i][0]]==0){
				v.x=ba.x+mv[i][0];
				v.y=ba.y+mv[i][1];
				v.c=ba.c+1;
				Q.push(v);
				Flag[v.y][v.x] = 1;
				//printf("%d %d\n",v.x,v.y);
			}
		}
	}
	cout <<ba.c<<endl;
	return 0;
}