hdu Swipe Bo(bfs+状态压缩)错了多次的题

2024-05-12 20:32
文章标签 bfs 压缩 状态 hdu 多次 bo swipe

本文主要是介绍hdu Swipe Bo(bfs+状态压缩)错了多次的题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Swipe Bo

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1549    Accepted Submission(s): 315


Problem Description
“Swipe Bo” is a puzzle game that requires foresight and skill. 
The main character of this game is a square blue tofu called Bo. We can swipe up / down / left / right to move Bo up / down / left / right. Bo always moves in a straight line and nothing can stop it except a wall. You need to help Bo find the way out.
The picture A shows that we needs three steps to swipe Bo to the exit (swipe up, swipe left, swipe down). In a similar way, we need only two steps to make Bo disappear from the world (swipe left, swipe up)!

Look at the picture B. The exit is locked, so we have to swipe Bo to get all the keys to unlock the exit. When Bo get all the keys, the exit will unlock automatically .The exit is considered inexistent if locked. And you may notice that there are some turning signs, Bo will make a turn as soon as it meets a 

turning signs. For example, if we swipe Bo up, it will go along the purple line.
Now, your task is to write a program to calculate the minimum number of moves needed for us to swipe Bo to the exit.

Input
The input contains multiple cases, no more than 40.
The first line of each test case contains two integers N and M (1≤N, M≤200), which denote the sizes of the map. The next N lines give the map’s layout, with each line containing M characters. A character is one of the following: '#': represents the wall; 'S' represents the start point of the Bo; 'E' represents the exit; '.' represents an empty block; ‘K’ represents the key, and there are no more than 7 keys in the map; 'L','U','D','R' represents the turning sign with the direction of left, up, down, right.

Output
For each test case of the input you have to calculate the minimal amount of moves which are necessary to make Bo move from the starting point to the exit. If Bo cannot reach the exit, output -1. The answer must be written on a single line.

Sample Input
  
5 6 ###### #....# .E...# ..S.## .##### 5 6 ###### #....# .....# SEK.## .##### 5 6 ###### #....# ....K# SEK.## .##### 5 6 ###### #....# D...E# S...L# .#####

Sample Output
  
3 2 7 -1

Source
2013 Multi-University Training Contest 4

题意:有一个迷宫,包含墙、空白格子、起点S、终点E、方向格子(LRUD)和钥匙K。要求如下:

(1)每次转弯只能在碰到墙壁时(每次转弯的选择和初始时从S出发的方向选择均称为一次操作);

(2)对于方向格子,若到达该格子,不管周围是不是墙,必须转向该格子指示的方向(这个不算一次操作);

(3)若迷宫中没有钥匙存在,则求出S到E的最少操作次数;若有钥匙,则必须先遍历到每个钥匙之后才能去E(在这个过程中可以经过E也就是E不算做障碍)。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;const int N  = 225;
const int inf = 1<<29;
struct node{int x,y,sta,stp;
};
int n,m,k;
char mapt[N][N];
int K[N][N];
bool vist[N][N][1<<7];
int dir[4][2]={0,-1,0,1,-1,0,1,0};int judge1(node& now,int &e){int flag=0;if(now.x<0||now.x>=n||now.y<0||now.y>=m)return 0;if(mapt[now.x][now.y]!='#'){if(mapt[now.x][now.y]=='L')e=0,flag=1;else if(mapt[now.x][now.y]=='R')e=1,flag=1;else if(mapt[now.x][now.y]=='U')e=2,flag=1;else if(mapt[now.x][now.y]=='D')e=3,flag=1;else if(mapt[now.x][now.y]=='K')now.sta|=(1<<K[now.x][now.y]);if(flag&&vist[now.x][now.y][now.sta])return 0;else if(flag) vist[now.x][now.y][now.sta]=1;  //固定方向的位置,可以直接标记return 1;}else    //遇到墙,退一格,在当前位置停止{now.x-=dir[e][0];now.y-=dir[e][1];return 2;}}
int bfs(int sx,int sy){queue<node>q;node now,pre;for(int i=0; i<n; i++)for(int j=0; j<m; j++)for(int sta=0; sta<(1<<k); sta++)vist[i][j][sta]=0;now.x=sx,now.y=sy,now.sta=0,now.stp=0;q.push(now);vist[now.x][now.y][now.sta]=1;while(!q.empty()){pre=q.front(); q.pop();pre.stp++;for(int te=0; te<4; te++){int e=te;now=pre;while(1){   //找到一个停止点,或不能走,或走过了,则跳出now.x+=dir[e][0];now.y+=dir[e][1];int flag=judge1(now,e);if(flag==0)break;if(flag==1&&mapt[now.x][now.y]=='E'&&now.sta==(1<<k)-1){return now.stp;}if(flag==2){if(vist[now.x][now.y][now.sta])break;vist[now.x][now.y][now.sta]=1;q.push(now);break;}}}}return -1;
}
int main()
{int sx,sy;while(scanf("%d%d",&n,&m)>0){k=0;for(int i=0; i<n; i++){scanf("%s",mapt[i]);for(int j=0; j<m; j++)if(mapt[i][j]=='S')sx=i,sy=j;else if(mapt[i][j]=='K')K[i][j]=k++;}printf("%d\n",bfs(sx,sy));}
}



这篇关于hdu Swipe Bo(bfs+状态压缩)错了多次的题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983668

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin