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 <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <deque>
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(deque<Point> &tp)
{
	counter++;
	int i,j;
	Point inp;
	int size = tp.size();

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

int main(void)
{
	int i,j;
	deque<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.y = i;
				inp.x = j;
				tp.push_back(inp);
				mfield[inp.y][inp.x] = life;
			}
		}
	}//üÍIíè

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

	return 0;
}

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;

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>=h||x<0||y>=w||y<0||ma[x][y]=='X')
                continue;
            if(ma[x][y]==ed)
            {
                start.x=x;
                start.y=y;
                start.time=no.time+1;
                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", & h, & w, & n))
    {
        for(int i=0;i<h;i++)
        {
            scanf("%s",&ma[i]);
            for(int j=0;j<w;j++)
                if(ma[i][j]=='S')
                {
                    start.x=i;
                    start.y=j;
                    start.time=0;
                }

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

            printf("%d\n",start.time);
    }
}

C++: #include<bits/stdc++.h>
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;pair<int,int> p[12];bool t[1010][1010],v[1010][1010];queue<pair<int,pair<int,int> > >Q;int main(){int h,w,k,a=0,dx[]={0,1,0,-1},dy[]={1,0,-1,0};cin>>h>>w>>k;rep(i,0,h)rep(j,0,w){t[i][j]=true;char z;cin>>z;if(z=='X')t[i][j]=0;if(z=='S')p[0]=make_pair(i,j);if(z>'0'&&z<='9')p[z-'0']=make_pair(i,j);}rep(r,0,k){Q.push(make_pair(0,p[r]));rep(i,0,h)rep(j,0,w)v[i][j]=0;v[p[r].x][p[r].y]=1;while(1){int L=Q.front().x,Y=Q.front().y.x,X=Q.front().y.y;if(Y==p[r+1].x&&X==p[r+1].y){a+=L;break;}rep(i,0,4){int f=Y+dy[i],g=X+dx[i];if(f>=0&&f<h&&g>=0&&g<w)if(t[f][g]==1&&v[f][g]==0)v[f][g]=1;Q.push(make_pair(L+1,make_pair(f,g)));}Q.pop();}while(!Q.empty())Q.pop();}cout<<a<<endl;return 0;}

C++: #include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define INF 0xfffffff
typedef long long int lli;
typedef pair<lli,lli> P;
lli ans,sgx[10],sgy[10],w,h,n,dtmp[1000][1000],d[1000][1000],sx,sy,gx,gy,nx,ny;
lli dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char s[1001];
queue <P> que;
int main()
{
	scanf("%lld %lld %lld",&h,&w,&n);
	for(int i=0;i<h;i++){
		scanf("%s",s);
		for(int j=0;j<w;j++){
			if(s[j]=='X') dtmp[i][j]=-1;
			if(s[j]=='.') dtmp[i][j]=INF;
			if('1'<=s[j]&&s[j]<='9'){
				dtmp[i][j]=INF;
				sgx[s[j]-'0']=j;
				sgy[s[j]-'0']=i;
			}
			if(s[j]=='S'){
				dtmp[i][j]=INF;
				sgx[0]=j;
				sgy[0]=i;
			}
		}
	}
	for(int i=0;i<n;i++){
		while(!que.empty()) que.pop();
		for(int j=0;j<h;j++) for(int k=0;k<w;k++) d[j][k]=dtmp[j][k];
		sx=sgx[i];
		sy=sgy[i];
		gx=sgx[i+1];
		gy=sgy[i+1];
		d[sy][sx]=0;
		que.push(P(sx,sy));
		while(1){
			P p=que.front();
			que.pop();
			nx=p.first;
			ny=p.second;
			for(int j=0;j<4;j++){
				if(0<=nx+dx[j]&&0<=ny+dy[j]&&nx+dx[j]<w&&ny+dy[j]<h&&d[ny+dy[j]][nx+dx[j]]>d[ny][nx]+1){
					d[ny+dy[j]][nx+dx[j]]=d[ny][nx]+1;
					if(nx+dx[j]==gx&&ny+dy[j]==gy){
						ans+=d[ny+dy[j]][nx+dx[j]];
						goto next;
					}
					que.push(P(nx+dx[j],ny+dy[j]));
				}
			}
		}
		next: ;
	}
	printf("%lld\n",ans);
}