T1215拯救公主
1 #include <cstdio>
2 #include <queue>
3 #include <set>
4 #include <cstring>
5 #include <string>
6 #define N 205
7 using namespace std;
8 int r, c, k;
9 char mp[N][N]; //地图位置
10 int tag[N][N]; //标志位,判断是否重复走
11 int door[N*N][2] = {}; //传送门位置
12 int door_cnt = 0; //有多少道传送门
13 int sx, sy, ex, ey; //起点和终点
14 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; //方向
15 typedef struct Node{ //每一个状态的结构体
16 int x, y, step; //位置和步数
17 //set<int> s; //开始是使用set保存宝石种类,发现0 > -1 为假,遂改为复杂形态
18 int ecnt; //宝石数量
19 bool e[5] = {}; //5种宝石是否被发现
20 Node(int x, int y, int step, int ecnt):x(x), y(y), step(step), ecnt(ecnt){}; //构造函数
21 }node;
22
23 bool judge(node nx){ //判断是否往下走
24 if(nx.x >= 0 && nx.x < r && nx.y >= 0 && nx.y < c && mp[nx.x][nx.y] != '#' &&
25 nx.ecnt > tag[nx.x][nx.y]){
26 return true;
27 }
28 return false;
29 }
30 int bfs(){
31 node cur(sx, sy, 0, 0), next(sx, sy, 0, 0); //当前状态和下一下状态
32 queue<node> q; //队列
33 q.push(cur);
34 while(!q.empty()){
35 cur = q.front();
36 q.pop();
37 if(cur.x == ex && cur.y == ey && cur.ecnt >= k){ //满足结束条件,返回
38 //printf("%d ", cur.s.size());
39 return cur.step;
40 }
41 //printf("%d %d %d %d\n", cur.x, cur.y, cur.ecnt, cur.step);
42 //getchar();
43 for(int i = 0; i < 4; i++){ //上下左右试探
44 next.x = cur.x + dir[i][0];
45 next.y = cur.y + dir[i][1];
46 next.step = cur.step + 1;
47 next.ecnt = cur.ecnt;
48 memcpy(next.e, cur.e, 5); //计算下一步的所有数据
49 //printf("tag:%d %d %d\n", next.ecnt > tag[next.x][next.y], next.ecnt, tag[next.x][next.y]);
50 if(judge(next)){ //判断是否可以继续往下走
51 tag[next.x][next.y] = next.ecnt;
52 if(isdigit(mp[next.x][next.y])){ //是否是宝石
53 int e_cnt = 0;
54 next.e[mp[next.x][next.y]-48] = true;
55 for(int j = 0; j < 5; j++){
56 if(next.e[j]){
57 e_cnt++;
58 }
59 }
60 next.ecnt = e_cnt;
61 q.push(next);
62 }else if(mp[next.x][next.y] == '$'){ //是否是传送门
63 for(int j = 0; j < door_cnt; j++){
64 next.x = door[j][0];
65 next.y = door[j][1];
66 q.push(next);
67 }
68 }else if(mp[next.x][next.y] == 'E' || mp[next.x][next.y] == '.'){//正常道路
69 q.push(next);
70 }
71 }
72 }
73 }
74 return -1;
75 }
76 int main(){
77 int t;
78 scanf("%d", &t);
79 while(t--){
80 memset(tag, -1, sizeof(tag)); //初始化标志位
81 memset(mp, 0, sizeof(mp)); //初始化地图
82 door_cnt = 0; //传送门初始化
83 scanf("%d %d %d", &r, &c, &k); //地图大小和需要的宝石种类
84 getchar();
85 for(int i = 0; i < r; i++){ //记录初始位置,结束位置和所有传送门
86 scanf("%s", mp[i]);
87 for(int j = 0; j < c; j++){
88 switch(mp[i][j]){
89 case 'S':
90 sx = i;
91 sy = j;
92 break;
93 case 'E':
94 ex = i;
95 ey = j;
96 break;
97 case '$':
98 door[door_cnt][0] = i;
99 door[door_cnt][1] = j;
100 door_cnt++;
101 break;
102 }
103 }
104 }
105 int cnt = bfs(); // bfs暴搜
106 if(cnt == -1){
107 printf("oop!");
108 }else{
109 printf("%d\n", cnt);
110 }
111 }
112 return 0;
113 }
T1215拯救公主的更多相关文章
- noi 7221 拯救公主 (状态压缩+bfs)
/* 这题实在调糊了 借鉴的题解的一些判断方法 位运算大法好 - - 因为要集齐所有的宝石所以状态压缩一下 f[i][j][s]将s化为二进制 每一0表示该宝石没有 1表示该宝石有 有:到(i,j)这 ...
- c++小游戏——拯救公主
#include<stdio.h> #include<ctime> #include<time.h> //suiji #include<windows.h&g ...
- T1215:迷宫
[题目描述] 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行.同时当Extense处在某 ...
- openjudge 拯救公主
点击打开题目 看到这道题,第一感觉是我有一句m2p不知当讲不当讲 传送门就算了,你提莫还来宝石,还不给我每种最多有几个~~ 在一般的迷宫问题里,无论已经走了多少步,只要到达同一个点,状态便是等价的,但 ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- OpenJudge4980:拯救行动//stl优先队列
总时间限制: 10000ms 内存限制: 65536kB 描述 公主被恶人抓走,被关押在牢房的某个地方.牢房用N*M (N, M <= 200)的矩阵来表示.矩阵中的每项可以代表道路(@). ...
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- bzoj3007: 拯救小云公主
Description 英雄又即将踏上拯救公主的道路…… 这次的拯救目标是——爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了,因为面前不只是一只boss,而是 ...
- hdu 2102 A计划
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...
随机推荐
- Pyqt5使用
一.帮助文档 二.PyQt5库结构 三. 面向对象的编程模式 class Windows(QWidget): def __init__(self): #继承父类的QWidget的方法 super(). ...
- JavaScript常见笔试题分析
1.Javascript的typeof可能返回的结果有哪些? 答:共6种,具体为number ,boolean,string,undefined,function,object(对象或者null返 ...
- springboot(六)Email demo
项目中经常使用邮件发送提醒功能,比如说更新安全机制,发送邮件通知用户等 一.简单邮件发送 导入依赖: <dependency> <groupId>org.springframe ...
- Git使用指南(上)
1 Git简介 学习一门技术老师更加倾向于看官网的. 度娘看完了,官网看完了,大家还是很懵逼 学生成绩管理系统 登录模块 3.2 登录模块进一步完善 缺一个验证码的功能 3.3 登录模 ...
- 记一次 Raven2 渗透(phpmailer漏洞+UDF提权)
目录: 1. 寻找IP 2.dirb目录爆破 2.PHPMailer漏洞反弹得到shell 3.python版本的exp修改 4.查看wordpress的wp-config.php配置文件得到数据库账 ...
- Chrome Enhanced Protection
Chrome Enhanced Protection chrome://settings/security?q=enhanced 站内外链跳转拦截 refs xgqfrms 2012-2020 www ...
- TypeORM Entity
TypeORM Entity Entity Entity is a class that maps to a database table when using SQL database(or col ...
- website captcha
website captcha 验证码 hCaptcha hCaptcha通过询问对人类来说很容易且对机器来说很困难的简单问题,可以帮助您喜欢的Web服务阻止机器人,垃圾邮件和滥用行为. https: ...
- TypeScript Generics
TypeScript Generics https://www.typescriptlang.org/docs/handbook/generics.html 泛型 1 Generic Interfac ...
- macOS & Nginx
macOS & Nginx ngnix # 使用 brew 安装(如果没有 brew 命令,需要自行安装 brew) $ brew install nginx $ nginx -h # 查看 ...