HDU 1010 Tempter of the Bone

原创 2012年03月28日 15:04:52

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

简单搜索,奇偶剪枝,如果当前点到终点的曼哈顿距离(abs(startx-endx)+abs(starty-endy) ) 于剩余步数 奇偶性不同,说明就不可能在规定步数内到达目的地。

还有一个路径的判断的小优化,如果途中可走的点小于规定步数,就一定不可以在规定步数内到达目的地。

参考代码:

#include<iostream>
#include<string.h>
#include<cmath>
#include<cstdio>
using namespace std;

#define M 10

char mp[M][M];
int s[M][M],flag,endx,endy;
int f[4][2]={-1,0,1,0,0,-1,0,1};
int cut[8][8] = {
    0,1,0,1,0,1,0,1,
    1,0,1,0,1,0,1,0,
    0,1,0,1,0,1,0,1,
    1,0,1,0,1,0,1,0,
    0,1,0,1,0,1,0,1,
    1,0,1,0,1,0,1,0,
    0,1,0,1,0,1,0,1,
    1,0,1,0,1,0,1,0
};

void DFS(int x,int y,int t)
{
    if(flag)  return ;
    if(x==endx&&y==endy && s[x][y] == t) {flag = 1;return ;}
    if(x == endx && y== endy || s[x][y] > t) return ;
    if((cut[x][y] + cut[endx][endy]) % 2 != (t-s[x][y])%2) return ;
    int xx,yy;
    for(int i=0;i<4;++i)
    {
        xx = x+f[i][0]; yy = f[i][1]+y;
        if(mp[xx][yy] =='.')
        {
            mp[xx][yy]='X';
            s[xx][yy]=s[x][y]+1;
            DFS(xx,yy,t);
            mp[xx][yy]='.';
            if(flag) return ;
        }
    }
}
int main()
{
    int n,m,t,beginx,beginy;
    while(scanf("%d%d%d",&n,&m,&t) && m+n+t)
    {
        memset(s,0,sizeof(s));
        flag=0;getchar();
        int sum = 0;
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;++j)
            {
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='S')
                {
                    beginx=i;beginy=j;
                    mp[i][j] = '.';
                }
                else if(mp[i][j]=='D')
                {
                    endx=i;endy=j;
                    mp[i][j] = '.';
                }
                else if(mp[i][j] == 'X')
                    sum++;
            }
            getchar();
        }
        mp[beginx][beginy]='X';
        if(n * m - sum > t) DFS(beginx,beginy,t);
        if(flag) printf("YES\n");
        else    printf("NO\n");
    }
    return 0;
}


C语言DFS(5)____Tempter of the Bone(HDU 1010)

Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howev...

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O...

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

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...

hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)

本题链接:点击打开链接 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768...

HDU1010 Tempter of the Bone (DFS)

写的第二道DFS,这题在上ACM课的时候要求过,但是没好好去写,现在才第一次试着写,虽然还是要借鉴网上AC的代码,但是觉得自己对于如何写搜索有了一定的感受,这题的关键在于剪枝,奇偶性剪枝。 #incl...

【HDU 1010 Tempter of the Bone】 + DFS + 减枝

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other...

HDU 1010.Tempter of the Bone【DFS+奇偶性剪枝】9月19

Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fa...

hdu1010Tempter of the Bone

1.题目:   Problem Description   The doggie found a bone in an ancient maze, which fascinated him a lot...

hdu 1010 Tempter of the Bone dfs+奇偶剪枝

H - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64...

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

http://acm.hdu.edu.cn/showproblem.php?pid=1010 AC代码: #include #include #include cha...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 1010 Tempter of the Bone
举报原因:
原因补充:

(最多只允许输入30个字)