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>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    int h, w, n;
    cin >> h >> w >> n;
    vector <vector <char>> s(h, vector <char> (w));
    vector <pair <int, int>> goals(n);
    pair <int, int> st;
    rep (i, h) rep (j, w) {
        cin >> s[i][j];
        if (s[i][j] == 'S') st = make_pair(i, j);
        if (isdigit(s[i][j])) goals[s[i][j] - '0' - 1] = make_pair(i, j);
    }

    int ans = 0;
    const int INF = 1000000000;
    for (auto & goal : goals) {
        queue <pair <int, int>> que;
        vector <vector <int>> dist(h, vector <int> (w, INF));
        que.push(st);
        dist[st.first][st.second] = 0;

        while(!que.empty()) {
            pair <int, int> now = que.front(); que.pop();
            int dx[] = {  0,  1,  0, -1},
                dy[] = { -1,  0,  1,  0};

            rep (i, 4) {
                int tox = now.first + dx[i], toy = now.second + dy[i];
                if (tox < 0 || h <= tox || toy < 0 || w <= toy) continue;
                if (s[tox][toy] == 'X') continue;
                if (dist[tox][toy] > dist[now.first][now.second] + 1) {
                    dist[tox][toy] = dist[now.first][now.second] + 1;
                    que.emplace(tox, toy);
                }
            }
        }

        st = goal;
        ans += dist[goal.first][goal.second];
    }
    cout << ans << endl;

    return 0;
}

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

const int INF=1000000000;
const int muki[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

queue<pair<int,int>> que;
char a[1002][1002];
int cost[1002][1002];
bool used[1002][1002];

int return_cost(pair<int,int> S,pair<int,int> G){
	while(!que.empty())que.pop();
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++){ cost[i][j]=INF; used[i][j]=false; }
	que.push(S);
	cost[S.first][S.second]=0;
	while(!que.empty()){
		pair<int,int> p=que.front(); que.pop();
		if(p==G)break;
		int x=p.first,y=p.second;
		if(!used[x][y]){
			for(int i=0;i<4;i++){
				int nx=x+muki[i][0],ny=y+muki[i][1];
				if(a[nx][ny]!='X'&&cost[nx][ny]>cost[x][y]+1){
					cost[nx][ny]=cost[x][y]+1;
					que.push(pair<int,int>(nx,ny));
				}
			}
			used[x][y]=true;
		}
	}
	return cost[G.first][G.second];
}

int main(){
	int h,w,n;
	pair<int,int> p[12];
	for(int i=0;i<1002;i++)for(int j=0;j<1002;j++)a[i][j]='X';

	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=h;i++){
scanf("\n");
	for(int j=1;j<=w;j++){
		scanf("%c",&a[i][j]);
		if(a[i][j]=='S'){
			p[0]=pair<int,int>(i,j);
		}
		else if('1'<=a[i][j]&&a[i][j]<='9'){
			p[a[i][j]-'1'+1]=pair<int,int>(i,j);
		}
	}
	}

	int ret=0;
	for(int i=0;i<n;i++){
		ret+=return_cost(p[i],p[i+1]);
	}

	printf("%d\n",ret);
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

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

struct OBJ{
public:
	OBJ(char a, bool b):kind(a), mark(b){}
	char kind;
	bool mark;
};

void dfs(char target, pair<int, int> &pos, vector<vector<OBJ> > &field, int count){
	if(field[pos.second][pos.first].kind == target){
		mincount = mincount > count ? count : mincount;
		return;
	}
	field[pos.second][pos.first].mark = true;
	for(int i = 0; i < 4; i++){
		if((pos.first+dx[i]<0||pos.first+dx[i]>=field[0].size())||(pos.second+dy[i]<0||pos.second+dy[i]>=field.size())) continue;
		if(field[pos.second+dy[i]][pos.first+dx[i]].kind != 'X' && !field[pos.second+dy[i]][pos.first+dx[i]].mark){
			pair<int, int> tmp(pos.first+dx[i], pos.second+dy[i]);
			dfs(target, tmp, field, count + 1);
		}
	}
	field[pos.second][pos.first].mark = false;
}

pair<int, int> decideStart(char target, vector<vector<OBJ> > &field){
	for(int i = 0; i < field.size(); i++){
		for(int j = 0; j < field[i].size(); j++){
			if(field[i][j].kind == target) return pair<int, int>(j, i);
		}
	}
}

int main(){
	int h = 0, w = 0, n = 0;
	cin >> h >> w >> n;
	cin.ignore();
	pair<int, int> start;
	vector<vector<OBJ> > field(h, vector<OBJ>(w, OBJ(' ', false)));
	for(int i = 0; i < h; i++){
		string tmp;
		getline(cin, tmp);
		for(int j = 0; j < w; j++){
			field[i][j].kind = tmp[j];
			if(tmp[j] == 'S') start = pair<int, int>(j, i);
		}
	}
	int count = 0;
	for(int i = 1; i <= n; i++){
		mincount = h*w;
		dfs(i+'0', start, field, 0);
		count += mincount;
		start = decideStart(i+'0', field);
	}

	cout << count << endl;

	return 0;
}

C++: #include<queue>
#include<cctype>
#include<cstdio>

#define mp           make_pair
#define rep(i,n)     for(int i = 0;i<n;i++)

using namespace std;

typedef pair<int , int > pii;


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

int h,w;
char grid[1010][1010];

int shortestDistance(int sx,int sy,int gx,int gy)
{
	static bool visited[1010][1010];
	rep(i,h)rep(j,w)  visited[i][j] = false;
	visited[sy][sx] = true;
	
	queue< pair< int ,pii > > qu;
	qu.push(mp(0,mp(sy,sx)));
	while(1)
	{
		pair<int ,pii> a = qu.front();
		qu.pop();
		int d = a.first,x = a.second.second,y = a.second.first;
		if(x == gx && y == gy) return d;
		
		rep(i,4)
		{
			int xx = x + dx[i], yy = y + dy[i];
			 if(0<=xx && xx<w && 0<=yy && yy<h && grid[yy][xx]!='X' && !visited[yy][xx])
			{
                visited[yy][xx]=true;
                qu.push(mp(d+1,mp(yy,xx)));
            }

		}
	}
}



int main(){
    int n;  scanf("%d%d%d",&h,&w,&n);
    int cx[10],cy[10];
    rep(i,h){
        scanf("%s",grid[i]);
        rep(j,w){
            if(grid[i][j]=='S')     cx[0]=j,cy[0]=i;
            if(isdigit(grid[i][j])) cx[grid[i][j]-'0']=j,cy[grid[i][j]-'0']=i;
        }
    }
 
    int cost=0;
    rep(i,n)    cost+=shortestDistance(cx[i],cy[i],cx[i+1],cy[i+1]);
    printf("%d\n",cost);
 
    return 0;
}