10.3 弱校 B Help the Princess! 搜索
来源:互联网 发布:char占几个字节 java 编辑:程序博客网 时间:2024/06/02 02:32
题目描述:
https://acm.bnu.edu.cn/v3/contest_show.php?cid=8504#problem/B
题目分析:
由于题目是pdf格式给出的 我就扔连接算了~~
题目大意就是一个n*m的地图,其中有一个’@’表示公主,有若干个’$’表示士兵,一个’%’表示出口,’#’表示墙(无法进入),’.’表示可以走的地方。其中公主想要逃亡到出口,有士兵追击,若公主能够走到出口且没有遇到士兵,就输出’Yes’,否则输出’No’。特别注意如果公主逃到出口时士兵也到了出口,公主也不能成功逃亡。
其实就是倒过来搜就可以,将’%’也就是出口当做搜索起点,记录搜索到公主和士兵的最短时间,如果公主时间短,则’Yes’,否则’No’。
转变思维很重要!!
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <queue>using namespace std;const int INF=0x3f3f3f3f;int n,m;char map[220][220];bool vis[220][220];int sx,sy;int dir[4][2]={1,0,-1,0,0,1,0,-1};struct Node{ int x,y,s; Node(int _x=0, int _y=0, int _s=0) { x=_x; y=_y; s=_s; }};bool bfs(){ int ps=INF,ss=INF; queue<Node>q; int s=0; memset(vis,false,sizeof(vis)); q.push(Node(sx,sy,s)); vis[sx][sy]=true; while(!q.empty()) { Node t=q.front(); q.pop(); for(int i=0; i<4; i++) { int nx=t.x+dir[i][0]; int ny=t.y+dir[i][1]; int ns=t.s+1; if (vis[nx][ny]) continue; vis[nx][ny]=true; if (nx<=0 || nx>n || ny<=0 || ny>m) continue; if (map[nx][ny]=='#') continue; if (map[nx][ny]=='@') { ps=min(ns,ps); continue; } if (map[nx][ny]=='$') { ss=min(ns,ss); continue; } q.push(Node(nx,ny,ns)); } } if (ps<ss) return true; else return false;}int main(){ while(scanf("%d%d",&n,&m)!=-1) { memset(map,0,sizeof(map)); for(int i=1; i<=n; i++) { scanf("%s",map[i]+1); for(int j=1; j<=m; j++) { if (map[i][j]=='%') { sx=i; sy=j; } } } if (bfs()) printf("Yes\n"); else printf("No\n"); } return 0;}
0 0
- 10.3 弱校 B Help the Princess! 搜索
- [JAG practice] B - Help the Princess! bfs
- Help the Princess! BFS队列
- 2016 ACM-ICPC Asia Regional B – Help the Princess!(bfs)
- (2016弱小联盟十一专场10.3) Help the Princess! BFS
- 搜索专题: HDU1027Ignatius and the Princess II
- 搜索专题: HDU1026Ignatius and the Princess I
- hdu1029 B - Ignatius and the Princess IV
- hdu1026 Ignatius and the Princess I(广度优先搜索)
- HDU 1026 Ignatius and the Princess I(记忆化搜索)
- B - Ignatius and the Princess IV (DP+模拟)
- B - Ignatius and the Princess IV——POJ
- B - Ignatius and the Princess III 整数拆分问题
- HDU 1026 Ignatius and the Princess I (搜索-广度优先搜索)
- CodeForces 618B-Guess the Permutation【搜索】
- Search Help (搜索帮助)
- B. Help Chef Gerasim
- 【搜索之BFS + 优先队列】杭电 hdu 1026 Ignatius and the Princess I
- 单片机—花样灯—代码
- 颜色平衡和偏色调节
- Android:ViewPager适配器PagerAdapter的使用
- linux菜鸟入门之进程
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- 10.3 弱校 B Help the Princess! 搜索
- 互联网热门岗位面试指导:后端工程师
- Codeforces Round #374 (Div. 2)D. Maxim and Array(贪心)
- 删除一个字符串中的指定字母,如:字符串 "aca",删除其中的 a 字母。
- 学习的第二天
- HDU 5823 color 状压DP
- Cocos2d-x3.8.1网络编程(HTTP)
- Binary Watch
- 感知哈希算法——找出相似的图片