hdu 2128 Tempter of the Bone II ( bfs+不好搞的判重 )

Tempter of the Bone II

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 98304/32768 K (Java/Others)
Total Submission(s): 1128    Accepted Submission(s): 282


Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.


The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.
 

Input
The input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block;
'1'--'9':explosives in that block.

Note,initially he had no explosives.

The input is terminated with two 0's. This test case is not to be processed.
 

Output
For each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.
 

Sample Input

4 4 SX.. XX.. .... 1..D 4 4 S.X1 .... ..XX ..XD 0 0
 

Sample Output

-1 9
 

Author
XHD
 

Source
HDU 2007-10 Programming Contest

感想:
这题其他的都简单,就是判重不好想。以前用的什么判重都不管用了,最后逼得我用了vectorvis[maxn][maxn]才解决了问题。过得有点囧,跑到3000ms了。
ps:网上很多代码都是错的,因为杭电数据水了,所以就过了,这份代码是严密的。

题意:
在一个矩阵中,给一个起点一个终点,矩阵中有的位置有墙,有的位置有炸弹,对于墙必须拿炸弹炸才能过,每走一步花费1s,每炸一次门花1s,问从起点到终点的最短时间。

思路:
因为地图时刻在变,所以一般的判重不行。我是这样处理的:
用两个long long 记录走到当前位置时的 炸弹的状态stateb、墙的状态statew,如果再次走到同一点的话,判断此时的stb、stw与以前经过这点时的状态有没有完全相同的,有的话就不用进栈了,没有的话就进栈。这样就可以保证所有的状态都进栈了,虽然有些是无效的,但是不好判断哪些是无效的,所以全部进栈处理。

ps:思路来源于:http://blog.csdn.net/acm_cxlove/article/details/7635497

代码:
#include 
#include 
#include 
#include 
#include 
#include 
#define maxn 10
using namespace std;

int n,m,ans;
int sx,sy,ex,ey;
long long stw,stb,curstw,curstb;
int mp[maxn][maxn];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
char s[maxn];
struct Node
{
    int x,y;
    int step,bomb;
    long long statew;
    long long stateb;
    friend bool operator <(const Node&xx,const Node&yy)
    {
        return xx.step>yy.step;
    }
} cur,now;
vectorvis[maxn][maxn];  // 位置+结构体判重
priority_queueq;

bool isok(int tx,int ty)  // 判重
{
    int i,j,sz;
    sz=vis[tx][ty].size();
    for(i=0; in||ty<1||ty>m) continue ;
            if(nbomb==0&&(nstw&(1LL<='1'&&s[j-1]<='9')
                {
                    mp[i][j]=s[j-1]-'0';
                    stb=(1LL<

代码2:
将炸弹和墙同时处理,会快一倍,优化到1400ms++了。
炸弹没取的时候状态可以看做墙,取了之后就变为空地了。所以地图就只有走和不走两个状态了,只用一个long long就能表示其状态了。这种处理也是严密的,想一想为什么?
#include 
#include 
#include 
#include 
#include 
#include 
#define maxn 10
using namespace std;

int n,m,ans;
int sx,sy,ex,ey;
long long stw,curstw;
int mp[maxn][maxn];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
char s[maxn];
struct Node
{
    int x,y;
    int step,bomb;
    long long statew;
    friend bool operator <(const Node&xx,const Node&yy)
    {
        return xx.step>yy.step;
    }
} cur,now;
vectorvis[maxn][maxn];  // 位置+结构体判重
priority_queueq;

bool isok(int tx,int ty)  // 判重
{
    int i,j,sz;
    sz=vis[tx][ty].size();
    for(i=0; in||ty<1||ty>m) continue ;
            if(nbomb==0&&(nstw&(1LL<='1'&&s[j-1]<='9')
                {
                    mp[i][j]=s[j-1]-'0';
                    stw=(1LL<


 

你可能感兴趣的:(搜索)