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<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
int D[1002][1002];
int A[1002][1002],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++;
		
		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++;
		if(A[S.y][S.x]==1||s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
		if(s[S.y][S.x]=='.'){}
		else{
			if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;
			}

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int bfs(PII begin, PII end, vector<string> grid, bool arv[]){
  int H = grid.size();
  int W = grid[0].size();
  queue<PII> q1,q2;
  q1.push(begin);
  int ans = 1;
  while(1){
    if(q1.empty()){
      q1 = q2;
      while(!q2.empty()) q2.pop();
      ans++;
    }else{
      PII point = q1.front();
      q1.pop();
      REP(i,4){
        int x = point.first + dx[i];
        int y = point.second + dy[i];
        if(PII(x,y)==end){
          return ans;
        }
        if(0<=x&&x<W&&0<=y&&y<H&&grid[y][x]!='X'&&arv[y*W+x]==false){
          q2.push(PII(x,y));
          arv[y*W+x] = true;
        }
      }
    }
  }
}
    


int main(void){
  int H,W,N;
  cin>>H>>W>>N;
  vector<string> grid(H);
  PII start;
  vector<PII> cheese(N+1);
  REP(i,H) cin>>grid[i];
  REP(y,H)REP(x,W){
    if(grid[y][x]=='S') cheese[0] = PII(x,y);
    if('0'<=grid[y][x]&&grid[y][x]<='9') cheese[ grid[y][x] - '0'] = PII(x,y);
  }
  int ans = 0;
  bool arv[1000000];
  REP(i,N){
    memset(arv,0,sizeof(arv));
    ans += bfs(cheese[i],cheese[i+1],grid,arv);
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>

#pragma warning(disable:4996)
#define REP(i,n) for(int i = 0 ; i < n ; ++i)
typedef long long Int;

using namespace std;

struct point{
	int j, i,step;
	point(int i_, int j_,int s) :i(i_), j(j_),step(s){}
};

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

queue<point> que;
char tile[1000][1000];
int H, W;
bool ed[1000][1000];

point solve(char target)
{
	while (!que.empty()){
		point now(que.front());
		ed[now.j][now.i] = true;
		que.pop();
		REP(i, 4){
			int x = now.j + dx[i];
			int y = now.i + dy[i];

			if (x < 0 || y < 0 || x > W || y > H || ed[x][y])
				continue;

			if (tile[y][x] == target){
				return point(y, x,now.step+1);
			}

			if (tile[y][x] != 'X'){
				que.push(point(y, x, now.step + 1));
				ed[x][y] = true;
			}
		}
	}
}

int main()
{
	cin.tie(0); ios::sync_with_stdio(false);
	//fstream fs("input.txt");
	////////////ここから開始///////////
	int n(0);
	cin >> H >> W >> n;

	REP(i, 1000)REP(j, 1000)tile[i][j] = 'X';

	REP(i, H)REP(j, W)cin>> tile[i][j];

	REP(i, H)REP(j, W){ if (tile[i][j] == 'S')que.emplace(point(i, j, 0)); }
	int ans;
	for(int i = 0 ; i < n ; ++i){
		REP(i_1, 1000)REP(j_1, 1000){ ed[i_1][j_1] = false; }
		point t = solve('1' + i);
		que = {};
		que.emplace(t);
		ans = t.step;
	}
	
	cout << ans << endl;

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int,int> _P;
typedef pair<_P,int> P;
char M[1000][1001];
bool vst[1000][1000];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int W,H,N,X,Y;
int bfs(char C) {
	queue<P> Q;
	Q.push(P(_P(X,Y),0));
	vst[X][Y]=true;
	while(!Q.empty()) {
		P p=Q.front();Q.pop();
		if(M[p.first.second][p.first.first]==C) {
			X=p.first.first,Y=p.first.second;
			return p.second;
		}
		for(int i=0;i<4;i++) {
			int nx=p.first.first+dx[i],ny=p.first.second+dy[i];
			if(0>nx||nx>=W||0>ny||ny>=H)continue;
			if(M[ny][nx]=='X')continue;
			if(vst[nx][ny])continue;
			Q.push(P(_P(nx,ny),p.second+1));
			vst[nx][ny]=true;
		}
	}
	return -1;
}
int main() {
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++) {
		scanf("%s",M[i]);
	}
	for(int i=0;i<H;i++) {
		for(int j=0;j<W;j++) {
			if(M[i][j]=='S')X=j,Y=i;
		}
	}
	int sum=0;
	for(int i=1;i<=N;i++) {
		memset(vst,0,sizeof(vst));
		sum+=bfs(i+'0');
	}
	printf("%d\n",sum);
}

C++: #include <iostream>
#include <queue>
#include <string>
using namespace std;
int main()
{
	int h,w,n;
	int sx,sy;
	int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
	string map[1001];
	int d[1001][1001];
	int x = 0;
	cin >> h >> w >> n;
	for(int i=0;i < h;i++)
	{
		cin >> map[i];
	}
	for(int i=0;i < h;i++)
	{
		for(int j=0;j < w;j++)
		{
			d[j][i] = -1;
			if(map[i][j] == 'S')
			{
				sx = j;
				sy = i;
			}
		}
	}
	d[sx][sy] = 0;
	for(int i=0;i<9;i++)
	{
		queue< pair<int, int> > que;
		for(int j=0;j < h;j++)
		{
			for(int k=0;k < w;k++)
			{
				d[k][j] = -1;
			}
		}
		d[sx][sy] = 0;
		que.push(make_pair(sx,sy));
		while(que.size())
		{
			pair<int, int> p = que.front();
			que.pop();
			if((map[p.second][p.first] - '0') == (i+1))
			{
				sx = p.first;
				sy = p.second;
				x+=d[sx][sy];
				break;
			}
			for(int j=0;j < 4;j++)
			{
				int nx = p.first + dx[j];
				int ny = p.second + dy[j];
				if(0 <= nx && nx < w && 0 <= ny && ny < h && map[ny][nx] != 'X' && d[nx][ny] == -1)
				{
					que.push(make_pair(nx,ny));
					d[nx][ny] = d[p.first][p.second] + 1;
				}
			}
		}
	}
	cout << x << endl;
}