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 <stack>
#include <math.h>
#include <queue>

using namespace std;

int height,width,N;
char** table;
bool** checkTable;

struct Info{
	int cheese_id,row,col;
};

struct Data{
	int row,col,dist;
	char pre_move;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= height-1 && col >=0 && col <= width-1)return true;
	else{
		return false;
	}
}

int func(int row,int col,int goal_row,int goal_col,int dist){
	queue<Data> Q;
	Data data,tmp;
	data.row = row;
	data.col = col;
	data.dist = 0;
	data.pre_move = 'F';
	Q.push(data);

	checkTable[row][col] = true;

	int ret = 0;

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

		if(tmp.col == goal_col && tmp.row == goal_row){
			ret = tmp.dist;
			break;
		}

		if(rangeCheck(tmp.row-1,tmp.col) == true && tmp.pre_move != 'D' && table[tmp.row-1][tmp.col] != 'X' && checkTable[tmp.row-1][tmp.col] == false){
			Data new_data;
			new_data.row = tmp.row-1;
			new_data.col = tmp.col;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'U';
			checkTable[tmp.row-1][tmp.col] = true;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row+1,tmp.col) == true && tmp.pre_move != 'U' && table[tmp.row+1][tmp.col] != 'X'&& checkTable[tmp.row+1][tmp.col] == false){
			Data new_data;
			new_data.row = tmp.row+1;
			new_data.col = tmp.col;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'D';
			checkTable[tmp.row+1][tmp.col] = true;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row,tmp.col-1) == true && tmp.pre_move != 'R' && table[tmp.row][tmp.col-1] != 'X'&& checkTable[tmp.row][tmp.col-1] == false){
			Data new_data;
			new_data.row = tmp.row;
			new_data.col = tmp.col-1;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'L';
			checkTable[tmp.row][tmp.col-1] = true;
			Q.push(new_data);
		}
		if(rangeCheck(tmp.row,tmp.col+1) == true && tmp.pre_move != 'L' && table[tmp.row][tmp.col+1] != 'X'&& checkTable[tmp.row][tmp.col+1] == false){
			Data new_data;
			new_data.row = tmp.row;
			new_data.col = tmp.col+1;
			new_data.dist = tmp.dist+1;
			new_data.pre_move = 'R';
			checkTable[tmp.row][tmp.col+1] = true;
			Q.push(new_data);
		}
	}

	return ret;
}

int main(){

	scanf("%d %d %d",&height,&width,&N);
	table = new char*[1000];
	checkTable = new bool*[1000];
	Info info[N+1];
	int info_index = 0,total_dist = 0;


	for(int i=0; i < height; i++){
		table[i] = new char[1001];
		checkTable[i] = new bool[1000];

		scanf("%s",table[i]);

		for(int k = 0; k <width; k++){
			checkTable[k] = false;

			if(table[i][k] == 'S'){
				info[info_index].cheese_id = 0;
				info[info_index].row = i;
				info[info_index].col = k;
				info_index++;
			}else if(table[i][k] >= '1' && table[i][k] <= '9'){
				info[info_index].cheese_id = table[i][k] - '0';
				info[info_index].row = i;
				info[info_index].col = k;
				info_index++;
			}
		}
	}


	//???????????????????????±?????????????????????
	for(int i=1; i <= N; i++){
		for(int k=N; k >= i; k--){
			if(info[k].cheese_id < info[k-1].cheese_id)swap(info[k],info[k-1]);
		}
	}

	//i????????????????????????i+1???????????????????????§??????????????¢?????¨??¢?´¢??§?±???????
	for(int i = 0; i <= N-1; i++){
		total_dist += func(info[i].row,info[i].col,info[i+1].row,info[i+1].col,0);
		for(int k = 0; k < height; k++){
			for(int p = 0; p < width; p++)checkTable[k][p] = false;
		}
	}

	printf("%d\n",total_dist);

    return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstring>

using namespace std ;

int mx[ 4 ] = { 0 ,0 ,1 ,-1 } ;
int my[ 4 ] = { 1, -1 ,0 ,0 } ;
char map[ 1000 ][ 1000 ] ;
char c = '1' ;

struct M
{
	int x ;
	int y ;
	int cnt ;
} ;

M s ,v ;

int run( int h ,int w )
{
	int came[ 1000 ][ 1000 ] = { 0 } ;
	queue< M > que ;
	s.cnt = 0 ;
	que.push( s ) ;
	while( 1 )
	{
		s = que.front() ;
		que.pop() ;
		came[ s. y ][ s.x ] = s.cnt ;
		if( map[ s.y ][ s.x ] == c )
		{
			while(!que.empty())que.pop() ;
			c++ ;
			return s.cnt ;
		}
		for( int i = 0 ; i < 4 ; i++ )
		{
			if( s.x + mx[ i ] > -1 && s.x + mx[ i ] < w && s.y +  my[ i ] > -1 && s.y + my[ i ] < h )
			{
				v = s ;
				v.x += mx[ i ] ;
				v.y += my[ i ] ;
				v.cnt++ ;
				if( map[ v.y ][ v.x ] != 'X' && came[ v.y ][ v.x ] == 0 )
				{
					que.push( v ) ;
				}
			}
		}
	}
}

int main()
{
	s.x = -1 ;
	int h ,w ,n ;
	cin >> h >> w >> n ;
	for( int i = 0 ; i < h ; i++ )
	{
		cin >> map[ i ] ;
		for( int j = 0 ; j < w && s.x < 0 ; j++ )
		{
			if( map[ i ][ j ] == 'S' )
			{
				s.x = j ;
				s.y = i ;
				break ;
			}
		}
	}
	int ans = 0 ;
	while( n )
	{
		n-- ;
		ans += run( h ,w ) ;
	}
	cout << ans << endl ;
	return 0 ;
}

C++: #include<queue>
#include<cstdio>

#define	mp			make_pair
#define	rep(i,n)	for(int i=0;i<n;i++)

using namespace std;

typedef	pair<int,int>	pii;

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

int h,w;
char grid[1000][1000];

int shortestDistance(int x1,int y1,int x2,int y2){
	static bool visited[1000][1000];
	rep(i,h)rep(j,w)	visited[i][j]=false;
	visited[y1][x1]=true;

	queue< pair<int,pii> > qu;	qu.push(mp(0,mp(x1,y1)));
	while(1){
		pair<int,pii> a=qu.front();	qu.pop();
		int d=a.first,x=a.second.first,y=a.second.second;
		if(x==x2 && y==y2)	return d;

		rep(i,4){
			int xx=x+dx[i],yy=y+dy[i];
			if(0<=xx && xx<w && 0<=yy && yy<h && grid[yy][xx]!='X' && !visited[yy][xx]){
				visited[y][x]=true;
				qu.push(mp(d+1,mp(xx,yy)));
			}
		}
	}
}

int main(){
	int n;	scanf("%d%d%d ",&h,&w,&n);
	int cx[10],cy[10];
	rep(i,h)rep(j,w){
		scanf("%c ",grid[i]+j);
		if(grid[i][j]=='S')		cx[0]=j,cy[0]=i;
		if(isdigit(grid[i][j]))	cx[grid[i][j]-'0']=j,cy[grid[i][j]-'0']=i;
	}

	int cost=0;
	rep(i,n)	cost+=shortestDistance(cx[i],cy[i],cx[i+1],cy[i+1]);
	printf("%d\n",cost);

	return 0;
}