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<string.h>
using namespace std;
long long  cheese[1001][1001];
long long check[1001][1001];
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 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;
					}
				}
			}
			
		}
	}
	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(y.d==cheese[y.a][y.b]&&n==y.d){
			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<<y.c<<endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>

#define INF 9

typedef std::pair<int,int> pair;

int h,w,n;
std::vector<std::vector<char> >data;
std::vector<std::vector<int> >cost;

int dfs(int sx,int sy,int gx,int gy){
	for(unsigned int i=0;i<cost.size();i++){
		for(unsigned int j=0;j<cost[i].size();j++){
			cost[i][j]=INF;
		}
	}
	int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
	std::queue<pair>q;
	cost[sy][sx]=0;
	q.push(pair(sx,sy));

	while(q.size()){
		int x=q.front().first,y=q.front().second;
		if(x==gx&&y==gy)return cost[y][x];
		q.pop();
		for(int i=0;i<4;i++){
			if(x+dx[i]>=0&&x+dx[i]<=w-1&&y+dy[i]>=0&&y+dy[i]<=h-1){
				if(data[y+dy[i]][x+dx[i]]!='X'&&cost[y+dy[i]][x+dx[i]]==INF){
					cost[y+dy[i]][x+dx[i]]=cost[y][x]+1;
					q.push(pair(x+dx[i],y+dy[i]));
				}
			}
		}
	}
	return 0;
}

int main(){
	int sx,sy,ans=0;
	pair g[10];
	std::cin>>h>>w>>n;
	for(int i=0;i<h;i++){
		data.push_back(std::vector<char>());
		cost.push_back(std::vector<int>());
		for(int j=0;j<w;j++){
			char a;
			std::cin>>a;
			data[i].push_back(a);
			cost[i].push_back(INF);
			if(a=='S')sx=j,sy=i;
			if(a!='S'&&a!='X'&&a!='.')g[a-'0']=pair(j,i);
		}
	}

	for(int i=1;i<=n;i++){
		ans+=dfs(sx,sy,g[i].first,g[i].second);
		sx=g[i].first,sy=g[i].second;
	}

	std::cout<<ans<<std::endl;

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#define INF 10000000
using namespace std;

typedef pair<int, int> P;

int H, W, N;
int d[1000][1000];
char cell[1000][1000];
int si, sj, sum = 0;

void bfs(P start, int search){
	//4????????¨
	int vi[4] = {0, 0, -1, 1}, vj[4] = {-1, 1, 0, 0};

	int gi, gj;

	//???????????¢???INF?????????????????????????????´???????????§?¨??????????
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			d[i][j] = INF;
			if(cell[i][j] == search + '0'){
				gi = i;
				gj = j;
			}
		}
	}

	//start??????????????¢0????????????
	d[start.first][start.second] = 0;

	queue<P> que;
	que.push(start);

	while(!que.empty()){
		P p = que.front();
		que.pop();

		//?????´??§break
		if(p.first == gi && p.second == gj) break;

		//next(i, j)
		int ni, nj;

		//4?????????
		for(int i=0;i<4;i++){
			ni = p.first + vi[i];
			nj = p.second + vj[i];

			if(ni >= 0 && ni < H && nj >= 0 && nj < W && cell[ni][nj] != 'X' && d[ni][nj] == INF){
				que.push(P(ni, nj));
				d[ni][nj] = d[p.first][p.second] + 1;
			}
		}
	}

	sum += d[gi][gj];

	if(search != N)	bfs(P(gi, gj), search+1);
}

int main(){
	cin >> H >> W >> N;

	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin >> cell[i][j];
			if(cell[i][j] == 'S'){
				si = i;
				sj = j;
			}
		}
	}
	
	bfs(P(si, sj), 1);

	cout << sum << endl;
}

C++: #include<cstdio>
#include<queue>
#define MAX_W 1000
#define MAX_H 1000
#define MAX_N 9
int h, w, n;
char fields[MAX_W + 1][MAX_H] = { };

int INF = 100000000;
int d[MAX_W][MAX_H];
int sx, sy;
typedef
std::pair < int, int >
  P;
P
  gp[MAX_N];
int
dx[4] = { -1, 1, 0, 0 }, dy[4] = {0, 0, -1, 1};

void
initd (void)
{
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      d[j][i] = INF;
}

int
bfs (int m)
{
  std::queue < P > que;
  que.push (P (sx, sy));
  initd ();
  d[sx][sy] = 0;
  while (que.size () > 0)
    {
      P
	p = que.front ();
      que.pop ();
      for (int i = 0; i < 4; i++)
	{
	  int
	    nx = p.first + dx[i];
	  int
	    ny = p.second + dy[i];
	  if (0 <= nx && nx < w && 0 <= ny && ny < h && d[nx][ny] == INF
	      && 'X' != fields[nx][ny])
	    {
	      que.push (P (nx, ny));
	      d[nx][ny] = d[p.first][p.second] + 1;
	      if (gp[m] == P (nx, ny))
		{
		  sx = nx;
		  sy = ny;
		  return d[nx][ny];
		}
	    }
	}
    }
}

int
main (void)
{
  scanf ("%d %d %d\n", &h, &w, &n);
  for (int y = 0; y < h; y++)
    for (int x = 0; x <= w; x++)
      {
	scanf ("%c", &fields[x][y]);
	if ('S' == fields[x][y])
	  {
	    sx = x;
	    sy = y;
	  }
	else if ('1' <= fields[x][y] && fields[x][y] <= '9')
	  {
	    gp[fields[x][y] - '1'] = P (x, y);
	  }
      }
  int
    t = 0;
  for (int d = 0; d < n; d++)
    t += bfs (d);
  printf ("%d\n", t);
  return 0;
}

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

typedef pair<int,int> P;
int flg[1000][1000];

void ini(int x,int y){
	for(int k=0;k<y;k++){for(int j=0;j<x;j++){flg[k][j]=-1;}}
}

int main()
{
	int x,y,n,sum=0;
	char map[1000][1001];
	char ntoi[10]="123456789";	
	queue<P> q;
	int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};

	cin>>y>>x>>n;
	for(int i=0;i<y;i++)
	{
		for(int j=0;j<x;j++)
		{
			cin>>map[i][j];
			if(map[i][j]=='S')
			{
				q.push(make_pair(i,j));
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		ini(x,y);
		flg[q.front().first][q.front().second]=0;
		while(!q.empty())
		{
			P temp=q.front();
			q.pop();
			if(map[temp.first][temp.second]==ntoi[i])
			{
				sum+=flg[temp.first][temp.second];
				while(!q.empty()){q.pop();}
				q.push(temp);
				break;
			}
			for(int j=0;j<4;j++)
			{
				if(temp.first+dy[j]>-1&&temp.first+dy[j]<y&&temp.second+dx[j]>-1&&temp.second+dx[j]<x&&map[temp.first+dy[j]][temp.second+dx[j]]!='X'&&flg[temp.first+dy[j]][temp.second+dx[j]]==-1)
				{
					flg[temp.first+dy[j]][temp.second+dx[j]] = flg[temp.first][temp.second]+1;
					q.push(make_pair(temp.first+dy[j],temp.second+dx[j]));
				}
			}
		}
	}
	cout<<sum<<endl;
	return 0;
}