类似"罗密欧与朱丽叶"那题,宽搜搞定。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5858 | Accepted: 1934 |
Description
Input
Output
Sample Input
3 4 YBEB EERE SSTE 0 0
Sample Output
8
#include
#include
#include
using namespace std;
const int S=310;
int h[S][S];
char g[S][S];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct point{
int x,y,step;
friend bool operator < (const point &a, const point &b){
return (a.step q;
q.push(begin);
while (!q.empty()){
now=q.top();
// printf("(%d , %d) step=%d\n",now.x,now.y,now.step);
q.pop();
for (i=0;i<4;i++){
x=now.x+dx[i];
y=now.y+dy[i];
if (x<0||y<0||x>=m||y>=n) continue;
if (g[x][y]=='S'||g[x][y]=='R') continue;
if (g[x][y]=='B') {
tmp.x=x;tmp.y=y;tmp.step=now.step+2;
if (h[x][y]>tmp.step||h[x][y]==-1){
q.push(tmp);
h[x][y]=tmp.step;
}
}
if (g[x][y]=='E'||g[x][y]=='T') {
tmp.x=x;tmp.y=y;tmp.step=now.step+1;
if (h[x][y]>tmp.step||h[x][y]==-1){
q.push(tmp);
h[x][y]=tmp.step;
}
}
}
}
return 0;
}
int main(){
point begin,end;
int m,n,i,j;
while (scanf("%d%d",&m,&n),!(m==0&&n==0)){
for (i=0;i