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<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<time.h>
#include<queue>
using namespace std;
#define INF 100000
char map[1001][1001];
int h,w,n;
int d1[4]={0,1,0,-1};
int d2[4]={1,0,-1,0};
int i,j,x[10],y[10];
int bfs(int num){
	int gy=y[num+1],gx=x[num+1];
	int d[1001][1001];
	for(int i=0;i<=1001;i++){
		for(int j=0;j<=1001;j++){
			d[i][j]=INF;
		}
	}
	queue<int> qx;queue<int> qy; 
	qx.push(x[num]);qy.push(y[num]);
	d[y[num]][x[num]]=0;
	while(qx.size()+qy.size()!=0){
		int ny,nx;
		ny=qy.front();qy.pop();
		nx=qx.front();qx.pop();
		if(ny==gy&&nx==gx){
			break;
		}
		for(int i=0;i<4;i++){
			int mx=nx+d1[i],my=ny+d2[i];
			if(0<=mx&&mx<w&&0<=my&&my<h&&map[my][mx]!='X'&&d[my][mx]==INF){
				d[my][mx]=d[ny][nx]+1;
				qx.push(mx);
				qy.push(my);
			}
		}
	}
	return d[gy][gx];
}
int main(){
	int ans=0;
	cin>>h>>w>>n;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>map[i][j];
			if(map[i][j]=='S'){
				y[0]=i;x[0]=j;
			}else if('0'<map[i][j]&&map[i][j]<='9'){
				y[(int)(map[i][j]-'0')]=i;
				x[(int)(map[i][j]-'0')]=j;
			}
		}
	}

	for(int i=0;i<n;i++){
		ans+=bfs(i);
	}
	
	cout<<ans<<endl;
	return 0;
}

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

typedef pair<int, int> P;

const int INF = 1000000;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int H, W;
bool a[1000][1000];
int d[1000][1000];

int bfs(int sx, int sy, int gx, int gy) {
	for (int y = 0; y < H; y++)
		fill_n(d[y], W, INF);
	d[sy][sx] = 0;
	queue<P> que;
	que.push(P(sx, sy));
	while (!que.empty()) {
		P p = que.front();
		que.pop();
		int x = p.first, y = p.second;
		if (x == gx && y == gy)
			return d[y][x];
		for (int i = 0; i < 4; i++) {
			int _x = x + dx[i], _y = y + dy[i];
			if (_x >= 0 && _x < W && _y >= 0 && _y < H && !a[_y][_x] && d[_y][_x] == INF) {
				d[_y][_x] = d[y][x] + 1;
				que.push(P(_x, _y));
			}
		}
	}
}


int main() {
	int N;
	scanf("%d%d%d", &H, &W, &N);
	int xi[10], yi[10];
	for (int y = 0; y < H; y++) {
		char s[1001];
		scanf("%s", s);
		for (int x = 0; x < W; x++) {
			a[y][x] = (s[x] == 'X');
			if (s[x] == 'S') {
				xi[0] = x;
				yi[0] = y;
			}
			if (s[x] >= '1' && s[x] <= '9') {
				int i = s[x] - '0';
				xi[i] = x;
				yi[i] = y;
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < N; i++)
		ans += bfs(xi[i], yi[i], xi[i + 1], yi[i + 1]);
	printf("%d\n", ans);
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<string.h>
using namespace std;
double  cheese[1001][1001];
double  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]){
			if(n==y.d) break;
			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&&check[y.a+q[i]][y.b+p[i]]!=1&&cheese[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*****/
#include <iostream>
#include <fstream>
#include <queue>

/*****マクロ定義*****/
#define START 0
#define EMPTY 10

/*****名前空間*****/
using namespace std;

/*****グローバル変数置き場*****/
char Field[1001][1001];
bool Visited[1001][1001];
int TimeF[1001][1001];

/*****その他関数置き場*****/
int bfs(int sx,int sy,int gx,int gy){
	queue<int> Qx;
	queue<int> Qy;
	int x_now;
	int y_now;
	int x_next;
	int y_next;
	int dx[] = {0,1,0,-1};
	int dy[] = {-1,0,1,0};
	Qx.push(sx);
	Qy.push(sy);
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			Visited[i][j] = false;
			TimeF[i][j] = 0;
		}
	}

	Visited[sy][sx] = true;
	TimeF[sy][sx] = 0;
	while(!(Qx.empty() && Qy.empty())){
		x_now = Qx.front();Qx.pop();
		y_now = Qy.front();Qy.pop();
		//cout << TimeF[y_now][x_now] << endl;
		if(x_now == gx && y_now == gy)	break;
		for(int i=0;i<4;i++){
			x_next = x_now + dx[i];
			y_next = y_now + dy[i];
			if(Field[y_next][x_next] == 'X' || Visited[y_next][x_next]);
			else{
				Qx.push(x_next);
				Qy.push(y_next);
				Visited[y_next][x_next] = true;
				TimeF[y_next][x_next] = TimeF[y_now][x_now] + 1;
			}
		}
	}
	return TimeF[gy][gx];
}
/*****main関数*****/
void main(){
	/*****変数置き場*****/
	int total_time = 0;
	char row[1001];
	int height;
	int width;
	int n;
	int x[] = {0,0,0,0,0,0,0,0,0,0};
	int y[] = {0,0,0,0,0,0,0,0,0,0};
	/*****ファイルオープン*****/	
	ofstream fout("output.txt");
	ifstream fin("input.txt");
	if(!fout || !fin){
		cout << "Can't open the file.\n";
		return;
	}
	
	/*****処理部*****/
	cin >> height >> width >> n;
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			Field[i][j] = 'X';
			Visited[i][j] = false;
			TimeF[i][j] = 0;
		}
	}
	for(int i=0;i<height;i++){
		cin >> row;
		for(int j=0;j<width;j++){
			Field[i+1][j+1] = row[j];
		}
	}
	/*for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			cout << Field[i][j];
		}
		cout << endl;
	}*/
	for(int i=0;i<height+1;i++){
		for(int j=0;j<width+1;j++){
			switch (Field[i][j]){
			case 'S':
				x[START] = j;
				y[START] = i;
				break;
			case '1':
				x[1] = j;
				y[1] = i;
				break;
			case '2':
				x[2] = j;
				y[2] = i;
				break;
			case '3':
				x[3] = j;
				y[3] = i;
				break;
			case '4':
				x[4] = j;
				y[4] = i;
				break;
			case '5':
				x[5] = j;
				y[5] = i;
				break;
			case '6':
				x[6] = j;
				y[6] = i;
				break;
			case '7':
				x[7] = j;
				y[7] = i;
				break;
			case '8':
				x[8] = j;
				y[8] = i;
				break;
			case '9':
				x[9] = j;
				y[9] = i;
				break;
			}
		}
	}
	/*for(int i=0;i<10;i++){
		cout << x[i] << " ";
	}
	cout << endl;
	for(int i=0;i<10;i++){
		cout << y[i] << " ";
	}
	cout << endl;*/
	for(int i=0;i<n;i++){
		total_time += bfs(x[i],y[i],x[i+1],y[i+1]);
	}
	/*for(int i=0;i<20;i++){
		for(int j=0;j<20;j++){
			cout << Visited[i][j];
		}
		cout << endl;
	}*/
	cout << total_time << endl;
	/*****処理終了後*****/
	fout.close();
	fin.close();
	//delete[] pField;
	//delete[] pVisited;
}