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 <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

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

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  char field[h][w];
  int d[h][w];
  n++;
  P goals[n];
  rep(i,h) rep(j,w) {
    cin >> field[i][j];
    if (field[i][j] == 'S') {
      goals[0] = P(i, j);
    } else if (field[i][j] != '.' && field[i][j] != 'X') {
      goals[field[i][j]-'0'] = P(i, j);
    }
  }

  int ans = 0;
  for (int i=0; i<n-1; i++) {
    rep(x,h) rep(y,w) d[x][y] = -1;

    queue<P> que;
    que.push(goals[i]);
    d[goals[i].first][goals[i].second] = 0;

    //    printf("[*] start: %d %d\t goal: %d %d\n", goals[i].first, goals[i].second, goals[i+1].first, goals[i+1].second);

    while (!que.empty()) {
      P xy = que.front(); que.pop();
      if (goals[i+1].first == xy.first && goals[i+1].second == xy.second) {
	ans += d[xy.first][xy.second];
	break;
      }
      
      //      printf("   [*] d[%d][%d]: %d\n", xy.first, xy.second, d[xy.first][xy.second]);
  
      rep(j,4) {
        int nx = xy.first + dx[j];
        int ny = xy.second + dy[j];
  
        if (nx < 0 || h <= nx) continue;
        if (ny < 0 || w <= ny) continue;
        if (field[nx][ny] == 'X') continue;
	if (d[nx][ny] != -1) continue;
	que.push(P(nx, ny));
	d[nx][ny] = d[xy.first][xy.second] + 1;
	//	printf("      [*] d[%d][%d]: %d\n", nx, ny, d[nx][ny]);
      }
    }
  }

  cout << ans << endl;
  return 0;
}

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

#define pb push_back
#define fr first
#define sc second
typedef pair<int,int> pii;
typedef vector<int> vi;
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

char maze[1001][1001];
int d[1001][1001];
int h,w,n;
int sx,sy;

int bfs(int num){
  queue<pii> que;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      d[i][j]=INF;
    }
  }

  que.push(pii(sy,sx));
  d[sy][sx]=0;

  while(que.size()){
    pii p=que.front();que.pop();
    if(maze[p.fr][p.sc]=='1'+num) {
      sy=p.fr;
      sx=p.sc;
      return d[p.fr][p.sc];
    }
    for(int i=0;i<4;i++){
      int ny=p.fr+dy[i],nx=p.sc+dx[i];
      if(0<=ny && ny<h && 0<=nx && nx<w && maze[ny][nx]!='X' && d[ny][nx]==INF){
	que.push(pii(ny,nx));
	d[ny][nx]=d[p.fr][p.sc]+1;
      }
    }
  }
}

int main(){
  cin>> h>> w>> n;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>> maze[i][j];
      if(maze[i][j]=='S') {
	sy=i;sx=j;
      }
    }
  }

  int res=0;
  for(int i=0;i<n;i++){
    res+=bfs(i);
  }

  cout<< res<< endl;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
long long  cheese[101][101];
long long check[101][101];
char num[11]={'0','1','2','3','4','5','6','7','8','9'};
int q[4]={0,1,0,-1};
int p[4]={1,0,-1,0};
struct ST {
	int a;
	int b;
	int c;
	int d;
};
int main(){
	memset(cheese,0,sizeof(cheese));
	memset(check,0,sizeof(check));
	int h,w,n;
	int i,j,m;
	int cut=99999;
	int ys,xs;
	char data;
	queue <ST> Q;
	ST x;
	ST y;
	cin>>h>>w>>n;//??¢??????????????°
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>data;
			if(data=='S'){//????????????
				ys=i;//??????????????§?¨?
				xs=j;
			}
			else if(data=='X'){//?????????
				cheese[i][j]=-1;
			}
			else if(data!='.'){//
				for(m=1;m<=9;m++){
					if(num[m]==data){
						cheese[i][j]=m;
					}
				}
			}
			
		}
	}
	/*for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cout<<cheese[i][j];
		}
		cout<<endl;
	}*/
	x.a=ys;
	x.b=xs;
	x.c=0;
	x.d=1;
	Q.push(x);
	while(!Q.empty()){
		y=Q.front();
		Q.pop();
		check[y.a][y.b]=1;
		if(n==y.d&&n==cheese[y.a][y.b]){
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			if(cut>y.c){
				cut=y.c;
			}
			break;
		}
		if(y.d==cheese[y.a][y.b]){
			memset(check,0,sizeof(check));
			while(!Q.empty()){
				Q.pop();
			}
			y.d+=1;
			x.a=y.a;
			x.b=y.b;
			x.c=y.c;
			x.d=y.d;
			Q.push(x);
			continue;
		}
		for(i=0;i<4;i++){
			if(y.a+q[i]>0&&y.a+q[i]<=h&&y.b+p[i]>0&&y.b+p[i]<=w&&cheese[y.a+q[i]][y.b+p[i]]!=-1&&check[y.a+q[i]][y.b+p[i]]!=1){
				x.a=y.a+q[i];
				x.b=y.b+p[i];
				x.c=y.c+1;
				x.d=y.d;
				Q.push(x);
			}
		}
	}
	cout<<cut<<endl;
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
struct Point{int x,y,dist;};
int main()
{
  int H,W,N;
  char vital='1';
  int i,j;
  string field[1000];
  bool visited[1000][1000];
  queue<Point>q;
  int dx[4]={0,1,0,-1};
  int dy[4]={-1,0,1,0};
  Point ans;

  cin>>H>>W>>N;
  for(i=0;i<H;i++)cin>>field[i];
  for(i=0;i<H;i++){
    for(j=0;j<W;j++){
      if(field[i][j]=='S'){
	Point p={j,i,0};
	q.push(p);
      }
    }
  }
  for(i=0;i<H;i++)for(j=0;j<W;j++)visited[i][j]=false;

  Point p,p2;
  while(q.size()){
    p=q.front();
    q.pop();
    visited[p.y][p.x]=true;
    if(field[p.y][p.x]==vital){
      if(vital-'0'==N){
	ans=p;
	break;
      }
      vital++;
      while(q.size())q.pop();
      for(i=0;i<H;i++)for(j=0;j<W;j++)visited[i][j]=false;
    }
    for(i=0;i<4;i++){
      p2.y=p.y+dy[i];
      p2.x=p.x+dx[i];
      p2.dist=p.dist+1;
      if(p2.y<0||H<=p2.y)continue;
      if(p2.x<0||W<=p2.x)continue;
      if(field[p2.y][p2.x]=='X')continue;
      if(visited[p2.y][p2.x])continue;
      q.push(p2);
    }
  }
  cout<<ans.dist<<endl;
}