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.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;
	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.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;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <sstream>
using namespace std;

struct Node{
	int x,y;
};

const int max_n=1000+10;
int h,w,n,dx[]={0,0,-1,1},dy[]={-1,1,0,0},d[max_n][max_n],ans=0;
Node sN;
char c[max_n][max_n];

int bfs(int m){
	for(int i=0;i<h;i++)
	    for(int j=0;j<w;j++)
	        d[i][j]=0;
	queue<Node> q;
	q.push(sN);
	d[sN.x][sN.y]=1;
	while(!q.empty()){
		Node p=q.front();
		q.pop();
		
		if(c[p.x][p.y]=='0'+m){
		    sN.x=p.x;
		    sN.y=p.y;
		}
		else{
			for(int i=0;i<4;i++){
				Node nN;
				nN.x=p.x+dx[i];
				nN.y=p.y+dy[i];
				if(nN.x>=0 && nN.x<h 
	            && nN.y>=0 && nN.y<w 
			   	&& c[nN.x][nN.y]!='X' && !d[nN.x][nN.y]){
					q.push(nN);
					d[nN.x][nN.y]=d[p.x][p.y]+1;
				}
			}
		}
	}
	return d[sN.x][sN.y]-1;
}

int main(){
    scanf("%d%d%d",&h,&w,&n);
    for(int i=0;i<h;i++)
        scanf("%s",c[i]);
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++)
            if(c[i][j]=='S'){
            	sN.x=i;
            	sN.y=j;
            	break;
            }
    for(int i=1;i<=n;i++)ans+=bfs(i);
    cout<<ans<<endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
struct k{
	int x,y,c;
};
queue<k> q;
int p[1002][1002],m[1002][1002];
int main(void) {
	k z,z1;
	char d;
	int v=1,h,w,n,i,j,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
	cin >> h >> w>>n;
	for (i = 0; i != w + 2; i++) {
		for (j = 0; j != h + 2; j++){
			p[j][i] = 99;
			m[j][i]=0;
		}
	}for (i = 1; i != w + 1; i++) {
		for (j = 1; j != h + 1; j++){
			m[j][i]=99;
		}
	}
	for (i = 1; i <= h; i++) {
		for (j = 1; j <= w; j++){
			cin >> d;
			switch(d){
				case 'S':
					z.x=j;
					z.y=i;
					z.c=0;
					p[i][j]=-1;
					break;
				case '.':
					p[i][j]=-1;
					break;
				case 'X':
					p[i][j]=99;
					break;
				case '1':
					p[i][j]=1;
					break;
				case '2':
					p[i][j]=2;
					break;
				case '3':
					p[i][j]=3;
					break;
				case '4':
					p[i][j]=4;
					break;
				case '5':
					p[i][j]=5;
					break;
				case '6':
					p[i][j]=6;
					break;
				case '7':
					p[i][j]=7;
					break;
				case '8':
					p[i][j]=8;
					break;
				case '9':
					p[i][j]=9;
					break;
			}
		}
	}
	q.push(z);
	while(!q.empty()){
			z=q.front();
			q.pop();
			if(m[z.y][z.x]==v)
				continue;
			if(p[z.y][z.x]==v){
				if(p[z.y][z.x]==n)
					break;
				while(!q.empty())
					q.pop();
				q.push(z);
				v++;
				//printf("%d %d %d\n",z.x,z.y,z.c);
			}
		//	cout<<p[z.y][z.x]<<" "<<z.y<<" "<<z.x<<endl;
			m[z.y][z.x]=v;
			for(i=0;i!=4;i++){
				if(p[z.y+dy[i]][z.x+dx[i]]!=99/*&&z.x+dx[i]!=v&&z.y+dy[i]!=v*/){
					z1.x=z.x+dx[i];
					z1.y=z.y+dy[i];
					z1.c=z.c+1;
					q.push(z1);
				}
			}
	}
	cout <<z.c<< endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int INF=1e7;
int h,w,n;
char m[1001][1001];
pii c[11];
int d[1001][1001];
int dy[4]={0,0,1,-1};
int dx[4]={1,-1,0,0};
int ans=0;

void bfs(){
	rep(i,0,h){
		rep(j,0,w){
			if(m[i][j]=='S'){
				c[0].fi=i; c[0].se=j;
			}else if('1'<=m[i][j] && m[i][j]<='9'){
				c[m[i][j]-'0'].fi=i;
				c[m[i][j]-'0'].se=j;
			}
		}
	}
	rep(i,0,n){
		rep(j,0,h) rep(k,0,w) d[j][k]=INF;
		d[c[i].fi][c[i].se]=0;
		int gy=c[i+1].fi;
		int gx=c[i+1].se;
		//o("    "<<i<<" "<<c[i].fi<<c[i].se);
		queue<pii> q;
		q.push(c[i]);
		while(q.size()){
			pii t=q.front(); q.pop();
			if(t.fi==gy && t.se==gx){
				ans+=d[gy][gx];
				//o("ok");
				break;
			}
			rep(j,0,4){
				int ny=t.fi+dy[j];
				int nx=t.se+dx[j];
				if(0<=ny && ny<h && 0<=nx && nx<w){
					if(m[ny][nx]!='X' && d[ny][nx]==INF){
						d[ny][nx]=d[t.fi][t.se]+1;
						//o(i<<" "<<ny<<" "<<nx<<" "<<d[ny][nx]);
						q.push(pii(ny,nx));
					}
				}
			}
		}
	}
}

signed main(){
	cin>>h>>w>>n;
	rep(i,0,h){
		rep(j,0,w){
			cin>>m[i][j];
		}
	}
	bfs();
	o(ans);
}