【HDU - 5094】 Maze (状态压缩+bfs)

题干:

This story happened on the background of Star Trek. 

Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS. 

The captain of Enterprise, James T. Kirk, had to fly to Qo’noS to rescue his deputy. Fortunately, he stole a map of the maze where Spock was put in exactly. 

The maze is a rectangle, which has n rows vertically and m columns horizontally, in another words, that it is divided into n*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from current location to next costs 1 second. And he is able to move to next location if and only if: 

Next location is adjacent to current Kirk’s location on up or down or left or right(4 directions) 
Open door is passable, but locked door is not. 
Kirk cannot pass a wall 

There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time. 

Initial location of Kirk was (1, 1) while Spock was on location of (n, m). Your task is to help Kirk find Spock as soon as possible.

Input

The input contains many test cases. 

Each test case consists of several lines. Three integers are in the first line, which represent n, m and p respectively (1<= n, m <=50, 0<= p <=10). 
Only one integer k is listed in the second line, means the sum number of gates and walls, (0<= k <=500). 

There are 5 integers in the following k lines, represents x i1, y i1, x i2, y i2, gi; when g i >=1, represents there is a gate of type gi between location (x i1, y i1) and (x i2, y i2); when g i = 0, represents there is a wall between location (x i1, yi1) and (x i2, y i2), ( | x i1 - x i2 | + | y i1 - y i2 |=1, 0<= g i <=p ) 

Following line is an integer S, represent the total number of keys in maze. (0<= S <=50). 

There are three integers in the following S lines, represents x i1, y i1 and q irespectively. That means the key type of q i locates on location (x i1, y i1), (1<= qi<=p).

Output

Output the possible minimal second that Kirk could reach Spock. 

If there is no possible plan, output -1. 

Sample Input

4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1

Sample Output

14

题目大意:

     给定一个棋盘,从(1,1)走到(n,m)有的任意两个格子之间的边视为:通路,门,墙。通路可以直接走,门必须早到相应的钥匙,墙永远不能通过。钥匙在一些给定点的格子中(同一个格子中可能有多把钥匙),问采取怎样的走法可以得到最少的移动步数。

解题报告:

       注意这题给的坐标代表的是一个房间,而不是点,读入的地图代表连接这两个房间的那条路是需要钥匙的还是不需要钥匙的还是墙。然后钥匙数状压一下,用邻接矩阵存图,然后用vis三维数组存当前状态和所得钥匙的情况数。vis[x][y][s]表示状态为s时到达(x,y)点是否已经到达过,s表示钥匙的得到情况的状态。

AC代码:

#include<bits/stdc++.h>using namespace std;
int tx[4] = {0,1,0,-1};
int ty[4] = {1,0,-1,0};
int n,m;
int maze[55][55][55][55],key[55][55];
bool vis[55][55][1<<11];
struct Node {int x,y;int step;int kk;Node(int x,int y,int step,int kk):x(x),y(y),step(step),kk(kk){}
};int bfs()
{queue<Node> q;memset(vis,0,sizeof(vis));q.push(Node(1,1,0,key[1][1]));vis[1][1][key[1][1] ] =1;int nx,ny;while(!q.empty()) {Node cur = q.front();q.pop();if(cur.x == n && cur.y == m) return cur.step;for(int k = 0; k<4; k++) {nx = cur.x + tx[k];ny = cur.y + ty[k];int t = maze[cur.x][cur.y][nx][ny];int nowkey = cur.kk | key[nx][ny];int w = 1<<(t-1);if(nx > n || ny > m || nx <1 || ny <1) continue;if(vis[nx][ny][nowkey] == 1) continue;if(t == 0 ) continue;if(t!=-1 && (nowkey & w ) == 0) continue;vis[nx][ny][nowkey] = 1;q.push(Node(nx,ny,cur.step+1,nowkey)); }}return -1;}int main()
{//n行m咧 int x1,x2,y1,y2;int g,p,k;while(~scanf("%d%d%d",&n,&m,&p) ) {scanf("%d",&k);memset(maze,-1,sizeof(maze));memset(key,0,sizeof(key));for(int i = 1; i<=k; i++) {scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);maze[x1][y1][x2][y2] = maze[x2][y2][x1][y1] = g;}int s;scanf("%d",&s);while(s--) {scanf("%d%d%d",&x1,&y1,&g);g--;key[x1][y1] |=(1<<g);}printf("%d\n",bfs());}return 0 ;
}

总结:

   1.没事不要瞎定义变量,也不要瞎定义全局变量(因为有可能定义了然后错用了还不报错)!这题的bfs中我刚开始就定义了x,y,然后有一个地方应该是cur.x和cur.y我用成了x,y。。。本来只是落下了cur.但谁知编译器没报错!所以答案显然是错的。

   2.if中该加括号的地方一定要加括号,你咋知道在评测机上的编译器上是怎么跑的程序。

   3.类似g--这样的细节一定要注意

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

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

相关文章

oracle idl_ub1$,system表空间急剧增大原因分析

system表空间增大是正常的&#xff0c;但急剧增大是不合理的。1有可能是用户对象错误的放在系统表空间中2也可能是system表空间的UNDO过大3还有可能和高级复制的空间使用有关可通过如下语句查看一下是不是有应用的段放到了SYSTEM中&#xff1a;select OWNER,SEGMENT_NAME,SEGME…

【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)

题干&#xff1a; Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two pl…

oracle启动监听读取哪个文件,监听服务启动及数据文件恢复oracle数据库

最近遭遇了 oralce 监听服务启动了 又自行关闭的 悲惨经历我把我的过程和大家分享一下&#xff01;1)排查原因程序员是懒惰的&#xff0c;我始终都希望能够成功启动监听服务&#xff0c;但是就是事与愿违有一下方式可能不能成功启动监听1.端口占用&#xff0c;oralce 要用到152…

linux串口写入命令失败,linux – 从串口读取失败

我有以下C程序&#xff1a;#include #include #include int main(){int fd open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NONBLOCK);if(fd < 0){perror("Could not open device");}printf("Device opened\n");struct termios options;tcgetattr…

【HDU - 1172】猜数字 (枚举暴力)

题干&#xff1a; 猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的&#xff1a;计算机随机产生一个四位数&#xff0c;然后玩家猜这个四位数是什么。每猜一个数&#xff0c;计算机都会告诉玩家猜对几个数字&#xff0c;其中有几个数字在正确的位置上。 比如计算机随…

linux内核支持的加密算法,Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口...

本文将主要介绍&#xff0c;如何在应用程序空间中(user space) 调用内核空间(kernel space)加密模块提供的加密算法API。方法一&#xff1a;通过调用crypto: af_alg - User-space interface for Crypto API&#xff0c; Herbert Xu 2010年&#xff0c;给内核2.6.X 接口实现下面…

【HDU - 2571】 命运(记忆化搜索)

题干&#xff1a; 穿过幽谷意味着离大魔王lemon已经无限接近了&#xff01; 可谁能想到&#xff0c;yifenfei在斩杀了一些虾兵蟹将后&#xff0c;却再次面临命运大迷宫的考验&#xff0c;这是魔王lemon设下的又一个机关。要知道&#xff0c;不论何人&#xff0c;若在迷宫中被…

【HDU - 2089 】不要62 (dp)

题干&#xff1a; 杭州人称那些傻乎乎粘嗒嗒的人为62&#xff08;音&#xff1a;laoer&#xff09;。 杭州交通管理局经常会扩充一些的士车牌照&#xff0c;新近出来一个好消息&#xff0c;以后上牌照&#xff0c;不再含有不吉利的数字了&#xff0c;这样一来&#xff0c;就可…

nmon监控linux内存,使用Nmon监控Linux系统性能

Nmon (又称 Nigel’s Monitor) 是一款常用的系统性能监视工具&#xff0c;由 IBM 工程师 Nigel Griffiths 开发&#xff0c;适用于 AIX 和 Linux 操作系统。该工具可以直接在屏幕上显示当前操作系统的资源利用率&#xff0c;以帮助大家找出系统瓶颈和协助系统调优。由于其十分出…

【Codeforces - 632C】The Smallest String Concatenation (对string巧妙排序)

题干&#xff1a; Youre given a list of n strings a1, a2, ..., an. Youd like to concatenate them together in some order such that the resulting string would be lexicographically smallest. Given the list of strings, output the lexicographically smallest…

linux mysql授权远程登录,Linux中 MySQL 授权远程连接的方法步骤

说明&#xff1a;当别的机子(IP )通过客户端的方式在没有授权的情况下是无法连接 MySQL 数据库的&#xff0c;如果需要远程连接 Linux 系统上的 MySQL 时&#xff0c;必须为其 IP 和 具体用户 进行 授权 。一般 root 用户不会提供给开发者。如&#xff1a;使用 Windows 上的 SQ…

【HDU - 3466 】Proud Merchants(dp,背包问题,巧妙排序)

题干&#xff1a; Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more…

linux 双通道 磁盘,HP MSA2012SA 双通道 磁盘阵列配置说明 for linuxoracle

HP MSA2012SA磁盘阵列配置说明说明&#xff1a;可以先安装HP服务器的操作系统&#xff0c;等安装完了以后再配置磁阵&#xff1b;1、安装好服务器的操作系统&#xff1b;该安装的包都安装上&#xff1b;2、磁盘阵列上架&#xff0c;加电&#xff0c;SAS线不用接服务器&#xff…

linux mariadb 乱码,MariaDB插入中文数据乱码解决过程

基本情况&#xff1a;MariaDB安装方式&#xff1a;yum乱码解决过程&#xff1a;1.查看当前数据库编码(登录数据库后)# show variables like character%;(上图为已经配置成功)2.如果结果不为上图则需要设置数据库配置文件•编辑 /etc/my.cnf.d/client.cnf 文件&#xff0c;添加如…

【nyoj - 890】 分东西 (水题 二进制)

题干&#xff1a; 分东西 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;1 输入 第一行输出一个数i表示有i组情况&#xff08;0<i<10&#xff09; 接下来的i行&#xff0c;每一行输入两个个数M(0<M<1000000)和N(0<N<2…

摩托罗拉为什么要限制自家linux手机,摩托罗拉为何在安卓手机大放异彩的时候,突然开始衰败了呢?...

摩托罗拉从一开始就走在了安卓的道路上&#xff0c;并且魅力四射&#xff0c;可以说一时间也是风光无比。对比诺基亚坚定的走向WP之路&#xff0c;这一点摩托罗拉没有走错。安卓当时的热门机中&#xff0c;摩托罗拉的里程碑系列可以算作是经典之作。销量也可以进入当年的前三。…

【POJ - 3624 】Charm Bracelet (dp,0-1背包裸题)

题干&#xff1a; Bessie has gone to the malls jewelry store and spies a charm bracelet. Of course, shed like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi…

pta输出三角形字符阵列c语言,C语言l|博客园作业11

这个作业属于哪个课程C语言程序设计II这个作业要求在哪里链接我在这个课程的目标是掌握C语言以及熟练运用这个作业在哪个具体方面帮助我实现目标询问同学&#xff0c;百度&#xff0c;vs2019上的报错参考文献链接1.1 题目名6-1 统计某类完全平方数本题要求实现一个函数&#xf…

【nyoj - 252】 01串(简单dp)

题干&#xff1a; 01串 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;2 输入 第一行有一个整数n&#xff08;0<n<100&#xff09;,表示有n组测试数据; 随后有n行&#xff0c;每行有一个整数m(2<m<40)&#xff0c;表示01串的…

c语言乘法表 m*(9-i),C语言做九九乘法表.doc

C语言做九九乘法表#include void main(){int i,j,x;/*第一种*/printf("第一种&#xff1a;\n");for(i1;i<9;i){for(ji;j>1;j--){printf("%d*%d%d\t",i,j,i*j);}printf("\n");}/*第二种*/printf("第二种&#xff1a;\n");for(i9;…