13
因为没有给friend的数目,所以要从a找r。
虽说是优先队列题,但是不这么做也能A
非优先:
#include
#include
#define inf 40010
int min;
char map[210][210];
int z[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int find(int i,int j,int time)
{
if(map[i][j]=='X'||time>min) return 0;
if(map[i][j]=='r')
{
if(min>time) min=time;
return 0;
}
if(map[i][j]=='x')
for(int x=0;x<4;++x)
{
map[i][j]='#';
find(i+z[x][0],j+z[x][1],time+2);
map[i][j]='x';
}
if(map[i][j]=='.')
for(int x=0;x<4;++x)
{
map[i][j]='#';
find(i+z[x][0],j+z[x][1],time+1);
map[i][j]='.';
}
return 0;
}
int main()
{
int si,sj,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(map,'#',sizeof(map));
for(int i=2;i //一定要从a找r;
}
getchar();
}
// printf("%d\n",min);
min=inf;
find(si,sj,0);
if(min==inf) puts("Poor ANGEL has to stay in the prison all his life.");
else printf("%d\n",min);
}
return 0;
}
#include
#include
#include
#define inf 400010
using namespace std;
char map[210][210];
int mark[210][210];
int z[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node
{
int x,y,time;
} ;
struct cmp
{
bool operator()(const node &a,const node &b)
{
return a.time>b.time;
}
};
int find(node first)
{
node now,next;
priority_queue,cmp> q;
q.push(first);
while(!q.empty())
{
now=q.top();
q.pop();
for(int i=0;i<4;++i)
{
next.x=now.x+z[i][0];
next.y=now.y+z[i][1];
if(map[next.x][next.y]=='#'||mark[next.x][next.y]) continue;
mark[next.x][next.y]=1; //这里不知为何,用map[next.x][next.y]='#';标记会错
next.time=now.time+1;
if(map[next.x][next.y]=='r') return next.time;
if(map[next.x][next.y]=='x') ++next.time;
q.push(next);
}
}
return inf;
}
int main()
{
int si,sj,n,m,min;
node first;
// freopen("F://cs.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(map,'#',sizeof(map));
memset(mark,0,sizeof(mark));
for(int i=2;i