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 <stdio.h>
#include <cstring>
#include <queue>
using namespace std;

char ma[1005][1005];int vis[1005][1005];
int w,h,n,ans;

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

struct node
{
    int x,y,time;
};
node start;

void bfs(char ed)
{
    queue<node> que;
    que.push(start);
    vis[start.x][start.y]=1;
    while(!que.empty())
    {
        node no=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int x=no.x+dx[i];
            int y=no.y+dy[i];
            if(!vis[x][y]&&x<w&&x>=0&&y<h&&y>=0&&ma[x][y]!='X')
            {
                if(ma[x][y]==ed)
            {
                start.x=x;
                start.y=y;
                ans+=no.time+1;
                start.time=0;
                return ;
            }
            vis[x][y]=1;
            node no1;
            no1.x=x;no1.y=y;no1.time=no.time+1;
            que.push(no1);

            }

        }
    }
}

int main()
{
    while(~scanf("%d %d %d", & w, & h, & n))
    {
        for(int i=0;i<w;i++)
        {
            scanf("%s",&ma[i]);
            for(int j=0;j<h;j++)
                if(ma[i][j]=='S')
                {
                    start.x=i;
                    start.y=j;
                    start.time=0;

                }

        }
        ans=0;
            for(int i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                bfs(i+'1');
            }

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

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>

using namespace std;

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

#define INF 1001001001
#define LLINF 1001001001001001001LL

typedef pair<int,int> P;

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

//------------------HEAD---------------------

int H,W;
char field[1002][1002];
P start;
P goal;

int d[1000][1000];

int bfs(){
    queue<P> que;
    
    rep(i,H)rep(j,W)d[i][j]=INF;
    que.push(start);
    d[start.first][start.second]=0;
    
    while(!que.empty()){
        P p=que.front();que.pop();
        //cerr<<"l:40 "<<p.first<<" "<<p.second<<endl;
        
        if(p==goal)break;
        
        rep(i,4){
            int nx=p.first+dx[i];
            int ny=p.second+dy[i];
            //if(nx==0&&ny==3)cerr<<"l:49 "<<(0<=nx&&nx<H)<<(0<=ny&&ny<W)<<(field[nx][ny]!='X')<<(d[nx][ny]==INF)<<endl;
            if(0<=nx&&nx<H&&0<=ny&&ny<W&&field[nx][ny]!='X'&&d[nx][ny]==INF){
                
                que.push(P(nx,ny));
                d[nx][ny]=d[p.first][p.second]+1;
            }
        }
        
    }
    return d[goal.first][goal.second];
}

int main(void){
    int N;
    cin>>H>>W>>N;
    rep(i,H)cin>>field[i];
    P cheeses[10];
    rep(i,H)rep(j,W){
        if(field[i][j]=='S')start=P(i,j);
        int t=int(field[i][j]-'1');
        if(0<=t&&t<=8)cheeses[t]=P(i,j);
    }
    int ans=0;
    rep(i,N){
        goal=cheeses[i];
        int t=bfs();
        ans+=t;
        //cerr<<t<<endl;
        start=cheeses[i];
    }
    cout<<ans<<endl;
    // Here your code !
    
}

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

struct P{
	int x, y;
};

const int MAX_H = 1000, MAX_W = 1000, MAX_CN = 10, INF = 1<<24;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int h, w, cn;
int map[MAX_H][MAX_W], d[MAX_H][MAX_W];
P ps[MAX_CN];
//ps x y の順
//map 通れる1 通れない0
//used 通ってない0 通った1
//d d[y][x] spから(x,y)までの最短距離

int main(){
	std::cin >> h >> w >> cn;
	for(int i=0;i<h;i++){
		std::fill(map[i], map[i]+w, 0);
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			char c;
			std::cin >> c;
			if(c == 'S'){
				ps[0].x = j;
				ps[0].y = i;
				map[i][j] = 1;
			}else if('1' <= c && c <= '9'){
				ps[c-'0'].x = j;
				ps[c-'0'].y = i;
				map[i][j] = 1;
			}else if(c == '.'){
				map[i][j] = 1;
			}
		}
	}

	int res = 0;
	for(int i=0;i<cn;i++){//ps[i] -> ps[i+1]への最短路を調べる．それを足せばいい
		P sp = ps[i], gp = ps[i+1];
		for(int i=0;i<h;i++){
			std::fill(d[i], d[i]+w, INF);
		}
		d[sp.y][sp.x] = 0;
		std::queue<P> pq;
		pq.push(sp);

		while(!pq.empty()){
			P p = pq.front();pq.pop();
			if(p.x == gp.x && p.y == gp.y){
				break;
			}
			for(int i=0;i<4;i++){
				int nx = p.x+dx[i], ny = p.y+dy[i];
				if(nx >= 0 && nx < w && ny >= 0 && ny < h && map[ny][nx] && d[ny][nx] == INF){
					pq.push({nx, ny});
					d[ny][nx] = d[p.y][p.x] + 1;
				}
			}
		}

		res += d[gp.y][gp.x];
	}
	
	std::cout << res << std::endl;
}