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 <queue>
#include <algorithm>
#define N 1002
#define INFINITY 2000000000
using namespace std;
void bfs();
int h,w,n,num[N][N],d[N*N],u,m;
string color[N*N],in[N*N];
int main(){
  int i,j,k;
  cin >> h >> w >> n;
  for(i=0;i<h+2;i++)
    for(j=0;j<w+2;j++)
      num[i][j]=-1;
  for(i=1;i<=h;i++) cin >> in[i],in[i]='X'+in[i];
  k=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(in[i][j]!='X') num[i][j]=k,k++;
    }
  }
  bfs();
  return 0;
}
void bfs(){
  int i,j,p=1,t=0,flag=0,x=1;
  char start='S',goal='1';
  queue<int> q;
  while(1){
    if(flag==1) start=x+'0',goal=x+'0'+1,x++;
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	color[num[i][j]]="WHITE",d[num[i][j]]=INFINITY;
      }
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==start) color[num[i][j]]="GLAY",d[num[i][j]]=0,q.push(num[i][j]);
      }
    }
    while(q.empty()!=1){
      u=q.front();
      q.pop();
      for(i=1;i<=h;i++){
	for(j=1;j<=w;j++){
	  if(num[i][j]!=-1&&(num[i-1][j]==u||num[i+1][j]==u||num[i][j+1]==u||num[i][j-1]==u)&&color[num[i][j]]=="WHITE"){
	    color[num[i][j]]="GLAY";
	    d[num[i][j]]=d[u]+1;
	    q.push(num[i][j]);
	  }
	}
      }
      color[u]="BLACK";
    }
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	if(in[i][j]==goal) t+=d[num[i][j]];
      }
    }
    flag=1;
    if(x>n) break;
  }
  cout << t << endl;
}

C++: #include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<limits.h>
#include<queue>
char o[1002][1003],*p;
int h,w;
int f(int sx,int sy,int gx,int gy)
{
	struct S{int x,y,s;}s={sx,sy,0},t;;
	std::queue<S>q;
	static int d[1001][1001]={0},i,j,a[]={-1,0,1,0};
	for(i=1;i<=h;++i)for(j=1;j<=w;++j)d[i][j]=INT_MAX;
	q.push(s);
	while(!q.empty())
	{
		s=q.front();q.pop();
		if(o[s.y][s.x]=='X' || d[s.y][s.x]<=s.s)continue;
		d[s.y][s.x]=s.s;
		for(i=0;i<4;++i)
		{
			t=s;
			t.x+=a[i];
			t.y+=a[(i+1)%4];
			t.s++;
			q.push(t);
		}
	}
	return d[gy][gx];
}
int main()
{
	int n,i,j,x[10],y[10],z,s=0;
	scanf("%d%d%d",&h,&w,&n);
	for(i=0;i<h;++i)
	{
		scanf("%s",&o[i+1][1]);
		p=&o[i+1][1];
		while(p=strpbrk(p,"S123456789"))
			z=isdigit(*p)?*p-'0':0,y[z]=i+1,x[z]=p-o[i+1],++p;
	}
	for(j=0;j<w+2;++j)o[0][j]=o[h+1][j]='X';
	for(i=0;i<h+2;++i)o[i][0]=o[i][w+1]='X';
	for(i=0;i<n;++i)s+=f(x[i],y[i],x[i+1],y[i+1]);
	printf("%d\n",s);
	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <cctype>
using namespace std;

#define f first
#define s second

char mas[1111][1111];
int used[1111][1111];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int w,h,sy,sx,n,len = 0;
typedef pair<int,int> P;
vector <P> pi(10);

int bfs(){
  memset(used,-1,sizeof(used));
  queue <P> que;
  que.push(P(sx,sy));
  used[sy][sx] = 0;
  while(!que.empty()){
    P p = que.front();
    que.pop();
    if(p.f == pi[len].f && p.s == pi[len].s){
      len++;
      sx = p.f;
      sy = p.s;
      return used[p.s][p.f];
    }
    for(int i=0;i<4;i++){
      int nx = p.f + dx[i];
      int ny = p.s + dy[i];
      if(nx >= 0 && nx < w && ny >= 0 && ny < h && mas[ny][nx]!='X'&& used[ny][nx] == -1){
	que.push(P(nx,ny));
	used[ny][nx] = used[p.s][p.f] + 1;
      }
    }
  }
}


int main(){
  cin >> h >> w >> n;
  char value = '1';
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mas[i][j];
      if(mas[i][j] == 'S'){
	sx = j;
	sy = i;
      }
      else if(isdigit(mas[i][j])) pi[mas[i][j]-'0'-1] = P(j,i);
    }
  }
  int ans = 0;
  for(int i=0;i<n;i++) ans += bfs();
  cout << ans << endl;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>
using namespace std;
const int INF = 8101919;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 10;
struct pt {
	int x;
	int y;
};
int h;
int w;
int n;
pt cheese[MAX_N];
queue<pt> nextsearch;
pt st;
int cost[MAX_H][MAX_W];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
vector<string> field;
int mousehp = 1;
int bfs() {
	for (int i = 0; i < MAX_N; i++) {
		cheese[i].x = INF;
		cheese[i].y = INF;
	}
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cost[i][j] = INF;
			if (field[i][j] == 'S') {
				st.x = j;
				st.y = i;
				cost[i][j] = 0;
			}
			switch (field[i][j]) {
			case '1':
				cheese[1].x = j;
				cheese[1].y = i;
				break;
			case '2':
				cheese[2].x = j;
				cheese[2].y = i;
				break;
			case '3':
				cheese[3].x = j;
				cheese[3].y = i;
				break;
			case '4':
				cheese[4].x = j;
				cheese[4].y = i;
				break;
			case '5':
				cheese[5].x = j;
				cheese[5].y = i;
				break;
			case '6':
				cheese[6].x = j;
				cheese[6].y = i;
				break;
			case '7':
				cheese[7].x = j;
				cheese[7].y = i;
				break;
			case '8':
				cheese[8].x = j;
				cheese[8].y = i;
				break;
			case '9':
				cheese[9].x = j;
				cheese[9].y = i;
				break;
			default:
				break;
			}
		}
	}
	nextsearch.push(st);
	int searchCheese = 1;
	int allcost;
	while (nextsearch.size()) {
		pt queuefirst = nextsearch.front();
		nextsearch.pop();
		if (queuefirst.x == cheese[searchCheese].x && queuefirst.y == cheese[searchCheese].y && field[queuefirst.y][queuefirst.x] - '0' <= mousehp) {
			searchCheese++;
			mousehp++;
			field[queuefirst.y][queuefirst.x] = '.';
			if (searchCheese > n) {
				allcost = cost[queuefirst.y][queuefirst.x];
				break;
			}
			queue<pt> empty;
			swap(nextsearch,empty);
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (queuefirst.x == j && queuefirst.y == i) {
					} else {
						cost[i][j] = INF;
					}
				}
			}
		}
		for (int i = 0; i < 4; i++) {
			pt nextp;
			nextp.x = queuefirst.x + dx[i];
			nextp.y = queuefirst.y + dy[i];
			if (0 <= nextp.x && nextp.x < w && 0 <= nextp.y && nextp.y < h && field[nextp.y][nextp.x] != 'X' && cost[nextp.y][nextp.x] == INF) {
				nextsearch.push(nextp);
				cost[nextp.y][nextp.x] = cost[queuefirst.y][queuefirst.x] + 1;
			}
		}
	}
	return allcost;
}
int main() {
	cin >> h >> w >> n;

	string inputtmp;
	for (int i = 0; i < h; i++) {
		cin >> inputtmp;
		field.push_back(inputtmp);
	}
	cout << bfs() << endl;
	return 0;
}