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

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W;
char N;
const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
int d[MAX_H][MAX_W];
int mytime;

void bfs() {
    queue<Pair> que;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INF;
        }
    }
    char muscle = '1';
    que.push(Pair(sx,sy));
    d[sx][sy] = 0;
    while (que.size()) {
        Pair p  = que.front(); que.pop();
        if (muscle-1 == N) break;
        for (int i = 0; i < 4; i++) {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if (0 <= nx && nx < H && 0 <= ny && ny < W && plant[nx][ny] != 'X' && d[nx][ny] == INF) {
                que.push(Pair(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
            if (muscle == plant[nx][ny]) {
                while(que.size()) {que.pop();}
                que.push(Pair(nx, ny));
                muscle = muscle + 1;
                mytime += d[nx][ny];
                for (int i = 0; i < H; i++) {
                    for (int j = 0; j < W; j++) {
                        d[i][j] = INF;
                    }
                }
                d[nx][ny] = 0;
                break;
            }
        }
    }
}

void solve() {
    mytime = 0;
    bfs();
    cout << mytime << endl;
}

int main () {
    cin >> H >> W >> N;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> plant[i][j];
            if(plant[i][j] == 'S') {sx = i; sy = j;}
        }
    }
    solve();
}

C++: #include <queue> 
#include <cstdio>
#include <cstring>
using namespace std;
typedef struct{
	int x,y;
	int time;
	int hp = 1;  //体力 
}Mouse;
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};
int main()
{
	int sx,sy;
	bool flag = true; 
	scanf("%d%d%d",&height,&width,&n);
	getchar();//清除回&#36710; 
	//&#36755;入迷&#23467; 
	for(int i = 0;i<height;i++)
	{
		fgets(maze[i],maxn,stdin);
		//&#26597;找老鼠&#24034;的位置	
		if(flag&&strchr(maze[i],'S'))
		{
			int len = strlen(maze[i]);
			for(int j = 0;j<len;j++)
				if(maze[i][j]=='S')
				{
					sy = i;
					sx = j;
					flag = false;
					break;
				}
		}
	}
	queue<Mouse> q;
	Mouse m;
	m.x = sx;m.y = sy;m.time = 0;m.hp = 1;
	q.push(m);
	while(q.size())
	{
		Mouse t = q.front();q.pop();
		if(t.hp-1==n)
		{
			printf("%d\n",t.time);
			return 0;
		}
		for(int i = 0;i<4;i++)
		{
			int nx = t.x + dx[i];
			int ny = t.y + dy[i];
			if(nx>=0&&nx<width&&ny>=0&&ny<height&&strchr("123456789.",maze[ny][nx]))
			{
				Mouse a;
				a.x = nx;a.y = ny;a.time = t.time+1;
				int y = maze[ny][nx]-'0';
				if(y>=1&&y<=10&&t.hp>=y)
				{
			 		a.hp = t.hp+1;
			 		maze[ny][nx] = '.';
			 	}
			 	else
			 		a.hp = t.hp;
				q.push(a);
			}
		}
	}
	printf("%d \t %d\n",sx,sy);
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,t,tot,sx[10],sy[10];
struct p{
	int x,y,step;
}cur,nxt,q[15000];
char map[1001][1001];
bool v[1001][1001];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int  bfs(int a,int b,int aa,int bb)
{
	int head=0,tail=1;
	memset(v,0,sizeof(v));
	q[head].x=a;q[head].y=b;q[head].step=0;v[a][b]=1;
	while(head<tail)
	{
		cur=q[head];
		for(int i=0;i<=3;i++)
		{
			if(nxt.x==aa&&nxt.y==bb) return nxt.step;
			nxt.x=cur.x+dx[i];
			nxt.y=cur.y+dy[i];
			nxt.step=cur.step+1;
			if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&map[nxt.x][nxt.y]!='X'&&v[nxt.x][nxt.y]==0)
			{
				v[nxt.x][nxt.y]=1;	
				q[tail]=nxt;tail++;
			}
		}
		head++;
	}
}
int main()
{
	cin>>n>>m>>t;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	{
		cin>>map[i][j];
		if(map[i][j]!='.'&&map[i][j]!='X')
		{
		if(map[i][j]=='S'){sx[0]=i;sy[0]=j;}
		else{
			sx[map[i][j]-'0']=i;sy[map[i][j]-'0']=j;
			}
		}
	}
	for(int i=0;i<t;i++)
	{int k=bfs(sx[i],sy[i],sx[i+1],sy[i+1]);tot+=k;}
	cout<<tot<<endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <algorithm>
using namespace std;

typedef long long ll;

int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
char field[1002][1002];

struct State {int x, y, step;};

int main()
{
    int h, w, n, sx, sy;
    cin >> h >> w >> n;
    for(int i = 0; i < 1002; i++) {
        fill(field[i], field[i] + 1002, 'X');
    }

    for(int i = 1; i <= h; i++) {
        for(int j = 1; j <= w; j++) {
            cin >> field[i][j];
            if(field[i][j] == 'S') {
                sx = j; sy = i;
                field[i][j] = '.';
            }
        }
    }
    
    int ans = 0;
    for(int k = 0; k < n; k++) {
        char goal = '0' + k + 1;
        queue<State> que;
        que.push((State){sx, sy, 0});
        bool visited[1002][1002];
        for(int j = 0; j < 1002; j++) {
            fill(visited[j], visited[j] + 1002, false);
        }

        while(!que.empty()) {
            State cur = que.front(); que.pop();
            //cout << cur.x << " " << cur.y << " " << cur.step << " " << field[cur.y][cur.x] << endl;
            if(visited[cur.y][cur.x]) {
                continue;
            }
            visited[cur.y][cur.x] = true;

            if(field[cur.y][cur.x] == goal) {
                ans += cur.step;
                sx = cur.x; sy = cur.y;
                break;
            }

            for(int i = 0; i < 4; i++) {
                int nx, ny;
                nx = cur.x + dx[i]; ny = cur.y + dy[i];
                if(field[ny][nx] == 'X' || visited[ny][nx]) {
                    continue;
                }
                que.push((State){nx, ny, cur.step + 1});
            }
        }
    }
    cout << ans << endl;
}