zoukankan      html  css  js  c++  java
  • ACM HDU 1010 Tempter of the Bone

    开始  就是暴力DFS搜索  没有什么剪枝  于是  TLE

    之后发现 要 奇偶剪枝  

    http://www.cnblogs.com/zhourongqing/archive/2012/04/28/2475684.html

    很详细  即使判断 递归时  是否  那一点与终点最短时间 和 已用时间 之和   被总时间 减去 是否为 偶数 

    不是偶数  就return 

    虽然这个不好弄  但还是写出来了 

      接下来  WA了 3个小时    怀疑过输入的问题 。。 各种怀疑。。

    最终最终  发现时起点  的状态 忘记标记了  坑爹啊。。。

    下一次 类似的一定要注意  

    还有这是 到终点是否恰好到达 ,  要用DFS  解决

    剪枝   

    #include<stdio.h>
    #include<math.h>
    int n,m,t;
    int s_x,s_y,e_x,e_y;
    int flag;
    char map[20][20];
    int dir[4][2]={0,1,0,-1,1,0,-1,0};
    void dfs(int x,int y,int s)
    {
        int i,tem,n_x,n_y;
        if(flag) return;
        if(x==e_x && y==e_y && s==t)
        {
            flag=1;
        }
        tem=t-s-abs(e_x-x)-abs(e_y-y);
        if(tem%2!=0) return;
        for(i=0;i<4;i++)
        {
            n_x=x+dir[i][0];
            n_y=y+dir[i][1];
            if(n_x>=0 && n_x<n && n_y>=0 && n_y<m && map[n_x][n_y]!='X')
            {
                map[n_x][n_y]='X';
                dfs(n_x,n_y,s+1);
                map[n_x][n_y]='.';
            }
        }
    }
    int main()
    {
        int i,j;
        while(scanf("%d%d%d",&n,&m,&t),n||m||t)
        {
            getchar();
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    scanf("%c",&map[i][j]);
                    if(map[i][j]=='S')
                    {
                        s_x=i; s_y=j;
                    }
                    else if(map[i][j]=='D')
                    {
                        e_x=i; e_y=j;
                    }
                }
                getchar();
            }
            flag=0;
            map[s_x][s_y]='X';//就是这条语句   弄得我检查了几个小时 注意啊!
            dfs(s_x,s_y,0);
            if(flag)
                printf("YES\n");
            else
                printf("NO\n");
        }
        return 0;
    }
  • 相关阅读:
    Advanced Configuration Tricks
    Reviewing the Blog Module
    Editing and Deleting Data
    Making Use of Forms and Fieldsets
    Understanding the Router
    SQL Abstraction and Object Hydration
    Preparing for Different Databases
    Java学习理解路线图
    Openstack学习历程_1_视频
    CentOS安装Nginx负载
  • 原文地址:https://www.cnblogs.com/napoleon/p/3181202.html
Copyright © 2011-2022 走看看