2020腾讯笔试--Ice Cave

         链接:http://codeforces.com/contest/540/problem/C

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

4 6X...XX...XX..X..X.......1 62 2

output

YES

这是腾讯笔试的原题,但我怎么觉得腾讯描述不清楚呢,并且没有给样例的说明.理解错了题意,卡了一个小时...

题意: 冰层由n*m个冰块组成,冰块有两种状态,如果为‘X‘,进入的话冰块就会破碎;‘.‘的话, 进入其状态会变成‘X‘

给你一个起点(r1,c1)和终点(r2,c2). 问是否存在一条从起点到终点的路径, 并且到达终点时试这块冰块破碎,从而进入下一层

其中起点因为是从上一层下降进入的, 所以其状态是‘X‘

分析: 可以把‘X‘看作不能走的点, 进入‘X‘的点,只有一种可能,就是这个点是终点;

这道题又加深了我对于dfs的理解, 只要存在一条路径,dfs就可以找到....

我又把这道题和CCF-1604-4-游戏作为比较, 其实这种多状态的图, 本质拓展开来也是一张一维平面图.

bfs和dfs一定会找到路径的

像xdoj 1045需要回溯的 是遍历所有情况吧... 并不是路径问题!!!

dfs版

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=507;
 4 int n, m;
 5 char s[N][N];
 6 int sx,sy,ex,ey;
 7 int dx[]={1,-1,0,0};
 8 int dy[]={0,0,-1,1};
 9 int cnt,num;
10 bool isok(int x, int y) {
11     return x>=1&&x<=n&&y>=1&&y<=m;
12 }
13 bool dfs(int x, int y) {
14     if (s[x][y]==‘X‘) {
15         if (x==ex&&y==ey)
16             return 1;
17         return 0;
18     }
19     s[x][y]=‘X‘;
20     for (int i=0;i<4;i++) {
21         int tx=x+dx[i];
22         int ty=y+dy[i];
23             if (isok(tx,ty)&&dfs(tx,ty))
24                 return 1;
25     }
26     return 0;
27 }
28 int main()
29 {
30     scanf("%d %d",&n,&m);
31     for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
32     scanf("%d %d %d %d", &sx,&sy,&ex,&ey);
33     s[sx][sy]=‘.‘;
34     bool flag=dfs(sx,sy);
35     if(flag) printf("YES\n");
36     else     printf("NO\n");
37     return 0;
38 }

bfs版

#include <bits/stdc++.h>
using namespace std;
const int N=507;
struct node {
    int x,y;
};
int n, m;
char s[N][N];
int sx,sy,ex,ey;
int dx[]={1,-1,0,0};
int dy[]={0,0,-1,1};
bool isok(int x, int y) {
    return x>=1&&x<=n&&y>=1&&y<=m;
}
bool bfs(int x, int y) {
    queue <node> q;
    node tmp={x,y}; q.push(tmp);
    while (!q.empty()) {
        tmp=q.front(); q.pop();
        if (s[tmp.x][tmp.y]==‘X‘) {
            if (tmp.x==ex&&tmp.y==ey) return 1;
            else                      continue;
        }
        s[tmp.x][tmp.y]=‘X‘;
        for (int i=0;i<4;i++) {
            node tm={tmp.x+dx[i],tmp.y+dy[i]};
            if (isok(tm.x, tm.y))
                q.push(tm);
        }
    }
    return 0;
}
int main()
{
    scanf("%d %d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
    scanf("%d %d %d %d", &sx,&sy,&ex,&ey);
    s[sx][sy]=‘.‘;
    bool flag=bfs(sx,sy);
    if(flag) printf("YES\n");
    else     printf("NO\n");
    return 0;
}

原文地址:https://www.cnblogs.com/xidian-mao/p/11387394.html

时间: 2024-10-23 20:01:15

2020腾讯笔试--Ice Cave的相关文章

ICE CAVE(BFS搜索(模拟))

Description 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 y

2015/4/8腾讯笔试

今天腾讯笔试,自我感觉良好,但是做完之后想到诸多问题....目前把记得的题目记录一下以便之后复习和思考. 首先是一道选择题:一个有五个节点的二叉树有多少种可能性?当时并没有提升到理论高度,只是单纯的分情况讨论,结果把自己给弄混了,随便选了一个,现在来整理一下: 一个有N个节点的二叉树有多少种可能..这个问题确切地说是一个数学问题,跟数据结构联系并不是很大,数学推导占据了很大成分. 首先考虑n=1的情况.显然只有根节点f(1) = 1; n = 2时,有一个根节点,还有n-1 = 1个节点可以作为

DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

题目传送门 1 /* 2 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 3 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两点相邻, 4 那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了:3. 普通的搜索看是否能到达, 5 若能还是要讨论终点踩几脚的问题:) 6 DFS 耗时大,险些超时,可以用BFS来做 7 */ 8 #include <cstdio> 9 #include <algorit

ice cave

Description 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 y

CodeForces 540C Ice Cave (BFS)

http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540C Description You play a computer game. Your character stands on some level of

C. Ice Cave (CF #301 (Div. 2) 搜索bfs)

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 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

CF #301 504C C. Ice Cave BFS

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 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

(简单广搜) Ice Cave -- codeforces -- 540C

http://codeforces.com/problemset/problem/540/C 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

2015腾讯笔试大题

今天做完腾讯的在线笔试,感觉自己弱爆了,选择题部分考得比较基础,但是考的面比较广,数据结构,计算机网络,算法常识,概率题,C,C++,都有.大题如下: 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code).请编写一个函数,使用递归方法生成N位的格雷码,并且保证这个函数的健壮性. 2. 有下图的题解,请用C/C++代码来列出满足下图0-100内的所有答案. 3. 如图所示,系统中有三个进程Producer,Transmitter和Consumer.