HDU1429胜利大逃亡(续)HDU 1885 Key Task BFS+状态压缩+水

HDU1429

只有10把钥匙 1<<10足够了

标记用三维数组

用Linux好不习惯 继续克服~

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include<queue>
const int INF = 999999;
char mp[33][33];
bool vis[33][33][1026];
int n,m;
struct node
{int x,y,step;int key;
};
queue<node>q;
int xx[4]= {1,0,-1,0};
int yy[4]= {0,1,0,-1};
int bfs(int x,int y)
{while(!q.empty())q.pop();memset(vis,false,sizeof(vis));node front,rear;front.x=x,front.y=y;front.key=0;front.step=0;vis[x][y][0]=true;q.push(front);while(!q.empty()){front=q.front();q.pop();for(int i=0; i<4; i++){int dx=front.x+xx[i],dy=front.y+yy[i];if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='*'&&!vis[dx][dy][front.key]){if(mp[dx][dy]=='^')//到终点{return front.step+1;}else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿到钥匙{vis[dx][dy][front.key]=true;//标记rear.key =(front.key )| ( 1<<(mp[dx][dy]-'a'));//添加钥匙rear.x=dx,rear.y=dy,rear.step=front.step+1;q.push(rear);}else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//遇到门{if(front.key&(1<<(mp[dx][dy]-'A')))//如果有对应的钥匙{rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}else{rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}}}return 0;
}
int main()
{int t;//  freopen("in.txt","r",stdin);while(scanf("%d%d%d",&n,&m,&t)!=EOF){node s;for(int i=0; i<n; i++)scanf("%s",mp[i]);for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(mp[i][j]=='@')s.x=i,s.y=j,s.step=0;}}int ans=bfs(s.x,s.y);if(ans==0||ans>=t)printf("-1\n");else printf("%d\n",ans);}return 0;
}

HDU1885

这个就4把钥匙

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include<queue>
const int INF = 999999;
char mp[133][133];
bool vis[133][133][33];
int n,m;
int ke[28];
struct node
{int x,y,step;int key;
};
queue<node>q;
int xx[4]= {1,0,-1,0};
int yy[4]= {0,1,0,-1};
int bfs(int x,int y)
{while(!q.empty())q.pop();memset(vis,false,sizeof(vis));node front,rear;front.x=x,front.y=y;front.key=0;front.step=0;vis[x][y][0]=true;q.push(front);while(!q.empty()){front=q.front();q.pop();for(int i=0; i<4; i++){int dx=front.x+xx[i],dy=front.y+yy[i];if(dx>=0&&dy>=0&&dx<n&&dy<m&&mp[dx][dy]!='#'&&!vis[dx][dy][front.key]){if(mp[dx][dy]=='X')//到终点{return front.step+1;}else if(mp[dx][dy]>='a'&&mp[dx][dy]<='z')//拿钥匙{vis[dx][dy][front.key]=true;rear.key =(front.key )| ( 1<<(ke[mp[dx][dy]-'a']));rear.x=dx,rear.y=dy,rear.step=front.step+1;q.push(rear);}else if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z')//开门{if(front.key&(1<<(ke[mp[dx][dy]-'A']))){rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}else{rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1;vis[dx][dy][rear.key]=true;q.push(rear);}}}}return 0;
}
int main()
{//freopen("in.txt","r",stdin);ke['a'-'a']=0,ke['b'-'a']=1,ke['r'-'a']=2,ke['g'-'a']=3;while(scanf("%d%d",&n,&m),n+m){node s;for(int i=0; i<n; i++)scanf("%s",mp[i]);for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(mp[i][j]=='*')s.x=i,s.y=j,s.step=0;}}int ans=bfs(s.x,s.y);if(ans==0)printf("The poor student is trapped!\n");else printf("Escape possible in %d steps.\n",ans);}return 0;
}


转载于:https://www.cnblogs.com/kewowlo/p/4002588.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273660.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Docker 安装nginx,并挂载文件

创建挂载所需目录&#xff1a; mkdir /test/server/nginx/{conf,logs,html,conf.d} /test/server/nginx/conf创建nginx.conf文件&#xff0c;并编辑: user nginx; worker_processes 1;error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;events {wor…

http解码-5

http://www.freebuf.com/articles/web/18084.html (1) 在 前端安全技术中常见的编码绕过技术利用的就是不同系统间的编码位宽(addslashes()楼哦的那个)&#xff0c;不同编码的解码顺序(htmlParserjavascriptParser的解码顺序)&#xff0c;不同的解码规则(unicode->ascii转换…

IllegalArgumentException:argument type mismatch

Exception nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property id of class com.wzq.test.demo.entity.Fee with value 1211518556674199553Cause:java.lang.IllegalArgumentException: argument type mismatch 场景: 添加不传ID使…

hadoop家族的各个成员

这篇文章不提原理&#xff0c;讲讲hadoop及其周边项目的作用。 hadoop这个词已经流行好多年了&#xff0c;一提到大数据就会想到hadoop&#xff0c;那么hadoop的作用是什么呢&#xff1f; 官方定义&#xff1a;hadoop是一个开发和执行处理大规模数据的软件平台。核心词语是平台…

ArrayList 源码分析

介绍 ArrayList 是一个数组队列&#xff0c;相当于 动态数组。与Java中的数组相比&#xff0c;它的容量能动态增长。 结构 ArrayList继承于AbstractList&#xff0c;实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。如下图&#xff1a; public class Arra…

Poj 1556 The Doors 计算几何+最短路

其实本题非常的无脑&#xff0c;无脑拍完1A&#xff0c;写到blog里只因为TM无脑拍也拍了很久啊 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostr…

String equals()方法 源码分析

public boolean equals(Object anObject) {// :比较的引用类型&#xff0c;比较的是地址值是否相同if (this anObject) { //地址值相等&#xff0c;返回truereturn true;}// instanceOf判断一个对象是不是某个类型的实例if (anObject instanceof String) {String anotherStrin…

Google,真的要离我们而去吗?

Google,真的要离我们而去吗&#xff1f; 好怀念&#xff0c;真正要解决问题&#xff0c;还得搜google!转载于:https://www.cnblogs.com/fuyujian/p/3852444.html

Oracle 位图索引

内容简介: 1.位图索引 1.1位图索引使用注意事项; 1.2 使用位图索引; 1.3 位图索引对DML操作的影响; 2.位图连接索引 2.1 明确需求后使用位图索引; 2.1创建位图连接索引的注意事项: 1.位图索引: 1.1位图索引使用注意事项: ❏ 一般适用于低基数列; ❏ 适合数据仓库; ❏ 对于启用位…

oracle服务器和客户端字符集的查看和修改

一、什么是oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系。ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储&#xff0c;处理&#xff0c;检索数据。它使数据库工具&#xff0c;错误消息&#xff0c;排序次序&#xff0c;日…

Java 按位运算符(,|,^,,)

&(按位与) 定义&#xff1a;针对二进制&#xff0c;只要有一个为0&#xff0c;就为0。2 & 5 02的二进制&#xff1a;00000000 00000000 00000000 000000105的二进制&#xff1a;00000000 00000000 00000000 00000101 |(按位或) 定义&#xff1a;针对二进制&#xff0c…

Oracle 多行合并一行 方法

假如有如下表&#xff0c;其中各个i值对应的行数是不定的 Sql代码 SQL> select * from t; I A D ---------- ---------- ------------------- 1 b 2008-03-27 10:55:42 1 a 2008-03-27 10:55:46 1…

Docker 简单入门(一)

Docker 简介 Docker是一个开源的容器引擎&#xff0c;它有助于更快地交付应。Docker可将应用程序和基础设施层隔离&#xff0c;并且能将基础设施当作程序-样进行管理。使用Docker&#xff0c;可更快地打包、测试以及部署应用程序,并可以缩短从编写到部署运行代码的周期。 Docke…

PDF解决方案(2)--文件转PDF

相关专题链接&#xff1a; PDF解决方案&#xff08;1&#xff09;--文件上传 PDF解决方案&#xff08;2&#xff09;--文件转PDF PDF解决方案&#xff08;3&#xff09;--PDF转SWF PDF解决方案&#xff08;4&#xff09;--在线浏览 前言&#xff1a;上一篇中讲到的文件上传&…

Docker 常用命令(二)

Docker 镜像常用命令 搜索镜像 可使用 docker search 命令搜索存放在 Docker Hub 中的镜像。例如&#xff1a; docker search java 执行该命令后&#xff0c; Docker 就会在 Docker Hub 中搜索含有 java 这个关键词的镜像仓库。执行该命令后&#xff0c;可看到类似于如下的表格…

Docker 使用Dockerfile构建Docker(三)

Dockerfile 简单使用 先来编写一个最简单的 Dockerfile。 例如&#xff1a; FROM nginx RUN echo <h1>使用Dockerfile构建镜像</h1> > /usr/share/nginx/html/index.html 该 Dockerfile 非常简单&#xff0c;其中的 FORM 、 RUN 都是 Dockerfile 的指令。 FROM …

网络流之最大流问题

Reference&#xff1a; http://blog.csdn.net/rrerre/article/details/6751520 http://blog.csdn.net/y990041769/article/details/21026445 http://www.nocow.cn/index.php/Translate:USACO/NetworkFlow 最大流Edmonds_Karp算法模板&#xff1a; EK算法即增广路算法。 最大流最…

delphi读取excel

简单的例子 1 procedure TForm1.Button1Click(Sender: TObject);2 var3 ExcelApp,MyWorkBook: OLEVariant;4 begin5 opendialog1.Filter:Microsoft Excel Workbook (*.xls)|*.XLS|; 6 edit2.Text : sheet1;7 if opendialog1.Execute then8 begin9 edit1.Text:o…

Docker-compose 常用命令及网络设置(五)

Docker Compose 常用命令 build 构建或重新构建服务。服务被构建后将会以 project_service的形式标记,例如:comoretest db。help 査看指定命令的帮助文档,该命令非常实用。 docker-compose所有命令的帮助文档都可通过该命令查看。 docker-compose he lp COMMAND 示例 docker-co…

浅谈 trie树 及其实现

定义&#xff1a;又称字典树&#xff0c;单词查找树或者前缀树&#xff0c;是一种用于快速检索的多叉树结构&#xff0c; 如英文字母的字典树是一个26叉树&#xff0c;数字的字典树是一个10叉树。 核心思想&#xff1a;是空间换时间.利用字符串的公共前缀来降低查询时间的开销以…