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<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
#include<string.h>
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1000][1000],C[11]={"0123456789"};
int D[1000][1000];
int A[1000][1000],d;
int main(){
	PP S;
	queue<PP> P;
	int  w,h;
	int d=0,i=0,j=0,l=-1;
	int x[4]={ 1, 0,-1,0};
	int y[4]={ 0 ,1,0,-1};
	scanf("%d %d %d",&h,&w,&d);
	memset(A,0,sizeof(A));
   memset(D,0,sizeof(D));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();
		A[S.y][S.x]=1;
		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		
		if(A[S.y][S.x]==1) continue;
		
		if(s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
			S.x-=x[l];
			S.y-=y[l];
		}
		else if(s[S.y][S.x]=='.'){}
		else{
			if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				printf("%d\n",S.Z);
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
			memset(A,0,sizeof(A));
			l=-1;
			}

		} 
		P.push(S);
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;

struct OZAWA
{
	int x;
	int y;
	int c;
};
int Flag[1000][1000];
char MAP[1000][1000];
queue<OZAWA> Q;
OZAWA ba,v;
int main(void)
{
	int H,W,N,Nc=0,X[10],Y[10];
	int mv[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
	char Cheese[10] ={'1','2','3','4','5','6','7','8','9'};
	memset(MAP,'X',sizeof(MAP));
	memset(Flag,0,sizeof(Flag));
	scanf("%d %d %d",&H,&W,&N);
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			//printf("%d %d ",i,j);
			cin >> MAP[i][j];
			//printf("Q");
			if(MAP[i][j] == 'S'){
				ba.x = j;
				ba.y = i;
				ba.c = 0;
				Flag[i][j] = 1;
				Q.push(ba);
				//printf("Sin\n");
			}
			for(int ii=0;ii<9;ii++){
				if(MAP[i][j] == Cheese[ii]){

					X[ii] = j;
					Y[ii] = i;
					//printf("\n@@@\n");
				}
			}
		}
	}
	//printf("S %d %d\n",ba.x,ba.y);
	//for(int ii=0;ii<2;ii++)
	//printf("%d %d\n",X[0],Y[0]);
	while(!Q.empty()){
		//printf("QA\n");
		ba = Q.front();
		Q.pop();
		if(ba.x==X[Nc] && ba.y==Y[Nc]){
			//printf("OZA");
			Nc++;
			memset(Flag,0,sizeof(Flag));
			while(!Q.empty()){
				Q.pop();
			}
			if(Nc == N) break;
			Q.push(ba);
			Flag[ba.y][ba.x] = 1;
		}
		//printf("aaa\n");
		//if(Nc == N) break;
		for(int i=0;i<4;i++){
			if(MAP[ba.y+mv[i][1]][ba.x+mv[i][0]]!='X' && Flag[ba.y+mv[i][1]][ba.x+mv[i][0]]==0 && (ba.x+mv[i][0])>=0&&(ba.x+mv[i][0])<W&&(ba.y+mv[i][1])>=0&&(ba.y+mv[i][1])<H){
				v.x=ba.x+mv[i][0];
				v.y=ba.y+mv[i][1];
				v.c=ba.c+1;
				Q.push(v);
				Flag[v.y][v.x] = 1;
				//printf("%d %d\n",v.x,v.y);
			}
		}
	}
	cout <<ba.c<<endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
//#include <functional>
#include <cstdio>
#include <queue>
#include <fstream>
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 csum = 0;
int H,W,N;


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

int main(void)
{
	int i,j;
	queue<Point> tp;
	Point top[10];
	Point inp;
	int count = 0;
	int counter=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')
			{
				top[0].y = i;
				top[0].x = j;
				mfield[j][i] = life;
			}
			if(field[i][j] >= '0' && field[i][j] <= '9')
			{
				top[field[i][j] - '0'].y = i;
				top[field[i][j] - '0'].x = j;
			}
		}
	}//üÍIíè

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

	return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<limits.h>
#include<queue>
struct S{int x,y,s;S(int x,int y,int s):x(x),y(y),s(s){}};
char o[1002][1003],*p;
int h,w;
int F(int sx,int sy,int gx,int gy)
{
	S s(sx,sy,0);
	std::queue<S>q;
	static int d[1001][1001],i,j,a[]={-1,0,1,0};
	for(i=1;i<=h;++i)for(j=1;j<=w;++j)d[i][j]=INT_MAX;
	q.push(s);
	while(!q.empty())
	{
		s=q.front();q.pop();
		if(o[s.y][s.x]=='X' || d[s.y][s.x]<=s.s)continue;
		d[s.y][s.x]=s.s;
		for(i=0;i<4;++i) q.push(S(s.x+a[i], s.y+a[(i+1)%4],s.s+1));
	}
	return d[gy][gx];
}
int main()
{
	int n,i,j,x[10],y[10],z,s=0;
	scanf("%d%d%d",&h,&w,&n);
	for(i=0;i<h;++i)
	{
		scanf("%s",&o[i+1][1]);
		p=&o[i+1][1];
		while(p=strpbrk(p,"S123456789"))
			z=isdigit(*p)?*p-'0':0,y[z]=i+1,x[z]=p-o[i+1],++p;
	}
	for(j=0;j<w+2;++j)o[0][j]=o[h+1][j]='X';
	for(i=0;i<h+2;++i)o[i][0]=o[i][w+1]='X';
	for(i=0;i<n;++i)s+=F(x[i],y[i],x[i+1],y[i+1]);
	printf("%d\n",s);
	return 0;
}