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 <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
//#include <map>
#include <iomanip>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1000000000;

typedef pair<int, int> P;	//縦、横

int H, W, N;
char maze[1000][1000];
int sx, sy;
int gx, gy;
int d[1000][1000];		//各点までの最短距離の配列
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int cnt;


int bfs()
{
	queue<P> que;
	
	for (int i=0; i<H; i++)
	{
		for (int j=0; j<W; j++)
		{
			d[i][j] = INF;
		}
	}
	
	que.push(P(sy, sx));
	d[sy][sx] = 0;
	
	while (que.size())
	{
		P p = que.front();
		que.pop();
		if (p.first == gy && p.second == gx) break;
		
		for (int i=0; i<4; i++)
		{
			int nx = p.second + dx[i];
			int ny = p.first + dy[i];
			
			if (0<=nx && nx<W && 0<=ny && ny<H && maze[ny][nx]!='X' && d[ny][nx]==INF)
			{
				que.push(P(ny, nx));
				d[ny][nx] = d[p.first][p.second] + 1;
			}
		}
	}
	
	return d[gy][gx];
}


int main()
{
	//入力
	cin >> H >> W >> N;
	for (int i=0; i<H; i++)
	{
		cin >> maze[i];
	}
	
	//巣と１番目のチーズ工場（ゴール）の設定
	cnt = 0;
	for (int k=1; k<=N; k++)
	{
		for (int i=0; i<H; i++)
		{
			for (int j=0; j<W; j++)
			{
				if (k == 1)
				{
					if (maze[i][j] == 'S')
					{
						sx = j;
						sy = i;
					} else if (maze[i][j] == '1') {
						gx = j;
						gy = i;
					}
				} else {
					if (maze[i][j] == '1' + k - 2)
					{
						sx = j;
						sy = i;
					} else if (maze[i][j] == '1' + k - 1) {
						gx = j;
						gy = i;
					}
				}
			}
		}
		
		cnt += bfs();
	}
	
	cout << cnt << endl;
	
	/*
	cout << "cnt = " << cnt << endl;
	
	//テスト出力
	for (int i=0; i<H; i++)
	{
		cout << maze[i] << endl;
	}
	*/
	
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
int map[1002][1002]={0},r[1002][1002]={0};
struct A{
int t,h,x,y;
};
int main(){
int h,w,n,i,j,m=0,g[2],mark=0,b;
g[0]=1;
g[1]=-1;
char s[2002];
A a;
queue<A> q;
cin>>h>>w>>n;
for(i=0;i<=h+1;i++){
map[i][0]=-1;
map[i][w+1]=-1;
}
for(i=0;i<=w+1;i++){
map[0][i]=-1;
map[h+1][i]=-1;
}
for(i=1;i<=h;i++){
	scanf("%s",s);
	for(j=0;j<=w;j++){
		b=s[j];
		if(b>48&&b<58)
			map[i][j+1]=b-48;
		else if(s[j]=='S'){
			a.x=i;
			a.y=j+1;
			a.t=0;
			a.h=1;
			q.push(a);
		}
		else if(s[j]=='X')
			map[i][j+1]=-1;
	}
}
while(!q.empty()){
	a=q.front();
	q.pop();
	if(map[a.x][a.y]==a.h){
		a.h++;
		mark=0;
		while(!q.empty()){
			q.pop();
		}
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				r[i][j]=0;
			}
		}
	}
	mark++;
	r[a.x][a.y]=mark;
	if(a.h==n+1){
		if(m==0||m>a.t){
			m=a.t;
			break;
		}
	}
	a.t++;
	for(i=0;i<2;i++){
		if(map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]>mark||map[a.x+g[i]][a.y]!=-1&&r[a.x+g[i]][a.y]==0){
			a.x+=g[i];
			q.push(a);
			a.x-=g[i];
		}
		if(map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]>mark||map[a.x][a.y+g[i]]!=-1&&r[a.x][a.y+g[i]]==0){
			a.y+=g[i];
			q.push(a);
			a.y-=g[i];
		}
	}
}
cout<<m<<endl;
return 0;
}

C++: #include <stdio.h>

bool syo[1000][1000];
int zi[1000][1000];
int kox[1000000];
int koy[1000000];
int kon;
int koxs[1000000];
int koys[1000000];
int kons;
int cx[10];
int cy[10];
int sx,sy;

int main()
{
	int h,w,n;
	scanf("%d %d %d",&h,&w,&n);
	for(int i = 0; i < h; i++)
	{
		char ww[1111];
		scanf("%s",ww);
		for(int ii = 0; ii < w; ii++)
		{
			if('1' <= ww[ii] && ww[ii] <= '9')
			{
				int d = ww[ii] - '1';
				cy[d] = i;
				cx[d] = ii;
			}
			if(ww[ii] == 'S')
			{
				sy = i;
				sx = ii;
			}
			if(ww[ii] == 'X')
			{
				syo[i][ii] = true;
			}
			else
			{
				syo[i][ii] = false;
			}
		}
	}
	int time = 0;
	int nx = sx;
	int ny = sy;
	for(int k = 0; k < n; k++)
	{
		for(int i = 0; i < h; i++)
		{
			for(int ii = 0; ii < w; ii++)
			{
				zi[i][ii] = 100000000;
			}
		}
		zi[ny][nx] = 0;
		kox[0] = nx;
		koy[0] = ny;
		kon = 1;
		while(1)
		{
			kons = 0;
			for(int i = 0; i < kon; i++)
			{
				if(syo[koy[i]][kox[i]])
				{
					continue;
				}
				if(kox[i] == cx[k] && koy[i] == cy[k])
				{
					goto aa;
				}
				if(kox[i] > 0)
				{
					if(zi[koy[i]][kox[i] - 1] > zi[koy[i]][kox[i]])
					{
						zi[koy[i]][kox[i] - 1] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i] - 1;
						koys[kons] = koy[i];
						kons++;
					}
				}
				if(kox[i] < w - 1)
				{
					if(zi[koy[i]][kox[i] + 1] > zi[koy[i]][kox[i]])
					{
						zi[koy[i]][kox[i] + 1] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i] + 1;
						koys[kons] = koy[i];
						kons++;
					}
				}
				if(koy[i] > 0)
				{
					if(zi[koy[i] - 1][kox[i]] > zi[koy[i]][kox[i]])
					{
						zi[koy[i] - 1][kox[i]] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i];
						koys[kons] = koy[i] - 1;
						kons++;
					}
				}
				if(koy[i] < h - 1)
				{
					if(zi[koy[i] + 1][kox[i]] > zi[koy[i]][kox[i]])
					{
						zi[koy[i] + 1][kox[i]] = zi[koy[i]][kox[i]] + 1;
						koxs[kons] = kox[i];
						koys[kons] = koy[i] + 1;
						kons++;
					}
				}
			}
			for(int i = 0; i < kons; i++)
			{
				kox[i] = koxs[i];
				koy[i] = koys[i];
			}
			kon = kons;
		}
aa:
		time += zi[cy[k]][cx[k]];
		nx = cx[k];
		ny = cy[k];
	}
	printf("%d\n",time);
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#define inf 1000000
using namespace std;
int H,W,N,x0,y0;
char map[1005][1005];
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
typedef pair<int,int>P;
struct position
{
	int rx,ry,e;
}xy[9];
bool comp(const position a,const position b)
{
	return a.e<b.e;
}
int bfs(int sx,int sy,int gx,int gy)
{
	int tx,ty,d[1005][1005];
	queue<P>q;
	for(int i=0;i<H;i++)
	{
		for(int j=0;j<W;j++)
		d[i][j]=inf;
	}
	q.push(P(sx,sy));
	d[sx][sy]=0;
	while(!q.empty())
	{
		P p=q.front();
		if(p.first==gx&&p.second==gy)
		return d[p.first][p.second];
		q.pop();
		for(int i=0;i<4;i++)
		{
			tx=p.first+dx[i];
			ty=p.second+dy[4];
			if(tx>=0&&tx<H&&ty>=0&&ty<W&&map[tx][ty]!='X'&&d[tx][ty]==inf)
			{
				d[tx][ty]=d[p.first][p.second]+1;
				q.push(P(tx,ty));
			}
		}
	}
}
int main(void)
{
	while(scanf("%d%d%d",&H,&W,&N)==3)
	{
		int x1[9],y1[9];
		for(int i=0;i<H;i++)
		scanf("%s",&map[i]);
		int t=0;
		for(int i=0;i<H;i++)
		{
			for(int j=0;j<W;j++)
			{
				if(map[i][j]=='S')
				{
					x0=i;
					y0=j;
				}
				if(map[i][j]-'0'>0&&map[i][j]-'0'<=9)
				{
					xy[t].rx=i;
					xy[t].ry=j;
					xy[t].e=map[i][j]-'0';
					t++;
				}
			}
		}
		sort(xy,xy+N,comp);
		int ans=bfs(x0,y0,xy[0].rx,xy[0].ry);
		for(int i=0;i<N;i++)
		ans+=bfs(xy[i].rx,xy[i].ry,xy[i+1].rx,xy[i+1].ry);
		printf("%d\n",ans);
	}
	return 0;
}