哈理工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;}
- 哈理工OJ 1942 this is bad problom(搜索BFS)
- 哈理工oj 1942this is bad problom 【基础bfs】
- Hrbust-1942-this is bad problom(搜索)
- 哈理工OJ 1490 咒语(BFS广度优先搜索)
- 哈理工 OJ Fire Maze(2次bfs)
- 哈理工oj 2035 Diablo【BFS+DFS】
- 哈理工oj 2035Diablo 【bfs+dfs】
- hrbust/哈理工oj 1617 回家【BFS+BFS】
- 哈理工OJ 1525 水神(海上葬礼)(【BFS(较困难)】)
- 哈理工 oj 1948 我又回来了(bfs 或着 最短路)
- 哈理工OJ 1286迷宫宝藏(这个bfs有点意思)
- 哈理工OJ 2034 Fire Maze【双BFS】【思维】
- 哈理工OJ 1367 King【基础入门BFS】
- hrbust 哈理工OJ 1498Elevator Trouble【BFS过】
- hrbust 哈理工oj 2188 星际旅行【BFS+传送阵】
- hrbust 哈理工oj 1989 营救小组【BFS】
- hrbust 哈理工oj 1613 迷宫问题 bfs
- 哈理工oj 2037油漆桶工具 【bfs】
- Git与github基本操作
- 网页游戏开发入门教程三(简单程序应用)
- linux下python安装Mysql扩展
- 软件测试笔记2-目的
- webservice axi2 远程服务端接口调用
- 哈理工OJ 1942 this is bad problom(搜索BFS)
- SharedPreferencesManager无法通过代码删除缓存
- maven tomcat 配置
- iOS 技术支持网址
- 经典散文集锦:读者杂志卷首语大荟萃
- CC1110下载程序接口电路图详解
- iOS 判断整数的位数,每位分别是什么。
- [Android-Camera开发]Android平台Camera实时预览数据处理即实时滤镜实现方法探讨(三)--通过Shader实现YUV转换RBG
- pinpoint Agent-启动加载插件分析