题意:起点(y),目的地(t),河流(r);刚墙(s),空地(e),射破刚强(b)需要一单位的t
代码:
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
char map[301][301];
bool visit[301][301];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int m,n,sx,sy;
struct node{
#include<queue>
#include<cstdio>
using namespace std;
char map[301][301];
bool visit[301][301];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int m,n,sx,sy;
struct node{
int x,y;
int time;
friend bool operator<(const node&a,const node&b)// 此处采用友元函数来重载运算符"<",使得根据元素time的大小让元素从小到大出队
{
return a.time>b.time;
}
};
int bfs()
{
int i;
node you;
node start;
node next;
priority_queue<node>q;
you.x=sx;
you.y=sy;
you.time=0;
q.push(you);
visit[sx][sy]=1;
while(!q.empty())
{
start=q.top();
q.pop();
for(i=0;i<4;i++)
{
next.x=start.x+dir[i][0];
next.y=start.y=dir[i][1];
if(next.x<0||next.y<0||next.x>=m||next.y>=n||map[next.x][next.y]=='r'||map[next.x][next.y]=='s'||visit[next.x][next.y]==1)//边界检查
continue;
if(map[next.x][next.y]=='b')
next.time=start.time+2;
else
next.time=start.time+1;
if(map[next.x][next.y]=='t')
return next.time;
visit[next.x][next.y]=1;
q.push(next);
}
}
return 0;
int time;
friend bool operator<(const node&a,const node&b)// 此处采用友元函数来重载运算符"<",使得根据元素time的大小让元素从小到大出队
{
return a.time>b.time;
}
};
int bfs()
{
int i;
node you;
node start;
node next;
priority_queue<node>q;
you.x=sx;
you.y=sy;
you.time=0;
q.push(you);
visit[sx][sy]=1;
while(!q.empty())
{
start=q.top();
q.pop();
for(i=0;i<4;i++)
{
next.x=start.x+dir[i][0];
next.y=start.y=dir[i][1];
if(next.x<0||next.y<0||next.x>=m||next.y>=n||map[next.x][next.y]=='r'||map[next.x][next.y]=='s'||visit[next.x][next.y]==1)//边界检查
continue;
if(map[next.x][next.y]=='b')
next.time=start.time+2;
else
next.time=start.time+1;
if(map[next.x][next.y]=='t')
return next.time;
visit[next.x][next.y]=1;
q.push(next);
}
}
return 0;
}
int main()
{
int i,j;
while(cin>>m>>n)
{
if(m==0||n==0) break;
memset(visit,0,sizeof(visit));
for(i=0;i<m;i++)
{
getchar();
for(j=0;j<n;j++)
{
cin>>map[i][j];
if(map[i][j]=='y')
{
sx=i;
sy=j;
}
}
}
int main()
{
int i,j;
while(cin>>m>>n)
{
if(m==0||n==0) break;
memset(visit,0,sizeof(visit));
for(i=0;i<m;i++)
{
getchar();
for(j=0;j<n;j++)
{
cin>>map[i][j];
if(map[i][j]=='y')
{
sx=i;
sy=j;
}
}
}
cout<<bfs()<<endl;
}
return 0;
}
return 0;
}