多校1(1009)(杭电4308)

题目:

Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 705    Accepted Submission(s): 257


Problem Description
Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy.
Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess.
The maze can be described as a matrix of r rows and c columns, with grids, such as 'Y', 'C', '*', '#' and 'P', in it. Every grid is connected with its up, down, left and right grids.
There is only one 'Y' which means the initial position when Prince ykwd breaks into the maze.
There is only one 'C' which means the position where Princess claire_ is jailed.
There may be many '*'s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station.
The grid represented by '#' means that you can not pass it. 
It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior.
'P' means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another. 

They say that there used to be some toll stations, but they exploded(surely they didn't exist any more) because of GDM teoy's savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes.
Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn't want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he needs to take his princess back.
 

 

Input
Multiple cases.(No more than fifty.)
The 1st line contains 3 integers, r, c and cost. 'r', 'c' and 'cost' is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )
Then an r * c character matrix with 'P' no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.
 

 

Output
One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)
 

 

Sample Input
1 3 3
Y*C
1 3 2
Y#C
1 5 2
YP#PC
 

 

Sample Output
3
Damn teoy!
0
 
开始想错了,不过很快就看出可以直接广搜(当然是在王文超童鞋一起讨论后才知道),不过因为不会写广搜。
1h写代码,2days's debug。
让大仙帮我看,大仙不干,我我只得自己debug,经过一步一步打印,最终确定输入格式有误,main里面初始化忘了写,就这么两个bug浪费了2days,以后要更加注意,敲代码前要想好运行的步骤。
还有一点,在所有bug改完之后,在杭电上交了好几遍都WA,后来把提交语言改为C++,就AC了。
代码:
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
struct M
{
    int x,y;
}Q[5005],p[505];
int head,teal;
void push( int a,int b )
{
    Q[teal].x=a;
    Q[teal].y=b;
    teal=(teal++)%5005;
}
M pop()
{
    M temp;
    temp=Q[head];
    head=(head++)%5005;
    return temp;
}
char map[5005];     //存放输入的字符矩阵
bool vis[5005]={0}; //vis[i*c+j]记录矩阵中[i,j]是否走过
int v[5005]={0};    //V[i*c+j]表示从起点到i,j的步数
int np;             //记录'P'的个数
const int xx[4]={ 1,-1,0,0 };
const int yy[4]={ 0,0,1,-1 };
int sx,sy,ex,ey;    //起点终点
int r,c,cost;       //行数列数,每一步的花费数
int bfs(  )
{
    push( sx,sy );//首先起点入队
    vis[sx*c+sy]=1;
    v[sx*c+sy]=0;
    M t;
    int i,j,x,y;
    while( head!=teal )
    {
        t=pop();
        if( t.x==ex&&t.y==ey )//若队头是终点,直接结束循环
            break;
        for( i=0;i<4;i++ )
        {
            x=t.x+xx[i];
            y=t.y+yy[i];
            if( vis[ x*c+y ]==0 && x<r && y<c && x>=0 && y>=0 )  //没走过且不超边界
            {
                if( map[x*c+y]=='P' )//如果遇到P,所有P全部入队
                {
                    for( j=0;j<=np;j++ )
                    {
                        vis[ p[j].x*c+p[j].y ]=1;
                        if( map[ t.x*c+t.y ]=='*' )//若是从*来,则要耗费一个cost,下同
                            v[p[j].x*c+p[j].y]=v[ t.x*c+t.y ]+1;
                        else
                            v[p[j].x*c+p[j].y]=v[t.x*c+t.y];
                        push( p[j].x,p[j].y );
                    }
                }
                else//若不是P,直接入队
                {
                    vis[x*c+y]=1;
                    if( map[ t.x*c+t.y ]=='*' )
                        v[x*c+y]=v[ t.x*c+t.y ]+1;
                    else
                        v[x*c+y]=v[t.x*c+t.y];
                    push(x,y);
                }
            }
        }
    }
    if( vis[ex*c+ey]==0 )//目标位置没有走到
        return -1;
    else
        return v[ex*c+ey];
}
int main()
{
    int i,j;
    while( scanf("%d%d%d",&r,&c,&cost)!=EOF )
    {
        memset(vis,0,sizeof(vis));
        memset(v,0,sizeof(v));
 //       getchar();
        head=0;
        teal=0;
        np=-1;
        for( i=0;i<r;i++ )
        {
            getchar();
            for( j=0;j<c;j++ )
            {
                scanf("%c",&map[i*c+j]);
                if( map[i*c+j]=='Y' )
                {
                    sx=i;sy=j;
                }
                if( map[i*c+j]=='C' )
                {
                    ex=i;ey=j;
                }
                if( map[i*c+j]=='#' )
                    vis[i*c+j]=1;
                if( map[i*c+j]=='P' )//记录P的位置
                {
                    np++;
                    p[np].x=i;
                    p[np].y=j;
                }
            }
        }
        int ans;
        ans=bfs();
        if( ans==-1 )
        {
            printf("Damn teoy!\n");
        }
        else
            printf("%d\n",ans*cost);
    }
    return 0;
}

 

posted @ 2012-07-21 16:47  萧若离  阅读(192)  评论(0)    收藏  举报