哈理工OJ 1942 this is bad problom(搜索BFS)

来源:互联网 发布:百度贴吧获取数据失败 编辑:程序博客网 时间:2024/05/18 01:56

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1942

this is bad problom
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 232(51 users) Total Accepted: 56(48 users) Rating: Special Judge: No
Description
周末了,小辉在大街上无聊地压马路,突然他想去找女童鞋一起快乐地玩耍,但是他有个毛病就是讨厌走路,他现在想知道到每个女童鞋那里的最少步数,再决定找哪个女童鞋。

首先给出小辉和女童鞋所在的N*N平面图,如图为6*6平面图。

….#.
.*.#..
……

#

……
……
有Q个女童鞋,每个女童鞋所在的地点用坐标表示,左上角处坐标为(0,0)。

图中’*’代表小辉所在的位置,即(1,1),’.’代表空地,’#’代表不能直接通过的建筑物。

小辉将去找他所能到达的并且离他步数最少的那个女童鞋。

女童鞋的位置可能在除建筑物的任意位置上。

Input
有多组测试数据,处理到文件结束。
对于每组测试数据,第一行是两个整数N(2<=N<=100),Q(1<=Q<=1000),分别代表地区的边长和女童鞋的个数.
接下来输入n*n平面图,代表地区平面图。
然后Q行,每行一个坐标代表女童鞋所在位置。

Output
输出小辉到那个女童鞋的步数,如果没有满足条件的女童鞋,则输出cry,最后换行.
Sample Input
6 3
….#.
.*.#..
……

#

……
……
0 0
1 4
5 3
3 2
*..

#


2 0
2 1
Sample Output
2
cry
Source
新生练习赛(2013.11.16)
Author
xuxu@hrbust

【思路分析】水搜索题目,直接把女生位置标记上,然后直接搜就行了。
【AC代码】

#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;struct node{    int x,y,step;};char a[105][105],book[105][105];int n,q;int dir[4][2]={{0,1},{0,-1},{1.0},{-1,0}};int bfs(int sx,int sy){    memset(book,0,sizeof(book));    queue<node>q;    node fr,ne;    fr.x=sx,fr.y=sy,fr.step=0;    book[sx][sy]==1;    q.push(fr);    while(!q.empty())    {        fr=q.front();        q.pop();        if(a[fr.x][fr.y]=='X')        {            return fr.step;        }        for(int i=0;i<4;i++)        {            ne.x=fr.x+dir[i][0];            ne.y=fr.y+dir[i][1];            ne.step=fr.step+1;            if(a[ne.x][ne.y]!='#'&&book[ne.x][ne.y]==0&&ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<n)            {                book[ne.x][ne.y]=1;                q.push(ne);            }        }    }    return -1;}int main(){    while(~scanf("%d%d",&n,&q))    {        int sx,sy;        for(int i=0;i<n;i++)        {            scanf("%s",&a[i]);            for(int j=0;j<n;j++)            {                if(a[i][j]=='*')                {                    sx=i;                    sy=j;                }            }        }        int u,v;        for(int i=0;i<q;i++)        {            scanf("%d%d",&u,&v);            a[u][v]='X';        }        int re=bfs(sx,sy);        if(re==-1)        {            printf("cry\n");        }        else        {            printf("%d\n",re);        }    }    return 0;}
0 0
原创粉丝点击