Saving Tang Monk II
题目链接:http://hihocoder.com/contest/acmicpc2018beijingonline/problem/1
AC代码:
#include<bits/stdc++.h>
using namespace std;
# define inf 0x3f3f3f3f
# define maxn 100+10
# define ll long long
int n,m;
char a[maxn][maxn];
int f[2][4]= {{1,-1,0,0},{0,0,1,-1}};
int vis[maxn][maxn][10];
struct node
{
int x,y,b,step;
node() {}
node(int xx,int yy,int tt,int ww)
{
x=xx;
y=yy;
b=tt;
step=ww;
}
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
void bfs(int t1,int t2)
{
priority_queue<node>q;
memset(vis,0,sizeof(vis));
q.push(node(t1,t2,0,0));
vis[t1][t2][0]=1;
while(!q.empty())
{
node temp=q.top();
q.pop();
// cout<<temp.x<<" "<<temp.y<<" "<<temp.b<<" "<<temp.step<<endl;
for(int i=0; i<4; i++)
{
node temp2;
int x=temp.x+f[0][i];
int y=temp.y+f[1][i];
if(x<=0||x>n||y<=0||y>m)continue;
temp2.x=x;
temp2.y=y;
temp2.b=temp.b;
temp2.step=temp.step+1;
if(a[x][y]=='B')
{
temp2.b=min(temp2.b+1,5);
}
else if(a[x][y]=='P')
{
temp2.step=max(temp2.step-1,0);
}
else if(a[x][y]=='#')
{
if(temp.b==0)continue;
temp2.b=max(temp2.b-1,0);
temp2.step++;
}
else if(a[x][y]=='T')
{
printf("%d\n",temp2.step);
return ;
}
if(vis[x][y][temp2.b])continue;
vis[x][y][temp2.b]=1;
q.push(temp2);
}
}
printf("%d\n",-1);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n+m==0)break;
int t1,t2;
getchar();
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='S')
{
t1=i;
t2=j;
}
}
getchar();
}
bfs(t1,t2);
}
return 0;
}