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++: 
/* AOJ0558 Cheese */

 

#include <iostream>

#include <cctype>

#include <queue>

#include <string.h>

 

using namespace std;

 

const int DIRECTSIZE = 4;

struct _direct {

    int drow;

    int dcol;

} direct[DIRECTSIZE] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

 

const int N = 1000;

const int N2 = 10;

char grid[N][N], visited[N][N];

int h, w, n;

struct _factory {

    int row, col;

} factory[N2];

 

struct _node {

    int row, col, level;

};

 

int bfs(int n, char goal)

{

    memset(visited, 0, sizeof(visited));

 

    _node start={factory[n-1].row, factory[n-1].col, 0}, temp;

 

    visited[start.row][start.col] = 1;

 

    queue<_node> q;

    q.push(start);

 

    while(!q.empty()) {

        _node front = q.front();

        q.pop();

 

        for(int i=0; i<DIRECTSIZE; i++) {

            int nextrow = front.row + direct[i].drow;

            int nextcol = front.col + direct[i].dcol;

            if( 0 <= nextrow && nextrow < h && 0 <= nextcol && nextcol < w) {

                if(grid[nextrow][nextcol] == goal) {

                    return front.level + 1;

                } else if(visited[nextrow][nextcol] == 0 && grid[nextrow][nextcol] != 'X') {

                    visited[nextrow][nextcol] = 1;

                    temp.row = nextrow;

                    temp.col = nextcol;

                    temp.level = front.level + 1;

                    q.push(temp);

                }

            }

        }

    }

 

    return 0;

}

 

int main()

{

    while(cin >> h >> w >> n) {

        for(int i=0; i<h; i++)

            for(int j=0; j<w; j++) {

                cin >> grid[i][j];

 

                if(grid[i][j] == 'S') {

                    factory[0].row = i;

                    factory[0].col = j;

                } else if(isdigit(grid[i][j])) {

                    factory[grid[i][j] - '0'].row = i;

                    factory[grid[i][j] - '0'].col = j;

                }

            }

 

        int minstep = 0;

        char goal = '1';

        for(int i=1; i<=n; i++) {

            minstep += bfs(i, goal);

            goal++;

        }

 

        cout << minstep << endl;

    }

 

    return 0;

}

C++: #include<stdio.h>

#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
struct PP{
	int x;
	int y;
	int Z;
	int c;
};
char s[1002][1002],C[11]={"0123456789"};
int A[1002][1002],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));
		for(i=0;i<h;i++){
		scanf("%s",s[i]);
	}
	for(i=0;i<=h;i++){
		for(j=0;j<=w;j++){
					A[i][j]=0;
		if(s[i][j]=='S'){
			S.x=j;
			S.y=i;
			S.Z=0;
			S.c=1;
			A[S.y][S.x]=1;
			P.push(S);
		}
		}
	}
	while(1){
		l++;
		
		if(l==4) {
			l=0;
			P.pop();
		}
		S=P.front();

		S.x+=x[l];
		S.y+=y[l];
		S.Z++;
		if(A[S.y][S.x]==1||s[S.y][S.x]=='X'||S.y==h||S.x==w||S.x==-1||S.y==-1){
		continue;
		}
				A[S.y][S.x]=1;
		if(C[S.c]==s[S.y][S.x]){
				S.c++;
				if(S.c==d+1){
				break;
				}
				while(!P.empty()){
				P.pop();
				
				}
				for(i=0;i<=h;i++){
					for(j=0;j<=h;j++){
						A[i][j]=0;
					}
				}
			l=-1;
		

		} 
		P.push(S);
	}
	printf("%d\n",S.Z);
	return 0;
}

C++: #include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <queue>
#include <iostream>


#define MAX_H 1100
#define MAX_W 1100


struct status
{
  int x;
  int y;
  int step;
  int n;
};


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

  
int H,W,N;
char maze[MAX_H][MAX_W];
int flag[MAX_H][MAX_W];
int xs,ys;
std::queue<status> Q;


int BFS();

int main(int argc, char* argv[])
{
  //freopen("in.txt","r",stdin);
  while(std::cin>>H>>W>>N)
    {

      std::cin.get();
      //std::cout << H << W << N << std::endl;

      for(int i = 0 ; i < H ; i++)
	std::cin.getline(maze[i],MAX_W);

      //for(int i = 0 ; i < H ; i++)
      //	std::cout << maze[i] << std::endl;

      for(int j = 0 ; j < H ; j++)
	for(int i = 0 ; i < W ; i++)
	  if(maze[j][i] == 'S' )
	    {
	      xs = i;
	      ys = j;
	      break;
	    }
      memset(flag,0,sizeof(flag));
      std::cout<<BFS()<<std::endl;
      
    }
}


int BFS()
{

  while(Q.empty()==false)
    Q.pop();
  status start;
  start.x = xs;
  start.y = ys;
  start.step = 0;
  start.n = 1;

  Q.push(start);

  while(Q.empty() == false )
    {
      status tmp = Q.front();
      Q.pop();


      for(int i = 0 ; i < 4 ; i++)
	{
	  int cx = tmp.x + dx[i];
	  int cy = tmp.y + dy[i];
	  int cstep = tmp.step + 1;
	  int cn = tmp.n;

	  if(cx < 0 || cx >= W ) continue;
	  if(cy < 0 || cy >= H ) continue;
	  if(maze[cy][cx] == 'X' ) continue;

	  if( (maze[cy][cx] - '0') == tmp.n )
	    cn = tmp.n + 1;
	  else
	    cn = tmp.n;

	  if(cn == N+1)
	    return cstep;

	  
	  
	  if(flag[cy][cx] >= cn  ) continue;

	  flag[cy][cx] = cn;

	  status nstatus;
	  nstatus.x = cx;
	  nstatus.y = cy;
	  nstatus.n = cn;
	  nstatus.step = cstep;
	  Q.push(nstatus);
	  
	}
    }

  return -1;
  
}