# 地图

Time Limit(Common/Java):1000MS/3000MS          Memory Limit:65536KByte

Description

Input

Output

Sample Input

3 3CCPCDCSCC3 3CDPCCCSGG 5 5CCMCPCCC#MCCCCCCDCG#CSCCC

Sample Output

SorrySorry9

Source

caisz

#include<iostream>
#include<queue>
using namespace std;

int n,m;
char map[101][101];
int f[4][2]={1,0,0,1,-1,0,0,-1};

struct Node
{
int x,y,step;
bool operator<(const Node &a) const
{
return step>a.step;
}
};

int bfs(int pi,int pj)
{
priority_queue<Node> q;
Node now,next;
now.x=pi;
now.y=pj;
now.step=0;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+f[i][0];
next.y=now.y+f[i][1];
if(next.x<0||next.y<0||next.x>=n||next.y>=m||map[next.x][next.y]=='#') continue;
if(map[next.x][next.y]=='S')
{
return now.step;
}
if(map[next.x-1][next.y]=='D'||map[next.x+1][next.y]=='D'||map[next.x][next.y-1]=='D'||map[next.x][next.y+1]=='D') continue;
if(map[next.x][next.y]=='C')
{
next.step=now.step+1;
q.push(next);
map[next.x][next.y]='#';
}
else if(map[next.x][next.y]=='G')
{
next.step=now.step+2;
q.push(next);
map[next.x][next.y]='#';
}
else
{
next.step=now.step+3;
q.push(next);
map[next.x][next.y]='#';
}
}
}
return -1;
}

int main()
{
int pi,pj;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
for(int j=0;j<m;j++)
{
if(map[i][j]=='P')
{
pi=i;
pj=j;
}
}
}
if(map[pi-1][pj]=='D'||map[pi+1][pj]=='D'||map[pi][pj-1]=='D'||map[pi][pj+1]=='D')
{
printf("Sorry\n");
continue;
}
int Min=bfs(pi,pj);
if(Min==-1)
{
printf("Sorry\n");
}
else
{
printf("%d\n",Min);
}
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：地图 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)