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 <queue>
using namespace std;

char map[1002][1002];

struct solve {
	int x, y, depth;
};

int startx, starty;

int main() {
	for (int i = 0; i < 1002; i++)for (int j = 0; j < 1002; j++) map[i][j] = 'X';

	int h, w, cheese;
	cin >> h >> w >> cheese;

	for (int i = 0; i < h; i++)for (int j = 0; j < w; j++) {
		char temp;
		cin >> temp;
		if (temp == 'S') {
			starty = i + 1;
			startx = j + 1;
		}
		map[i + 1][j + 1] = temp;
	}

	int ans = 0;

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

		bool used[1002][1002] = { 0 };
		used[starty][startx] = true;
		solve point = { startx,starty, 0 };
		queue<solve> q;
		q.push(point);
		while (!q.empty()) {
			solve u = q.front(); q.pop();

			if (map[u.y][u.x] == i + '0') {

				startx = u.x;
				starty = u.y;
				ans += u.depth;
				break;
			}

			if ((map[u.y + 1][u.x] != 'X') && (used[u.y + 1][u.x] == false)) {
				solve temp = { u.x , u.y + 1 , u.depth + 1 };
				used[u.y + 1][u.x] = true;
				q.push(temp);
			}
			if ((map[u.y - 1][u.x] != 'X') && (used[u.y - 1][u.x] == false)) {
				solve temp = { u.x , u.y - 1 , u.depth + 1 };
				used[u.y - 1][u.x] = true;
				q.push(temp);
			}
			if ((map[u.y][u.x + 1] != 'X') && (used[u.y][u.x + 1] == false)) {
				solve temp = { u.x + 1 , u.y , u.depth + 1 };
				used[u.y][u.x + 1] = true;
				q.push(temp);
			}

			if ((map[u.y][u.x - 1] != 'X') && (used[u.y][u.x - 1] == false)){
				solve temp = { u.x - 1 , u.y , u.depth + 1 };
				used[u.y][u.x - 1] = true;
				q.push(temp);
			}
		}
	}

	cout << ans << endl;

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

int h, w, n;
const int MAX_N = 1000;
char map[MAX_N][MAX_N];
struct point{
	int x, y;
	int step;
	point(){
		x = 0; y = 0; step = 0;
	}
	point(int xx, int yy, int ss){
		x = xx;
		y = yy;
		step = ss;
	}
};
struct point p[10];
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };

int ok(int x, int y){
	if (x >= 0 && x<h && y >= 0 && y<w) return 1;
	return 0;
}

int bfs(int next){
	int nextcheese = next + 1; int res = 0;

	queue<point> q;
	point cur(p[next].x, p[next].y, 0);
	q.push(cur);
	while (q.size()){
		cur = q.front(); q.pop();
		int x = cur.x, y = cur.y;
		int nx, ny;
		for (int i = 0; i<4; i++){
			nx = x + dx[i]; ny = y + dy[i];
			if (ok(nx, ny)){
				if ((map[nx][ny] - '0') == nextcheese) return ++cur.step;
				if (map[nx][ny] == '.'){
					point temp(nx, ny, cur.step + 1);
					q.push(temp);
				}
			}
		}
	}
}

void solve(){
	int sx, sy;
	scanf("%d%d%d", &h, &w, &n);
	for (int i = 0; i<h; i++){
		for (int j = 0; j<w; j++){
			char ch;
			cin >> ch; map[i][j] = ch;
			switch (ch){
			case '.': break;
			case 'X': break;
			case 'S': p[0].x = i; p[0].y = j; break;
			default:
				int t = ch - '0';
				p[t].x = i; p[t].y = j;
			}
		}
	}
	int step = 0;
	for (int i = 0; i<n; i++){
		step += bfs(i);
	}
	printf("%d", step);
}


int main(){
	solve();
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define MAXN 1000
#define PR pair<int,int>
#define X first
#define Y second
int w,h,n,ans;
char M[MAXN+5][MAXN+5];
int dp[MAXN+5][MAXN+5];
PR ST[10];
queue< PR > Q;
int Dx[4]={1,0,-1,0},Dy[4]={0,1,0,-1};
int BFS(int S)
{
    memset(dp,0x3f,sizeof(dp));
    while(!Q.empty())Q.pop();
    PR H=ST[S-1];
    dp[H.X][H.Y]=0;
    Q.push(H);
    while(!Q.empty())
    {
        PR Ls=Q.front();
        Q.pop();
        if(Ls.X==ST[S].X&&Ls.Y==ST[S].Y)return dp[Ls.X][Ls.Y];
        for(int i=0;i<=3;i++)
        {
            PR Nr;
            Nr=make_pair(Ls.X+Dx[i],Ls.Y+Dy[i]);
            if(M[Nr.X][Nr.Y]=='X'||Nr.X==0||Nr.Y==0||Nr.X==w+1||Nr.Y==h+1)continue;
            if(dp[Ls.X][Ls.Y]+1<dp[Nr.X][Nr.Y])
            {
                dp[Nr.X][Nr.Y]=dp[Ls.X][Ls.Y]+1;
                Q.push(Nr);
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&w,&h,&n);
    for(int i=1;i<=w;i++)
       scanf("%s",M[i]+1);
    for(int i=1;i<=w;i++)
        for(int j=1;j<=h;j++)
        if(M[i][j]=='S')ST[0]=make_pair(i,j);
        else if(M[i][j]>='1'&&M[i][j]<='9')ST[M[i][j]-'0']=make_pair(i,j);
    for(int i=1;i<=n;i++)ans+=BFS(i);
    printf("%d",ans);
}

C++: #include<cstdio>
#include<queue>
/*
#include<windows.h>
*/
using namespace std;

struct DATA{
	int y;
	int x;
	int n;
};


bool  vi[1001][1001];
char cp[1001][1001];  
DATA Start,Goal;
int H,W,N;
/*
void pa(){
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(vi[i][j])printf("¡");
			else printf(" ");
		}puts("");
	}puts("");
}
*/
int BFS(){
	queue<DATA> Q;
	Q.push(Start);
	
	while(!Q.empty()){
//		pa();
//		system("cls");
		DATA p = Q.front();
		Q.pop();
//		printf("%d (%4d,%4d) %d\n", Q.size(), p.x, p.y, p.n);
		const int dy[] = {1,0,-1,0};
		const int dx[] = {0,1,0,-1};
		
		if(vi[p.y][p.x])continue;
		vi[p.y][p.x] = true;
		if(p.y == Goal.y && p.x == Goal.x)return p.n;
		
		for(int i = 0; i < 4; i++){
			if(p.x+dx[i] < 0 || p.x+dx[i] > W || p.y+dy[i] < 0 || p.y+dy[i] > H || cp[p.y][p.x] == 'X' && !vi[p.y+dy[i]][p.x+dx[i]])continue;
			DATA NEXT = {p.y+dy[i],p.x+dx[i],p.n+1};
			Q.push( NEXT );
		}
	}
	return -1;
}

int main(void){
	
	while(scanf("%d %d %d", &H, &W, &N) != EOF ){
		
		char next = '1';
		int ans = 0;
		for(int i = 0; i < H; i++)scanf("%s",cp[i]);
		
		while(next-'1' != N){
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					vi[i][j] = false;
				}
			}
			
			if(next == '1'){
				for(int i = 0; i < H; i++){
					for(int j= 0; j < W; j++){
						if(cp[i][j] == 'S'){
							Start.x = j;
							Start.y = i;
							Start.n = 0;
							goto A;
						}
					}
				}
			}else {
				for(int i = 0; i < H; i++){
					for(int j = 0; j < W; j++){
						if(cp[i][j] == next-1){
							Start.x = j;
							Start.y = i;
							Start.n = 0;
							goto A;
						}
					}
				}
			}
			A:
			
			for(int i = 0; i < H; i++){
				for(int j = 0; j < W; j++){
					if(cp[i][j] == next){
						Goal.x = j;
						Goal.y = i;
						goto B;
					}
				}
			}
			B:
//			printf("S = (%d %d)\n", Start.x, Start.y);
//			printf("G = (%d %d)\n", Goal.x, Goal.y);
			ans += BFS();
			next++;
		}
		printf("%d\n",ans);
	}
	return 0;
}