zoukankan      html  css  js  c++  java
  • hdu 4528(搜索好题)

    小明系列故事——捉迷藏

    Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 1406    Accepted Submission(s): 391


    Problem Description
      小明的妈妈生了三个孩子,老大叫大明, 老二叫二明, 老三..., 老三自然就叫小明了。
      一天,小明的妈妈带小明兄弟三人去公园玩耍,公园里面树木很多,有很多地方可以藏身, 于是他们决定玩捉迷藏。经过几轮的猜拳后,第一轮是小明来找其他两个人,游戏规则很简单:
       只要小明可以在规定的时间内找到他们就算小明获胜,并且被发现的两个人猜拳决定谁在下一轮负责找人;如果在规定的时间内只找到一个人,那么没有被发现的 人获胜,被找到的人下一轮负责找人;如果在规定的时间内一个人都没有找到,则小明失败了,下一轮还是他来找人。现在小明想知道,在规定时间内,自己是否可 以找到所有的人,现在他想请你来帮忙计算一下。
      为了简单起见,把公园看成是n行m列的矩阵,其中’S’表示小明,’D’表示大名,’E’表示 二明,’X’表示障碍物,’.’表示通路。这里,我们把发现定义为,可以直接看到对方, 也就是说两个人在同一行或者同一列,并且中间没有障碍物或者没有其他人就可以看到对方。并且假设,大明,二明藏好以后就不会再改变位置,小明每个单位时间 可以从当前的位置走到相邻的四个位置之一,并且不会走出公园。
     
    Input
    测试数据第一行是一个正整数T,表示有T组测试数据。
    每一组测试数据首先是三个正整数n,m,t,分别表示行数、列数和规定的时间,接下来n行,每行m个上述的字符,并且保证有且只有一个’S’,一个’E’,一个’D’。

    [Technical Specification]
    T < 200
    3 <= n, m <= 100
    0 <= t <= 100
     
    Output
    每组先输出一行Case c:(c表示当前的组数,从1开始计数);
    接下来一行,如果小明可以在规定时间内找到所有的人,则输出最少需要的时间,否则输出-1。
     
    Sample Input
    3 5 6 3 XXD... ....E. ....X. ....S. ...... 5 6 3 XDX... ....E. ...... ....S. ...... 5 6 8 XXDX.. .XEX.. ...... ....S. ......
     
    Sample Output
    Case 1: -1 Case 2: 3 Case 3: -1
     
    Source
     
    这个题最重要的两点:
    1.定义vis数组状态,因为这个点是可以往回走的。所以我们定义状态 vis[x,y,isVisD,isVisE]代表当前在x,y这点我们已经找到几个人了。
    2.预处理,首先预处理能够看到D和E的所有坐标..
    第一个条件想到了,,第二个条件没有想出来。。还是题目做得不够。
    #include<stdio.h>
    #include<string.h>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    const int N = 105;
    int n,m,t;
    char graph[N][N];
    bool vis[N][N][2][2];
    bool look[2][N][N]; ///预处理能够看到d(e)的位置
    int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    struct Node
    {
        int x,y;
        int step;
        bool flag1,flag2; ///flag1 is D,flag2 is E
    } s,d,e;
    bool check(int x,int y)
    {
        if(x<1||x>n||y<1||y>m) return false;
        return true;
    }
    int bfs()
    {
        memset(vis,false,sizeof(vis));
        s.flag1 = s.flag2 = false;
        if(look[0][s.x][s.y]) s.flag1 = true;
        if(look[1][s.x][s.y]) s.flag2 = true;
        queue<Node>q;
        vis[s.x][s.y][s.flag1][s.flag2] = true;
        q.push(s);
        while(!q.empty())
        {
            Node now = q.front();
            q.pop();
            if(now.flag1&&now.flag2) return now.step;
            //printf("%d %d %d %d %d
    ",now.x,now.y,now.flag1,now.flag2,now.step);
            if(now.step>t) return -1;
            for(int i=0; i<4; i++)
            {
                Node next = now;
                next.x+=dir[i][0];
                next.y+=dir[i][1];
                next.step+=1;
                if(!check(next.x,next.y)||graph[next.x][next.y]!='.') continue;
                if(look[0][next.x][next.y]) next.flag1 = true;
                if(look[1][next.x][next.y]) next.flag2 = true;
                if(vis[next.x][next.y][next.flag1][next.flag2]) continue; ///当前状态已经访问过了
                vis[next.x][next.y][next.flag1][next.flag2] = true;
                q.push(next);
            }
        }
        return -1;
    }
    void deal(int x,int y,bool flag)  ///由于只有200ms,预处理能够看到的位置
    {
        for(int i=0; i<4; i++)
        {
            int nx=x,ny=y;
            while(true)
            {
                nx+=dir[i][0],ny+=dir[i][1];
                if(!check(nx,ny)||graph[nx][ny]=='X') break;
                if(graph[nx][ny]=='E'&&flag==0) break;
                if(graph[nx][ny]=='D'&&flag==1) break;
                look[flag][nx][ny] = true;
            }
        }
    }
    int main()
    {
        int tcase;
        scanf("%d",&tcase);
        int time = 1;
        while(tcase--)
        {
            scanf("%d%d%d",&n,&m,&t);
            for(int i=1; i<=n; i++)
            {
                scanf("%s",graph[i]+1);
                for(int j=1; j<=m; j++)
                {
                    if(graph[i][j]=='S')
                    {
                        s.x=i,s.y = j;
                        graph[i][j] = '.';
                    }
                    if(graph[i][j]=='D')
                    {
                        d.x=i,d.y = j;
                    }
                    if(graph[i][j]=='E')
                    {
                        e.x=i,e.y = j;
                    }
                }
            }
            memset(look,false,sizeof(look));
            deal(d.x,d.y,0);
            deal(e.x,e.y,1);
            int res = bfs();
            printf("Case %d:
    %d
    ",time++,res);
        }
        return 0;
    }
    /*
    7 7 19
    S......
    .X.....
    ..X....
    ...X...
    ....X..
    .....XD
    ......E
    res : 18
    */
  • 相关阅读:
    c++中为什么static成员函数不能声明为const
    VS 如何查看反汇编代码?
    Algorithm: 获取指定范围的随机数(包含范围临界值)
    Windows2003防黑客入侵:系统权限与安全配置
    nc使用方法
    突破安全狗的技术总汇
    入侵检测经验总结
    Temporary failure in name resolution:Illegal seek(转)
    各种在线编辑器漏洞
    Linux加固
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5619559.html
Copyright © 2011-2022 走看看