zoukankan      html  css  js  c++  java
  • 日常训练 搜索 昨天的 忘记写了 昨天只写了两题

    一个题目是拓扑排序,这个昨天写了,然后就是这个bfs

    C. Ice Cave

    You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.

    The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.

    Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).

    You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?

    Input

    The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.

    Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).

    The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.

    The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.

    Output

    If you can reach the destination, print 'YES', otherwise print 'NO'.

    Examples
    input
    Copy
    4 6
    X...XX
    ...XX.
    .X..X.
    ......
    1 6
    2 2
    output
    Copy
    YES
    input
    Copy
    5 4
    .X..
    ...X
    X.X.
    ....
    .XX.
    5 3
    1 1
    output
    Copy
    NO
    input
    Copy
    4 7
    ..X.XX.
    .XX..X.
    X...X..
    X......
    2 2
    1 6
    output
    Copy
    YES
    Note

    In the first sample test one possible path is:

    After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.

    大意:

    给你一个n×m的表格,每个点有两种状态,一种是好冰(‘.’),一种是碎冰(‘X’),并且好冰被走过后就变成碎冰了,然后再告诉你起点和终点四个数(t1,c1),(t2,c2),问起点是否能到终点,保证起点是碎冰,终点不一定,最后要求掉进起点(即是碎冰,可以踩一下到另一个点再回来),在这期间(除了起点和终点)不得掉进任何一个碎冰,如果可行输出“YES”,不行“NO”。

    这个bfs不是很难,算一个模板题吧。

    思路就是只能走‘.' 所以如果是‘X’就 判断一下是不是终点,如果是就返回,不是就跳过。

    然后你发现就是一个普通的bfs

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <queue>
    #define inf 0x3f3f3f3f
    using namespace std;
    int n, m, gx, gy, sx, sy;
    int dx[4] = { 0,1,-1,0 };
    int dy[4] = { 1,0,0,-1 };
    int a[550][550];
    char s[550][550];
    struct node
    {
        int x, y;
        node(int x=0,int y=0):x(x),y(y){}
    };
    
    int bfs()
    {
        queue<node>que;
        que.push(node(gx, gy));
        while(!que.empty())
        {
            node u = que.front(); que.pop();
            for(int i=0;i<4;i++)
            {
                int tx = u.x + dx[i];
                int ty = u.y + dy[i];
    
                if (tx<1 || ty<1 || tx>n || ty>m) continue;
    
                if(s[tx][ty]=='X')
                {
                    if (tx == sx && sy == ty) return 1;
                }
                else
                {
                    s[tx][ty] = 'X';
                    que.push(node(tx, ty));
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        cin >> n >> m;
        for(int i=1;i<=n;i++)
        {
            cin >> s[i] + 1;
        }
        cin >> gx >> gy >> sx >> sy;
        int t=bfs();
        if (t) printf("YES
    ");
        else printf("NO
    ");
        return 0;
    }
  • 相关阅读:
    阿里云 linux centos7安装tomcat
    定义全局的输入框获取焦点指令vfocus
    vue按enter键刷新页面 使用@submit.native.prevent阻止表单默认提交,添加在form标签上
    vue按键修饰符@keyup.enter.native
    阿里云 linux centos7中安装redis
    CRON表达式
    python定时执行nutch爬取任务
    Spring中设置bean作用域
    Cassandra 安装
    cassandra入门
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10691462.html
Copyright © 2011-2022 走看看