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 <algorithm>
#include <iostream>
#include <string.h>
#include <queue>

using namespace std;

#define int_ int64_t
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define lb long double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const ll N=1010;
ll n,m,x;
char grid[N][N];
bool vis[N][N];
ll u[10],v[10];
ll dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

struct node {
    ll x,y,val;
};

ll solve(ll s,ll t) {
    memset(vis,0,sizeof(vis));
    queue<node> q;
    q.push({u[s],v[s],0});
    while (!q.empty()) {
        node cur=q.front();
        q.pop();
        if (vis[cur.x][cur.y]) continue;
        vis[cur.x][cur.y]=true;
        if (cur.x==u[t]&&cur.y==v[t]) {
            return cur.val;
        }
        for (ll i=0;i<4;i++) {
            ll xx=cur.x+dir[i][0];
            ll yy=cur.y+dir[i][1];
            if (xx>0&&xx<=n&&yy>0&&yy<=m&&!vis[xx][yy]&&grid[xx][yy]!='X') {
                q.push({xx,yy,cur.val+1});
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%lld%lld%lld",&n,&m,&x);
    for (ll i=1;i<=n;i++) {
        scanf("%s",&grid[i][1]);
        for (ll j=1;j<=m;j++) {
            if (grid[i][j]=='S') {
                u[0]=i,v[0]=j;
            } else if (grid[i][j]!='.'&&grid[i][j]!='X') {
                ll idx=grid[i][j]-'0';
                u[idx]=i,v[idx]=j;
            }
        }
    }
    ll ans=0;
    for (ll i=1;i<=x;i++) {
        ans+=solve(i-1,i);
    }
    printf("%lld\n",ans);
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
#define MAX 1005

char JOI[MAX][MAX];
int h,w,n;
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};

pair<int, int> goal[10];
bool check(int a,int b){
    return (a>=1&&a<=h&&b>=1&&b<=w);
}

int step[MAX][MAX];
int bfs(const pair<int, int>& st, const pair<int, int>& en){
    queue<pair<int,int> > q;
    q.push(st);
    memset(step, -1, sizeof(step));
    step[st.first][st.second]=0;
    int stp=0;
    while(!q.empty()){
        pair<int, int> S=q.front(); q.pop();
        int x=S.first;  int y=S.second;
        stp=step[x][y];
        for(int i=0; i<4; i++){
            int cx=x+dx[i]; int cy=y+dy[i];
            if(check(cx, cy)){
                if(JOI[cx][cy]!='X'&&step[cx][cy]==-1){
                    if(cx==en.first&&cy==en.second) return stp+1;
                    q.push(make_pair(cx, cy));
                    step[cx][cy]=stp+1;
                }
            }
        }
    }
    return stp;
}
int main(){
    cin>>h>>w>>n;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            cin>>JOI[i][j];
            if(JOI[i][j]=='S') goal[0]=make_pair(i, j);
            else if(JOI[i][j]>='0'&&JOI[i][j]<='9'){
                int x=JOI[i][j]='0';
                goal[x]=make_pair(i, j);
            }
        }
    }
    int ans=0;
    for(int i=0; i<n; i++){
        ans+=bfs(goal[i],goal[i+1]);
    }
    cout<<ans<<endl;
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>
#include<queue>
int main(){
int h,w,n,i,j,k,m[1002][1002]={0},time=0;
pair<int,int> ht,s;
char c[2001];
queue<pair<int,int> > q,st;
scanf("%d%d%d",&h,&w,&n);
for(i=0;i<=h+1;i++){
	for(j=0;j<=w+1;j++){
		if(i==0||i==h+1||j==0||j==w+1){
		m[i][j]=10;
		}
	}
}
for(i=1;i<=h;i++){
	scanf("%s",&c);
	for(j=0;j<w;j++){
		if(c[j]=='S'){
			s.first=i;
			s.second=j+1;
			q.push(s);
		}
		else if(c[j]=='X'){
			m[i][j+1]=10;
		}
		else if(c[j]!='.'){
			m[i][j+1]=c[j];
		}
	}
}
ht.first=1;
ht.second=0;
st.push(ht);
while(!q.empty()){
	s=q.front();
	ht=st.front();
	q.pop();
	st.pop();
	ht.second++;
	if(m[s.first+1][s.second]!=10){
		s.first++;
		q.push(s);
		s.first--;
		st.push(ht);
	}
	if(m[s.first-1][s.second]!=10){
		s.first--;
		q.push(s);
		s.first++;
		st.push(ht);
	}
	if(m[s.first][s.second+1]!=10){
		s.second++;
		q.push(s);
		s.second--;
		st.push(ht);
	}
	if(m[s.first][s.second-1]!=10){
		s.second--;
		q.push(s);
		s.second++;
		st.push(ht);
	}
	ht.second--;
	if(ht.first==m[s.first][s.second]){
		ht.first++;
	}
	if(ht.first==n){
		if(time==0||time>ht.second){
			time=ht.second;
		}
	}
}
printf("%d\n",time);
return 0;
}

C++: #include <queue> 
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef struct{
	int x,y;
	int time;
	int hp;  //体力 
}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;
	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 <queue>
#include <cstdio>
#include <algorithm>

#define MAX_H 1000
#define MAX_DIS 1000000

using namespace std;

typedef pair<int,int> P;

int H,W,N;
char board[MAX_H][MAX_H];
int dis[MAX_H][MAX_H];

int si,sj,gv;

int vx[4] = {1,-1,0,0};
int vy[4] = {0,0,1,-1};

void solve(){
	queue<P> que;
	int sum=0,n=1,value=0;
	
	while(n<=N){
	
		que.push(P(si,sj));
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				dis[i][j] = MAX_DIS;
			}
		}
		dis[si][sj] = value;
		
		
		int qx,qy;
		while(1){
			P v = que.front(); que.pop();
			qx = v.first;
			qy = v.second;
			
			if(board[qx][qy] == '0'+(char)n) break;
			for(int i=0;i<4;i++){
				int pi,pj;
				pi = qx+vx[i];
				pj = qy+vy[i];
				if(-1<pi&&pi<H&&-1<pj&&pj<W&&board[pi][pj]!='X'&&dis[qx][qy]+1<dis[pi][pj]){
					dis[pi][pj] = dis[qx][qy]+1;
					que.push(P(pi,pj));
				}
			}
		}
		si = qx; sj = qy;
		value = dis[qx][qy];
		while(!que.empty()) que.pop();
		n++;
	}
	
	printf("%d\n",value);
}
	


int main(){
	scanf("%d %d %d",&H,&W,&N);
	
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			char c;
			scanf(" %c",&c);
			board[i][j] = c;
			if(c=='S'){
				si = i; sj = j;
			}
		}
	}
	
	solve();
	
	return 0;
}