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<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stdio.h>
#include<string.h>
#include<set>
#define INF 99999999

using namespace std;

typedef pair<int,int> P;
typedef queue<P> Q;


int H,W,N;
char cmap[1200][1200];
int sx,sy;
int kotae;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int d[1200][1200];

void bfs(int x,int y,int a){
	for(int i=0;i<1200;i++){
		for(int j=0;j<1200;j++){
			d[j][i] = INF;
		}
	}
	d[x][y] = 0;
	Q q;
	q.push(P(x,y));
	while(!q.empty()){
		P p = q.front();q.pop();
		for(int i = 0;i<4;i++){
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if(nx<0 || nx>=W || ny<0 || ny>=H)continue;
			if(cmap[nx][ny] == 'X')continue;
//			printf("d[%d][%d]=%d\n",nx,ny,d[nx][ny]);
//			printf("cmap[%d][%d]=%c\n",nx,ny,cmap[nx][ny]);
//			printf("a=%d\n",a);
			if(cmap[nx][ny] == a+'0'){
//				printf("a\n");
				d[nx][ny] = d[p.first][p.second]+1;
				kotae += d[nx][ny];
//				printf("kotae=%d\n",kotae);
				sx = nx;sy = ny;
				return;
			}
			if(d[nx][ny] == INF){
				d[nx][ny] = d[p.first][p.second]+1;
				q.push(P(nx,ny));
			}
		}
	}
}

int main(){
	cin >> H >> W >> N;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cin >> cmap[j][i];
			if(cmap[j][i] == 'S'){
				sx = j;sy = i;
			}				
		}
	}
	for(int i=0;i<N;i++){
		bfs(sx,sy,i+1);
	}
	cout << kotae << endl;
	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
struct as
{
    int x,y;
}pos[10];
struct node
{
    int x,y;
    int step;
};
bool vis[1010][1010];
char maze[1050][1050];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int H,W,N;

bool check(int x,int y)
{
    if (x<=0 || x>H) return 1;
    if (y<=0 || y>W) return 1;
    if (vis[x][y]) return 1;
    if (maze[x][y] =='X')return 1;
    return 0;
}
int bfs(int m,int n)
{
    memset(vis,0,sizeof vis);
    int sx = pos[m].x, sy = pos[m].y;
    int gx = pos[n].x, gy = pos[n].y;
   // printf("%d %d\n",sx,sy);
   // printf("%d %d\n",gx,gy);
    queue<node> Q;
    node a,next;
    a.step = 0;
    a.x = sx, a.y =sy;
    vis[sx][sy] = 1;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if (a.x == gx && a.y == gy)
            return a.step;
        for(int i = 0; i < 4 ;i++)
        {
            int nx = a.x + dx[i];
            int ny = a.y + dy[i];
            if(check(nx,ny))
                continue;
            else
            {
                vis[nx][ny] =1;
                next.x = nx,next.y = ny,next.step = a.step+1;
                Q.push(next);
            }
        }
    }
    return 999999;
}

int main()
{
   // freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&H,&W,&N))
    {
        int sx,sy;
        fflush(stdin);
        for(int i = 1;i<=H;i++)
        {
            for(int j = 1; j <= W;j++)
            {
                scanf("%c",&maze[i][j]);
                if (maze[i][j] >='1' && maze[i][j]<='9')
                {
                    pos[maze[i][j]-'0'].x = i;
                    pos[maze[i][j]-'0'].y = j;
                }
                if (maze[i][j]=='S')
                    sx=i,sy = j;
            }
            getchar();
        }
        pos[0].x = sx,pos[0].y = sy;

        int ans = 0;
        for(int i = 0;i<N;i++)
            ans += bfs(i,i+1);
        printf("%d\n",ans);
    }
    return 0;
}

/*
入力例 2
4 5 2
.X..1
....X
.XX.S
.2.X.

出力例 2
12
*/

C++: #include <bits/stdc++.h>
typedef std::pair<int, int> P;

#define MAX 1000
#define LEN 10
#define INFTY 10001

char field[MAX][MAX];
P targets[LEN];
int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1};
int H, W, N;
char idxs[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

int bfs(const int &sx, const int &sy, const int &idx) {

    std::queue<P> que;
    int d[H][W];
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INFTY;
        }
    }

    que.push(P(sx, sy));

    d[sx][sy] = 0;

    while (que.size()) {

        P p = que.front(); que.pop();;

        if (p.first == targets[idx].first && p.second == targets[idx].second) break;

        for (int i = 0; i < 4; i++) {

            int nx = p.first + dx[i]; int ny = p.second + dy[i];

            if (0 <= nx && nx < H && 0 <= ny && ny < W && (field[nx][ny] != 'X') && d[nx][ny] == INFTY) {
                que.push(P(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }

    return d[targets[idx].first][targets[idx].second];
}



int main()
{

    int sx, sy; //start point
    std::cin >> H >> W >> N;
    int idx;
    char tmp;
    int sum = 0;

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            std::cin >> tmp;
            field[i][j] = tmp;
            if (tmp != '.' && tmp != 'X') {
                idx = (int) tmp - '0';
                targets[idx] = std::make_pair(i, j);
            }
            if (tmp == 'S') {
                sx = i; sy = j;
            }
        }
    }

   sum += bfs(sx, sy, 1);
   for (int i = 1; i < N; i++) {
       sx = targets[i].first; sy = targets[i].second;
       sum += bfs(sx, sy, i+1);
   }
   std::cout << sum << std::endl;

    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int h,w,n;
char mp[1003][1003];
char vis[1003][1003];
int ans;
struct pt{
	int x,y;
	int st;	
};
pt st;
pt dl[1000005];
int front,back;
pt cun[21];
int dx[5]={0,0,0,1,-1};
int dy[5]={0,1,-1,0,0};
void bfs(int a)
{
	front=back=1;
	dl[back]=cun[a];
	back++;
	memset(vis,0,sizeof(vis));
	while(front<back)
	{
		pt ls=dl[front];
		front++;
		if(mp[ls.x][ls.y]=='0'+a+1)
		{
			ans+=ls.st;
			break;	
		}
		for(int i=1;i<=4;i++)
		{
			int xx=ls.x+dx[i];
			int yy=ls.y+dy[i];
			if(xx>0&&yy>0&&xx<=h&&yy<=w)
			{
				if(mp[xx][yy]!='X'&&vis[xx][yy]==0)
				{
					dl[back]=(pt){xx,yy,ls.st+1};
					back++;
					vis[xx][yy]=1;	
				}
			}
		}
	}
}
int main()
{
	scanf("%d%d%d",&h,&w,&n);
	for(int i=1;i<=h;i++)
	{
		scanf("%s",mp[i]+1);
	}
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			if(mp[i][j]=='S')
			{
				cun[0]=(pt){i,j,0};	
			}else if(mp[i][j]<'9'&&mp[i][j]>'0')
			{
				cun[mp[i][j]-'0']=(pt){i,j,0};	
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		bfs(i);
	}
	printf("%d\n",ans);
	return 0;
}