zoukankan      html  css  js  c++  java
  • BFS:HDU2597-Dating with girls(2) (分时间标记状态)

    Dating with girls(2)
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1418    Accepted Submission(s): 393


    Problem Description
    If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
    The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
    There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.

     

    Input
    The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
    The next r line is the map’s description.
     

    Output
    For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
     

    Sample Input
    1
    6 6 2
    ...Y..
    ...#..
    .#....
    ...#..
    ...#..
    ..#G#.
     

    Sample Output
    7
     

    Source
    HDU 2009-5 Programming Contest
     

    Recommend
    lcy


    解题心得:
    1、这个题是一个新的类型的bfs搜索,其中有一点很重要,那就是在k的倍数的时候墙会消失,所以在处理的时候要注意每个点的状态的标记,因为可能在墙消失之后有些走了的地点还需要重新走,所以状态要分时间标记,原本想的是标记墙在的状态和墙不在的状态就可以了,但是仔细一想,每一刻的状态是相互影响的,墙消失 的前一秒对墙消失的时候有影响,所以需要标记t%k的每一个时刻的状态,由于数据量不大,所以可以如此处理。
    2、在处理地图中的元素随时间会产生变化的时候我们需要分时间标记状态,看前后时间是否会互相的影响,若后一时刻需要独立的处理不能受前一个时刻的状态影响应该将状态分时间标记,当然也要注意数据量,数据量太大的时候这种方法可能并不适用。



    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 110;
    int k,n,m;
    char maps[maxn][maxn];
    int dir[4][2] = {1,0,0,1,-1,0,0,-1};
    bool vis[maxn][maxn][15],flag;//三维数组第三维为时间的标记
    struct node
    {
        int x,y;
        int step;
    } now,Next;
    
    bool check(int x,int y)
    {
        if(x<0 || y<0 || x>=n || y>=m)
            return false;
        else
            return true;
    }
    
    void pre_maps()
    {
        now.x = -1;
        now.y = -1;
        memset(maps,0,sizeof(maps));
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0; i<n; i++)
            scanf("%s",maps[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(maps[i][j] == 'Y')
                {
                    now.x = i;
                    now.y = j;
                    now.step = 0;
                    return ;
                }
            }
    }
    
    void bfs()
    {
        queue <node> qu;
        qu.push(now);
        memset(vis,0,sizeof(vis));
        vis[now.x][now.y][now.step%k] = 1;
        while(!qu.empty())
        {
            now = qu.front();
            if(maps[now.x][now.y] == 'G')
            {
                printf("%d
    ",now.step);
                flag = true;
                return ;
            }
            qu.pop();
            Next.step = now.step + 1;
            for(int i=0; i<4; i++)
            {
                Next.x = now.x + dir[i][0];
                Next.y = now.y + dir[i][1];
                if(check(Next.x,Next.y))
                {
                    if((maps[Next.x][Next.y] != '#' || !(Next.step%k)) && !vis[Next.x][Next.y][Next.step%k])
                    {
                        qu.push(Next);
                        vis[Next.x][Next.y][Next.step%k] = 1;
                    }
                }
            }
        }
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            flag = false;
            pre_maps();
            if(now.x == -1 && now.y == -1)
            {
                printf("Please give me another chance!
    ");
                continue;
            }
            bfs();
            if(!flag)
                printf("Please give me another chance!
    ");
        }
    }



  • 相关阅读:
    转:超实用!聊聊图标设计流程及小技巧
    WPF 高性能画心电图
    转:WPF .NET 4.0下实现外发光效果
    (转)翻译:使用ViewModel模式来简化WPF的TreeView
    (转)How to get TreeViewItem from HierarchicalDataTemplate item?
    (转)WPF中的TreeView入门
    (转) WPF Treeview 学习 图标,checkbox,右键菜单
    linux中的定时任务--cron任务
    tar命令的使用与学习
    linux学习一
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107353.html
Copyright © 2011-2022 走看看