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

char ma[1005][1005];
int vis[1005][1005];
int n,m,t,ax,ay,c;
struct node
{
    int x,y,ti;//位置及??
    node(int a,int b,int c){x=a;y=b;ti=c;}
};

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

int bfs(int xx,int yy)
{
    memset(vis,0,sizeof(vis));
    queue<node> que;
    que.push(node(xx,yy,0));
    vis[xx][yy]=1;
    while(!que.empty())
    {
        node tem=que.front();
        que.pop();
        //vis[tem.x][tem.y]=0;
        for(int i=0;i<4;i++)
        {
            if(tem.x+dx[i]>=0&&tem.x+dx[i]<n&&tem.y+dy[i]>=0&&tem.y+dy[i]<m)//没越界
            {
                int x=tem.x+dx[i];
                int y=tem.y+dy[i];
                if(vis[x][y])continue;//如果?个点在?????个方向
                if(c==ma[x][y]-'0')//如果下一个是?点的?直接返回
                {
                    ax=x;ay=y;
                    return tem.ti+1;
                }
                if(ma[x][y]=='X')continue;//如果是?的?，那?只能?个方向

                else//放入?列就好了
                     que.push(node(x,y,tem.ti+1));
                vis[x][y]=1;

            }
        }
    }
}

int main()
{
    freopen("1.txt","r",stdin);
    while(cin>>n>>m>>t)
    {
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            scanf("%s",&ma[i]);
            for(int j=0;j<m;j++)
                if(ma[i][j]=='S')
            {
                ax=i;ay=j;
            }
        }
        int ans=0;
        for(int i=1;i<=t;i++)
        {
            c=i;
            ans+=bfs(ax,ay);
            //cout<<ans<<endl;
        }

        cout<<ans<<endl;


    }
}

C++: #include<bits/stdc++.h>
#define int long long int

using namespace std;

const int INF = 100000000;

typedef pair<int ,int> P;
char masu[1003][1003];
int W,H,K,ans=0;
int sx[10],sy[10];
int gx[10],gy[10];

int d[1003][1003];

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

queue<P> que;

void sho(){
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            d[j][i]=INF;
        }
    }
    while(que.size()){
        que.pop();
    }
}

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

signed main(){
    cin >> H >> W >> K;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin >> masu[j][i];
            int a=masu[j][i]-'0';
            if(isdigit(masu[j][i])){
                gx[a]=j,gy[a]=i,sx[a]=j,sy[a]=i;
            }else if(masu[j][i]=='S'){
                sx[0]=j,sy[0]=i;
            }
        }
    }
    int answer = bfs();
    cout << answer << endl;
return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <cstdio>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point
{
	int x;
	int y;
};

int dx[] = {0,-1,0,1};//ã©çvñè
int dy[] = {1,0,-1,0};
string field[1000];
int mfield[1001][1001] = {};
int life=1;
int counter=0;
int csum = 0;
int H,W,N;


int open(vector<Point> &tp)
{
	counter++;
	int i,j;
	Point inp;
	int size = tp.size();
	vector<Point>::iterator it;

	for(i=0;i<size;i++)
	{
		for(j=0;j<4;j++)
		{
			inp.x = tp[i].x+dx[j];
			inp.y = tp[i].y+dy[j];
			if(inp.x<0 || inp.y<0 || inp.x>=H || inp.y>=W)
				continue;
			if(field[inp.x][inp.y] != CANTGO && field[inp.x][inp.y] != START  && mfield[inp.x][inp.y] != life)
			{
				//v·éÉÅZ£Ú®Â\][
				if(field[inp.x][inp.y] == life + '0' ) //©ªÉ©Á½`[YÌ­©
				{
					tp.clear();
					tp.push_back(inp);
					return counter;
				}
				else
				{
					tp.push_back(inp);
					mfield[inp.x][inp.y] = life;
				}
			}
		}
	}
	tp.erase(tp.begin(),tp.begin()+size);
	//copy(tp.begin() , tp.end() , tp.begin());
	return 0;
}

int main(void)
{
	int i,j;
	vector<Point> tp;
	Point inp;
	Point gool;
	int count = 0;
	cin >> H >> W >> N;

	for(i=0;i<H;i++)//üÍ
	{
		cin >> field[i];
		for(j=0;j<W;j++)
		{
			if(field[i][j] == 'S')
			{
				inp.x = i;
				inp.y = j;
				tp.push_back(inp);
				mfield[inp.x][inp.y] = life;
			}
		}
	}//üÍIíè

	while(1)
	{
		if(open(tp) != 0)
		{
			life++;
			csum += counter;
			counter = 0;
		}
		if(life > N)
			break;
	}
		cout << csum << endl;

	return 0;
}