• toj 2815 Searching Problem


    2815.   Searching Problem
    Time Limit: 1.0 Seconds   Memory Limit: 65536K
    Total Runs: 421   Accepted Runs: 177



    It is said that the predecessors had hidden a lot of expensive treasures just before the death of their city hundreds of years ago. People's searching for it has never stopped. The only problem is that they can't tell the exact position of the treasure. What's more, there exists great dangerousness in searching. So although the treasure has been their place for countries, it's still safe.

    You are one of the people who want to make a fortune. However, you're a very lucky man. You happen to get a secret map, and you know the map can lead you to the treasure directly.

    The map is a maze. There are five symbols on the map. They are 'S', 'D', '$', '1' and '0'. 'S' is the point you enter into the maze. '$' stand for the treasure in the maze. '0' and 'D' are the points you can walk along, while '1' is walls that you can't go through. You shall start at the 'S' point, and pick up the treasure in the maze along the '0' and 'D' points, then come out along the same way.

    It seems easy, and you'll become a wealthy man. But there is still a problem you have to solve. On every point marked as a 'D' there is a dog to protect the treasure. It's absurd that the dogs can live for countries without food! Just forget about it. So you decide to bring a gun with you. But how many bullets it will take? It's up to you to calculate here. Assume that you're a good shooter, and need only one bullet to shoot one dog.

    Input

    The first line of each test case is two integer R and C (1 ≤ R, C ≤ 50). R is the row number of the maze, and C is the column number of the maze. The following R lines will contain C characters for each. The R * C characters includes exactly one 'S' and one '$', the remains are '1', '0' and 'D'. It guarantees that the 'S' point is always on the edge of the maze.

    Two zeros indicate the end of the input.

    Output

    For each case, just print a line with the number K, which is the least number of bullets you need.

    Sample Input

    4 5
    00$00
    00100
    D00D0
    S0001
    4 5
    00$00
    01100
    D00D0
    S0001
    4 5
    DDDDD
    DD$DD
    D0D0D
    SDD0D
    0 0
    

    Sample Output

    0
    1
    2
    

    Problem Setter: Venice



    Source: TJU Programming Contest 2007


    Submit   List    Runs   Forum   Statistics

    #include <iostream>
    #include 
    <queue>
    #define MAX 54
    using namespace std;
    int mark[MAX][MAX],a[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
    char map[MAX][MAX];
    typedef 
    struct node
    {
        
    int x;
        
    int y;
        
    int step;
    }Point;
    queue
    <Point>Q;
    Point start,p,temp;
    int r,c,MM;
    void Init()
    {
        
    int i,j,k;
        
    for(i=0;i<r;i++)
            
    for(j=0;j<c;j++)
            {
                cin
    >>map[i][j];
                
    if(map[i][j]=='S')
                {
                    start.x
    =i;
                    start.y
    =j;
                    start.step
    =0;
                }
                mark[i][j]
    =-1;
            }
    }
    bool Bound(int x,int y)
    {
        
    if(x>=0&&y>=0&&x<r&&y<c)
            
    return true;
        
    else
            
    return false;
    }
    void BFS()
    {
        
    int k,tx,ty;
        
    while(!Q.empty())
            Q.pop();
        Q.push(start);
        mark[start.x][start.y]
    =0;
        MM
    =-1;
        
    while(!Q.empty())
        {
            p
    =Q.front();
            Q.pop();
            
    if(map[p.x][p.y]=='$')
            {
                
    if(p.step<MM||MM==-1)
                {
                    MM
    =p.step;
                }
            }
            
    for(k=0;k<4;k++)
            {
                tx
    =p.x+a[k][0];
                ty
    =p.y+a[k][1];
                
    if(!Bound(tx,ty)||map[tx][ty]=='1')
                    
    continue;
                
    if(map[tx][ty]=='0'||map[tx][ty]=='$')
                {
                    
    if(mark[tx][ty]==-1||p.step<mark[tx][ty])
                    {
                        temp.x
    =tx;
                        temp.y
    =ty;
                        temp.step
    =p.step;
                        mark[tx][ty]
    =p.step;
                        Q.push(temp);
                    }
                    
    continue;
                }
                
    if(map[tx][ty]=='D')
                {
                    temp.step
    =p.step+1;
                    
    if(mark[tx][ty]==-1||temp.step<mark[tx][ty])
                    {
                        temp.x
    =tx;
                        temp.y
    =ty;
                        
    //temp.step=p.step;
                        mark[tx][ty]=temp.step;
                        Q.push(temp);
                    }
                    
    continue;
                }
            }
        }
        printf(
    "%d\n",MM);
    }
    int main()
    {
        
    while(scanf("%d%d",&r,&c)!=EOF)
        {
            
    if(r==0&&c==0)
                
    break;
            Init();
            BFS();
        }
        
    return 0;
    }
  • 相关阅读:
    datatables ajax异步分页
    redis实现消息发布/订阅
    使用Tomcat+Redis来实现集群部署中的Session共享问题
    工具:从一个文件夹中复制jar到另一个文件夹中
    CentOS配置上网
    热力图与百度地图扩展实例
    【转】JavaWeb编码之get方式中文乱码问题
    数字转换大写核心类
    详说 Navicat for MySQL 快捷键
    MYBATIS常用的sql事例
  • 原文地址:https://www.cnblogs.com/forever4444/p/1456965.html
Copyright © 2020-2023  润新知