#include using namespace std; #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) for (int i=0;i<(n);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) int dp[3002][3002]; #define INF 10000 #define MAX_V 10000000 struct edge{int to ,cost;}; typedef pair P; //firstは最短距離,secondは頂点の番号 int V; int d[MAX_V]; priority_queue,greater

> que; int H,W; void dijkstra(int s){ //greater

を指定することでfirstが小さい順に取り出せる fill(d,d+V,INF); //d[s]=0; //queue.push(P(0,s)); while(!que.empty()){ P p=que.top();que.pop(); int v=p.second; if(d[v] < p.first)continue; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ if(i==0&&j==0)continue; int a=0; a=v+(i*1)+j*(W+2); if(a<0||(a>=(H+2)*(W+2)))continue; if((v%(W+2)==0&&i==-1)||(v%(W+2)==W+1&&i==1))continue; /* switch(i){ case 0: if(v-1<0)continue; a=v-1; break; case 1: if(v-W-2<0)continue; a=v-W-2; break; case 2: if(v%(W+2)==W+1)continue; a=v+1; break; case 3: if(v+W+2>(H+2)*(W+2))continue; a=v+H+2; break; case 4: if(v) } */ if(d[a]>d[v]+1){ d[a]=d[v]+1; que.push(P(d[a],a)); } } } /* cout<>H>>W; REP(i,H+2){ REP(j,W+2){ if(i==0||i==H+1||j==0||j==W+1){ d[i*(W+2)+j]=0; que.push(P(0,i*(W+2)+j)); }else{ d[i*(W+2)+j]=INF; } } } REP(i,H){ string S; cin>>S; REP(j,W){ if(S[j]=='.'){ d[(i+1)*(W+2)+(j+1)]=0; que.push(P(0,(i+1)*(W+2)+j+1)); }else{ d[(i+1)*(W+2)+j+1]=INF; } } } dijkstra(0); int m=-1; /* REP(i,H+2){ REP(j,W+2){ cout<