AOJ 0558 - Cheese

チーズの数をcnとすると,
(S->1までの最短路) + (1->2までの最短路) + ... + (cn-1->cn)までの最短路を求めればいい.
それはbfsでできる.

#include<iostream>
#include<queue>

struct P{
	int x, y;
};

const int MAX_H = 1000, MAX_W = 1000, MAX_CN = 10, INF = 1<<24;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int h, w, cn;
int map[MAX_H][MAX_W], d[MAX_H][MAX_W];
P ps[MAX_CN];
//ps x y の順
//map 通れる1 通れない0
//used 通ってない0 通った1
//d d[y][x] spから(x,y)までの最短距離

int main(){
	std::cin >> h >> w >> cn;
	for(int i=0;i<h;i++){
		std::fill(map[i], map[i]+w, 0);
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			char c;
			std::cin >> c;
			if(c == 'S'){
				ps[0].x = j;
				ps[0].y = i;
				map[i][j] = 1;
			}else if('1' <= c && c <= '9'){
				ps[c-'0'].x = j;
				ps[c-'0'].y = i;
				map[i][j] = 1;
			}else if(c == '.'){
				map[i][j] = 1;
			}
		}
	}

	int res = 0;
	for(int i=0;i<cn;i++){//ps[i] -> ps[i+1]への最短路を調べる.それを足せばok
		P sp = ps[i], gp = ps[i+1];
		for(int i=0;i<h;i++){
			std::fill(d[i], d[i]+w, INF);
		}
		d[sp.y][sp.x] = 0;
		std::queue<P> pq;
		pq.push(sp);

		while(!pq.empty()){
			P p = pq.front();pq.pop();
			if(p.x == gp.x && p.y == gp.y){
				break;
			}
			for(int i=0;i<4;i++){
				int nx = p.x+dx[i], ny = p.y+dy[i];
				if(nx >= 0 && nx < w && ny >= 0 && ny < h && map[ny][nx] && d[ny][nx] == INF){
					pq.push({nx, ny});
					d[ny][nx] = d[p.y][p.x] + 1;
				}
			}
		}

		res += d[gp.y][gp.x];
	}
	
	std::cout << res << std::endl;
}