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<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<time.h>
#include<queue>
using namespace std;
#define INF 100000
char map[101][101];
int h,w,n;
int d1[4]={0,1,0,-1};
int d2[4]={1,0,-1,0};
int i,j,x[10],y[10];
int bfs(int num){
	int gy=y[num+1],gx=x[num+1];
	int d[101][101];
	for(int i=0;i<101;i++){
		for(int j=0;j<101;j++){
			d[i][j]=INF;
		}
	}
	queue<int> qx;queue<int> qy; 
	qx.push(x[num]);qy.push(y[num]);
	d[y[num]][x[num]]=0;
	while(qx.size()+qy.size()!=0){
		int ny,nx;
		ny=qy.front();qy.pop();
		nx=qx.front();qx.pop();
		if(ny==gy&&nx==gx){
			break;
		}
		for(int i=0;i<4;i++){
			int mx=nx+d1[i],my=ny+d2[i];
			if(0<=mx&&mx<w&&0<=my&&my<h&&map[my][mx]!='X'&&d[my][mx]==INF){
				d[my][mx]=d[ny][nx]+1;
				qx.push(mx);
				qy.push(my);
			}
		}
	}
	return d[gy][gx];
}
int main(){
	int ans=0;
	cin>>h>>w>>n;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>map[i][j];
			if(map[i][j]=='S'){
				y[0]=i;x[0]=j;
			}else if('0'<map[i][j]&&map[i][j]<='9'){
				y[(int)(map[i][j]-'0')]=i;
				x[(int)(map[i][j]-'0')]=j;
			}
		}
	}

	for(int i=0;i<n;i++){
		ans+=bfs(i);
	}
	
	cout<<ans<<endl;
	return 0;
}

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

int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

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

void bfs()
{
    for (int n = 1; n < N + 1; n++)
    {
        queue<pair<int, int>> que;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                d[i][j] = INF;
            }
        }
        que.push(start);
        d[start.first][start.second] = 0;

        while (que.size())
        {
            pair<int, int> p = que.front();
            que.pop();
            if (field[p.first][p.second] == '0' + (char)n)
            {
                cheese = p;
                break;
            }

            for (int i = 0; i < 4; i++)
            {
                int nx = p.first + dx[i];
                int ny = p.second + dy[i];

                if (0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' && d[nx][ny] == INF)
                {
                    que.push(pair<int, int>(nx, ny));
                    d[nx][ny] = d[p.first][p.second] + 1;
                }
            }
        }
        result += d[cheese.first][cheese.second];
        start = cheese;
    }
}

int main() {
    scanf(" %d %d %d", &H, &W, &N);
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            scanf(" %c", &field[i][j]);
            if (field[i][j] == 'S')
            {
                start.first = i;
                start.second = j;
            }
        }
    }
    bfs();
    printf("%d\n", result);
}

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 <stdio.h>
#include <cstring>
#include <queue>
using namespace std;

char ma[1005][1005];int vis[1005][1005];
int w,h,n;

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

struct node
{
    int x,y,time;
};
node start;

void bfs(char ed)
{
    queue<node> que;
    que.push(start);
    vis[start.x][start.y]=1;
    while(!que.empty())
    {
        node no=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int x=no.x+dx[i];
            int y=no.y+dy[i];
            if(vis[x][y]||x>=h||x<0||y>=w||y<0||ma[x][y]=='X')
                continue;
            if(ma[x][y]==ed)
            {
                start.x=x;
                start.y=y;
                start.time=no.time+1;
                return ;
            }
            vis[x][y]=1;
            node no1;
            no1.x=x;no1.y=y;no1.time=no.time+1;
            que.push(no1);
        }
    }
}

int main()
{
    while(~scanf("%d %d %d", & h, & w, & n))
    {
        for(int i=0;i<h;i++)
        {
            scanf("%s",&ma[i]);
            for(int j=0;j<w;j++)
                if(ma[i][j]=='S')
                {
                    start.x=i;
                    start.y=j;
                    start.time=0;
                }

        }
            for(int i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                bfs(i+'1');
            }

            printf("%d\n",start.time);
    }
    return 0; 
}

C++: #include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
#define Max 10000
char field[Max][Max];
const int INF=10000000;
typedef pair<int,int> P;
int N,M,sx,sy,gx,gy,H;
int d[Max][Max];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int goal[Max]{};
int bfs()
{
	int anw=0;
	deque<P>Q;
	for(int c=0;c<H;c++){
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				d[i][j]=INF;
			}
		}
	Q.push_back(P(sx,sy));
	d[sx][sy]=0;
	while(Q.size()){
		int x=Q.front().first;
		int y=Q.front().second;
		Q.pop_front();
		if((int)field[x][y]==49+c){
			goal[c]=d[x][y];
			sx=x;
			sy=y;
			break;
		}
		
		for(int i=0;i<4;i++){
			int nx=x+dx[i];
			int ny=y+dy[i];
			if(0<=nx&&nx<N&&0<=ny&&ny<M&&field[nx][ny]!='X'&&d[nx][ny]==INF){
				Q.push_back(P(nx,ny));
				d[nx][ny]=d[x][y]+1;
			}
		}
		
	}
	Q.clear();
	anw=anw+goal[c];
	}
	return anw;
}
int main()
{
	cin>>N>>M>>H;
	for(int i=0;i<N;i++)
		for(int j=0;j<M;j++){
			cin>>field[i][j];
			if(field[i][j]=='S')
				sx=i; sy=j;
				//field[sx][sy]='0';
		}
	printf("%d\n",bfs());
	return 0;
}