#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char map[30][30];
int step;
int havet;
int s1, s2, f1, f2;
int X, Y;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
struct node{
int place[2];
int count1;
};
bool operator <(const node &a, const node &b){
return a.count1 > b.count1;
}
priority_queue<node> q; //以上优先队列
void bfs(){
int i, j;
int a1, a2;
struct node temp1;
struct node temp2;
while(!q.empty()){
temp1 = q.top();
q.pop();
if(temp1.place[0] == f1 && temp1.place[1] == f2){
if(temp1.count1 < havet){
printf("%d\n", temp1.count1);
step = 1;
}
//return ;
}
for(i = 0; i < 4; i++){
a1 = temp1.place[0] + dir[i][0];
a2 = temp1.place[1] + dir[i][1];
if(a1 >= 0 && a1 < X && a2 >= 0 && a2 < Y && map[a1][a2] != 'o'){
temp2.place[0] = a1;
temp2.place[1] = a2;
if(map[a1][a2] == '.' || map[a1][a2] == 'm')
temp2.count1 = temp1.count1 + 1;
else if(map[a1][a2] == '#')
temp2.count1 = temp1.count1 + 2;
map[a1][a2] = 'o';
q.push(temp2);
}
}
}
}
int main()
{
step = -1;
int i, j;
char str[40] = "";
struct node temp;
scanf("%d%d%d", &havet, &Y, &X);
getchar();
for(i = 0; i < X; i++){
gets(str);
for(j = 0; str[j] != 0; j++){
map[i][j] = str[j];
if(map[i][j] == 's'){
s1 = i;
s2 = j;
// printf("s1 = %d s2 = %d\t", s1, s2);
}
if(map[i][j] == 'm'){
f1 = i;
f2 = j;
// printf("f1 = %d f2 = %d\n", f1, f2);
}
}
}
//printf("%d %d\n%d %d\n", s1, s2, f1, f2);
temp.place[0] = s1;
temp.place[1] = s2;
temp.count1 = 0;
map[s1][s2] = 'o';
q.push(temp);
bfs();
if(step == -1) printf("55555\n");
while(!q.empty()){
q.pop();
}
return 0;
}
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char map[30][30];
int step;
int havet;
int s1, s2, f1, f2;
int X, Y;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
struct node{
int place[2];
int count1;
};
bool operator <(const node &a, const node &b){
return a.count1 > b.count1;
}
priority_queue<node> q; //以上优先队列
void bfs(){
int i, j;
int a1, a2;
struct node temp1;
struct node temp2;
while(!q.empty()){
temp1 = q.top();
q.pop();
if(temp1.place[0] == f1 && temp1.place[1] == f2){
if(temp1.count1 < havet){
printf("%d\n", temp1.count1);
step = 1;
}
//return ;
}
for(i = 0; i < 4; i++){
a1 = temp1.place[0] + dir[i][0];
a2 = temp1.place[1] + dir[i][1];
if(a1 >= 0 && a1 < X && a2 >= 0 && a2 < Y && map[a1][a2] != 'o'){
temp2.place[0] = a1;
temp2.place[1] = a2;
if(map[a1][a2] == '.' || map[a1][a2] == 'm')
temp2.count1 = temp1.count1 + 1;
else if(map[a1][a2] == '#')
temp2.count1 = temp1.count1 + 2;
map[a1][a2] = 'o';
q.push(temp2);
}
}
}
}
int main()
{
step = -1;
int i, j;
char str[40] = "";
struct node temp;
scanf("%d%d%d", &havet, &Y, &X);
getchar();
for(i = 0; i < X; i++){
gets(str);
for(j = 0; str[j] != 0; j++){
map[i][j] = str[j];
if(map[i][j] == 's'){
s1 = i;
s2 = j;
// printf("s1 = %d s2 = %d\t", s1, s2);
}
if(map[i][j] == 'm'){
f1 = i;
f2 = j;
// printf("f1 = %d f2 = %d\n", f1, f2);
}
}
}
//printf("%d %d\n%d %d\n", s1, s2, f1, f2);
temp.place[0] = s1;
temp.place[1] = s2;
temp.count1 = 0;
map[s1][s2] = 'o';
q.push(temp);
bfs();
if(step == -1) printf("55555\n");
while(!q.empty()){
q.pop();
}
return 0;
}