2020腾讯笔试--Ice Cave

Posted xidian-mao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 6
X...XX
...XX.
.X..X.
......
1 6
2 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;

 

以上是关于2020腾讯笔试--Ice Cave的主要内容,如果未能解决你的问题,请参考以下文章

2015腾讯校招笔试,二面经(菜鸟的流水账)

腾讯校招前端开发笔试初试总结

2015腾讯暑期实习笔试题目

腾讯笔试(4.2)

腾讯笔试(4.2)

腾讯移动客户端笔试记录