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 <sstream>
#include <cstdlib>
#include <queue>

using namespace std;

const int INF = 100000000;
const int MAX_W = 1000;
const int MAX_H = 1000;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
    queue<P> que;
    P start;
    int pow = 1;
    while (std::cin >> H >> W >> N, H) {
        for (int i = 0; i < H; i++) {
            std::cin >> field[i];
        }
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                d[i][j] = INF;
                if (field[i][j] == 'S') {
                    start = P(i, j);
                }
            }
        }
        que.push(start);
        d[start.first][start.second] = 0;
        int count = 0;
        while (que.size()) {
            P p = que.front();
            que.pop();
            if (field[p.first][p.second] == '0' + N and N <= pow) {
                count = d[p.first][p.second];
                break;
            }

            for (int i = 0; i < 4; i++) {
                int nx = p.second + dx[i];
                int ny = p.first + dy[i];
                if (nx >= 0 and ny >= 0 and nx < W and ny < H and field[ny][nx] != 'X' and d[ny][nx] == INF) {
                    if (field[ny][nx] == '.') {
                        que.push(P(ny, nx));
                        d[ny][nx] = d[p.first][p.second] + 1;
                    } else {
                        if (field[ny][nx] <= '0' + pow) {
                            cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
                            int s = d[p.first][p.second];
                            field[ny][nx] = '.';
                            for (int i = 0; i < H; i++) {
                                for (int j = 0; j < W; j++) {
                                    d[i][j] = INF;
                                }
                            }


                            while (que.size()) {
                                que.pop();
                            }
                            que.push(P(ny, nx));
                            d[ny][nx] = s + 1;
                            if(pow == N) {
                                field[ny][nx] = '0' + N;
                            }
                            pow++;
                            break;
                        } else {
                            que.push(P(ny, nx));
                            d[ny][nx] = d[p.first][p.second] + 1;
                        }
                    }
                }
            }
        }

        std::cout << count << std::endl;
        break;
    }

    return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
struct dir
{
	int x,y;
}dir[4]={{-1,0},{1,0},{0,-1},{0,1}};
char hole[1005][1005],factory[10][2];
int d[1005][1005],h,w,n;
typedef pair<int,int> P;
int bfs(const int &sx,const int &sy,const int &gx,const int &gy)
{
	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			d[i][j] = -1;
		}
	}
	//memset(d,-1,sizeof(d));
	queue<P> que;
	que.push(P(sx,sy));
	d[sx][sy]=0;
	while(que.size())
	{
		P p=que.front();
		que.pop();	
		if(p.first==gx&&p.second==gy)
		break;
		for(int i=0;i<4;i++)
		{
		int nx=p.first+dir[i].x;
		int ny=p.second+dir[i].y;
		if(nx>=0&&ny>=0&&nx<h&&ny<w&&hole[nx][ny]!='X'&&d[nx][ny]==-1)
		{
			que.push(P(nx,ny));
			d[nx][ny]=d[p.first][p.second]+1;
		}
		}
	}
	return d[gx][gy];
}
int main()
{
	//scanf("%d%d%d\n",&h,&w,&n);
	cin>>h>>w>>n;
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		{
			//scanf("%c",&hole[i][j]);
			cin >> hole[i][j];
			if(hole[i][j]=='S')
			{
				factory[0][0]=i;
				factory[0][1]=j;
			}
			if(isdigit(hole[i][j]))
			{
				int index=hole[i][j]-'0';
			factory[index][0]=i;
			factory[index][1]=j;
			}
		}
	int step=0;
	for(int i=0;i<n;i++)
	step+=bfs(factory[i][0],factory[i][1],factory[i+1][0],factory[i+1][1]);
	cout<<step<<endl;
	//printf("%d\n",step);
	return 0;
 }

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
//#include <map>
#include <set>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef pair<int, int> P;


typedef struct
{
	int i;
	int j;
	int hp;
} STATE;

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

char map[1024][1024];
int H, W, N;
int si, sj;
int d[1024][1024][9];

int search()
{
	queue<STATE> que;
	
	for (int i = 1; i <= H; i++){
		for (int j = 1; j <= W; j++){
			for (int k = 0; k <= 8; k++){
				d[i][j][k] = INF;
			}
		}
	}
	
	STATE tmp = {si, sj, 0};
	que.push(tmp);
	d[si][sj][0] = 0;
	
	while (que.size()){
		STATE st = que.front();
		que.pop();
		
		//printf("%d %d %d\n", st.i, st.j, st.hp);
		
		if (st.hp == N) return (d[st.i][st.j][st.hp]);
		
		for (int i = 0; i < 4; i++){
			int ni = st.i + dy[i];
			int nj = st.j + dx[i];
			int nhp = st.hp + (st.hp == map[ni][nj]);
			
			if (map[ni][nj] != 'X' && d[ni][nj][nhp] == INF){
				STATE tmp = {ni, nj, nhp};
				que.push(tmp);
				d[ni][nj][nhp] = d[st.i][st.j][st.hp] + 1;
			}
		}
	}
	
	return (-1);
}

int main()
{
	for (int i = 0; i < 1024; i++){
		for (int j = 0; j < 1024; j++){
			map[i][j] = 'X';
		}
	}
	
	scanf("%d %d %d", &H, &W, &N);
	for (int i = 1; i <= H; i++){
		for (int j = 1; j <= W; j++){
			scanf(" %c", &map[i][j]);
			
			if (map[i][j] == 'S'){
				si = i;
				sj = j;
			}
			else if (map[i][j] >= '1' && map[i][j] <= '9'){
				map[i][j] -= '1';
			}
		}
	}
	
	printf("%d\n", search());
}