HDU 1010 (搜索+奇偶剪枝)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010

题目大意:给定起点和终点,问刚好在t步时能否到达终点。

解题思路

4个剪枝。

①dep>t剪枝

②搜到一个解后剪枝

③当前走到终点最少步数>满足条件还需要走的步数剪枝(关键)

③奇偶剪枝(关键):当前走到终点步数的奇偶性应该与满足条件还需要走的步数奇偶性一致。

其中三四两步放在一步中写:remain=abs(x-ex)+abs(y-ey)-abs(dep-t)

奇偶剪枝的原理:abs(x-ex)+abs(y-ey)为奇,则说明到达终点肯定还要走奇数步,反之偶数步。于是得和abs(dep-t)即还需走的步数奇偶性协调。

所以③④剪枝条件这么写if(remain>0||remain%2) return;

#include "cstdio"
#include "string"
#include "iostream"
#include "cstring"
using namespace std;
int n,m,t,sx,sy,ex,ey,map[15][15],dir[4][2]={-1,0,1,0,0,-1,0,1};
bool vis[15][15],flag;
int ABS(int x) {return x<0?-x:x;}
void dfs(int x,int y,int dep)
{
    vis[x][y]=true;
    if(dep>t) return;
    if(flag) return;
    if(dep==t&&map[x][y]==3) {flag=true;return;}
    int remain=ABS(x-ex)+ABS(y-ey)-ABS(dep-t);
    if(remain>0||remain&1) return;
    for(int s=0;s<4;s++)
    {
        int X=x+dir[s][0],Y=y+dir[s][1];
        if(vis[X][Y]||X<0||X>n||Y<0||Y>m||map[X][Y]==0) continue;
        dfs(X,Y,dep+1);
        vis[X][Y]=false;
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    string tt;
    while(cin>>n>>m>>t&&n)
    {
        flag=false;
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            cin>>tt;
            for(int j=0;j<tt.size();j++)
            {
                if(tt[j]==‘S‘) {map[i][j+1]=2;sx=i;sy=j+1;}
                if(tt[j]==‘D‘) {map[i][j+1]=3;ex=i;ey=j+1;}
                if(tt[j]==‘.‘) map[i][j+1]=1;
                if(tt[j]==‘X‘) map[i][j+1]=0;
            }
        }
        dfs(sx,sy,0);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}
11864555 2014-10-13 19:36:45 Accepted 1010 515MS 284K 1408B C++ Physcal
时间: 2024-10-26 09:38:38

HDU 1010 (搜索+奇偶剪枝)的相关文章

hdu 1010 启发式搜索+奇偶剪枝

#include <cstdio> #include <iostream> #include <cstring> #include <queue> using namespace std; #define INF 10000000 int n,m,t; struct point{ int x,y; }; point p; int vis[10][10]; int vs[10][10]; char a[10][10]; int v[4][2] = {0,1,1

hdu 1010 深搜+剪枝

深度搜索 剪枝 还不是很理解 贴上众神代码 //http://blog.csdn.net/vsooda/article/details/7884772#include<iostream> #include<math.h> using namespace std; char map[10][10]; int N,M,T; int di,dj,escape; int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; void dfs(int x,int y,

HDOJ1010-Tempter of the Bone(搜索+奇偶剪枝)

Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried despe

HDU 1010 Tempter of the Bone 骨头诱惑(AC代码)DFS搜索+剪枝法

参考了别人的思路:将迷宫外围四面都筑墙‘X’.方便减少代码量. 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <math.h> 5 using namespace std; 6 vector<string> v; 7 int n,m; 8 int x_1,y_1,x_2,y_2; 9 bool maze(int x,int y,int t) //目

hdu 1010(迷宫搜索,奇偶剪枝)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 144191    Accepted Submission(s): 38474 Problem Description The doggie fou

HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 125945    Accepted Submission(s): 33969 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of Bone DFS + 奇偶剪枝

奇偶剪枝: 对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1). 否则必不可达. #include <iostream> #inclu

HDU 1010 深搜+奇偶剪枝

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 贴个资料: http://acm.hdu.edu.cn/forum/read.php?tid=6158 奇偶剪枝: 对于从起始点 s 到达终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y -

HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a