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>
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;
				}
			}
		}
	}
	for(i=0;i<size;i++)
	{
		it=tp.begin();
		tp.erase(it);
	}
	//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;
}

C++: //待解，&#35843;用函数 
#include <queue> 
#include <cstdio>
#include <cstring>
#include <iostream>
#define mp          make_pair
using namespace std;
typedef pair<int,int> point;
const int maxn = 1005;
int height,width,n;    //n代表硬度 
char maze[maxn][maxn];
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};
bool visited[maxn][maxn];
int shortDistance(int sx,int sy,int ex,int ey)
{
	//初始化visted数&#32452;
	for(int i = 0;i<height;i++)
		for(int j = 0;j<width;j++)
			visited[i][j] = false;
	visited[sx][sy] = true;
	
	queue<pair<int,point> > q;q.push(mp(0,mp(sy,sx)));
	while(1)
	{
		pair<int,point> a = q.front();q.pop();
		int d=a.first,x=a.second.second,y=a.second.first;
		if(x==ex&&y==ey)
		{
			return d;
		}
		for(int i = 0;i<4;i++)
		{
			int nx = x + dx[i];
			int ny = y + dy[i];
			if(nx>=0&&nx<width&&ny>=0&&ny<height&&maze[ny][nx]!='X'&&!visited[ny][nx])
			{
				visited[ny][nx]=true;
				q.push(mp(d+1,mp(ny,nx)));
			}
		}
	}
}

int main()
{
	int cx[10],cy[10];
	int cnt = 0;
	//freopen("I:\\0558-in2.txt","r",stdin);
	scanf("%d%d%d",&height,&width,&n);
	//&#36755;入迷&#23467; 
	for(int i = 0;i<height;i++)
	{
		scanf("%s",maze[i]);
		//&#26597;找老鼠&#24034;的位置
		for(int j = 0;j<width;j++)
		{
			if(maze[i][j]=='S')
			{
				cy[0] = i;
				cx[0] = j;
			}
			else if(isdigit(maze[i][j]))
			{
				int k = maze[i][j]-'0';
				cx[k] = j;cy[k] = i; 
			}
			
		}
	}
	int cost = 0;
	for(int i = 0;i<n;i++)
		cost += shortDistance(cx[i],cy[i],cx[i+1],cy[i+1]);
	cout << cost <<endl;
	return 0;
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <cstdio>
#include <string.h>
#include <cstring>

using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)

typedef long long ll;
//各地点の行と列をPairで持たせる
typedef pair<int,int> P;
int y,x,n,nx,ny,nextx,nexty;
int ans = 0;
int counter = 0;
int d[1000][1000] = {0};
char c[1100][1100];
int d1[] = {0,0,-1,1};
int d2[] = {1,-1,0,0};
int dc = 0;

int main(){
    cin >> y >> x >> n;
    queue<P> que;
    REP(i,y){
        cin >> c[i];
    }
    REP(i,y){
        bool isFoundStart = false;
        REP(j,x){
            if(c[i][j] == 'S'){
                isFoundStart = true;
                que.push(P(i,j));
                d[i][j] = 0;
                break;
            }
        }
        if(isFoundStart) break;
    }
   
    while(counter < n){
        int memo[1100][1100] = {0};
        while(que.size()){
            P p = que.front(); que.pop();
            ny = p.first;
            nx = p.second;
            dc = d[ny][nx];
 
            if(c[ny][nx] - '0' == counter + 1){
                counter++;
    
                if(counter == n) {
                    cout << dc << endl;
                    return 0;
                }
                queue<P> empty;
                swap(que, empty);
                que.push(P(ny,nx));
                c[ny][nx] = '.';
              
                break;
            }
        
            REP(i,4){
                nextx = nx + d1[i];
                nexty = ny + d2[i];

                if(nexty < 0 || nexty >= y || nextx < 0 || nextx >= x) continue;
                if(c[nexty][nextx] == 'X' || memo[nexty][nextx] != 0) continue;
            
                que.push(P(nexty,nextx));
     
                d[nexty][nextx] = dc + 1;
            
            }
            memo[ny][nx] = 1;
        }
    }
}