Ice Cave-CF-C(bfs)

来源:互联网 发布:lomo风格 知乎 编辑:程序博客网 时间:2024/05/17 21:54

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 mcolumns. 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'.

Example
Input
4 6X...XX...XX..X..X.......1 62 2
Output
YES
Input
5 4.X.....XX.X......XX.5 31 1
Output
NO
Input
4 7..X.XX..XX..X.X...X..X......2 21 6
Output
YES


一道广搜题,从一点搜到另一点,这都很基础

但是题目不是这样就完了,要再找一条路绕一圈再回到这个点

注意

如 目标点为 1  1  你绕到  2  1  然后再回1  1这样是不行的

要绕一个大圈,题目没说,别问我怎么知道的,多么痛的领悟!

还有这道题用DFS会超时,而BFS差点超时!同上别问为什么!

翻翻前面的博客发现自己曾经总结过,找最短路时BFS会比DFS少用不少时间

都怪自己平时不注意回顾自己的博客!

写BFS时注意,能节省一点时间就节省,否则很有可能就超时了!

先看一下DFS超时代码

#include<stdio.h>char map[505][505];int x1,y1,x2,y2;int m,n,flag,l;int dir[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};void dfs(int a,int b,int c,int d){    if(flag>=2)    {        return;    }    for(int i=0; i<4; i++)    {        int dx=a+dir[i][0];        int dy=b+dir[i][1];        //printf("%d %d!!\n",dx,dy);        if(dy==d&&dx==c&&map[dy][dx]=='X')        {            flag+=2;            return;        }        if(dx<=n&&dy<=m&&dx>0&&dy>0&&map[dy][dx]!='X')        {            //printf("%d %d\n",dx,dy);            map[dy][dx]='X';            dfs(dx,dy,c,d);            map[dy][dx]='.';        }    }}int main(){    while(scanf("%d%d",&m,&n)!=EOF)    {        flag=0,l=0;        for(int i=0; i<=m+1; i++)        {            for(int j=0; j<=n+1; j++)            {                if(i==0||j==0||i==m+1||j==n+1)                {                    map[i][j]='X';                }                else                {                    scanf(" %c",&map[i][j]);                }            }        }        scanf("%d%d%d%d",&y1,&x1,&y2,&x2);        for(int i=0; i<4; i++)        {            int dx=x2+dir[i][0];            int dy=y2+dir[i][1];            if(map[dy][dx]!='X')            {                l++;            }        }        if(l<=1&&map[y2][x2]!='X')        {            printf("NO\n");            continue;        }        /*for(int i=1; i<=m; i++)        {            for(int j=1; j<=n; j++)            {                printf("%c",map[i][j]);            }            printf("\n");        }*/        dfs(x1,y1,x2,y2);        if(flag>=2)        {            printf("YES\n");        }        else        {            printf("NO\n");        }    }}

当到  text 14 的时候就超时了

下面看一下DFS代码

#include <stdio.h>   #include <string>  #include <iostream>  #include <algorithm>  using namespace std;  #include <queue>  #include <vector>        int dir[4][2]={1,0,-1,0,0,1,0,-1};  char mp[510][510];  int n,m;  int ex,ey;  int flag;    struct point  {      int x,y;  };    int ok(int x,int y)  {      if(x<1||x>n||y<1||y>m)          return 0;      return 1;  }    int bfs(int x,int y)  {      queue<point> q;        point sta,nw,nxt,ep;      ep.x=ex;      ep.y=ey;      sta.x=x,sta.y=y;       q.push(sta);        while(!q.empty())      {          nw=q.front();          q.pop();          //printf("%d %d\n",nw.x,nw.y);            for(int i=0;i<4;i++)          {              nxt.x=nw.x+dir[i][0];              nxt.y=nw.y+dir[i][1];                            if(ok(nxt.x,nxt.y))              {                  if(nxt.x==ep.x&&nxt.y==ep.y)                  {                      if(mp[nxt.x][nxt.y]=='.')                          mp[nxt.x][nxt.y]='X';                      else                          return 1;                      q.push(nxt);                   }                  else if(mp[nxt.x][nxt.y]=='.')                  {                      mp[nxt.x][nxt.y]='X';                      q.push(nxt);                  }              }          }        }      return 0;  }  int main()  {      int sx,sy;      while(scanf("%d%d",&n,&m)!=EOF)      {          for(int i=1;i<=n;i++)               scanf("%s",mp[i]+1);             scanf("%d%d",&sx,&sy);          scanf("%d%d",&ex,&ey);          flag=0;                    if(bfs(sx,sy))              puts("YES");          else               puts("NO");        }      return 0;  }  

这个代码是差一点不过,为什么那?VJ人多的时候就超时,等比赛过了就AC

应该可以再更优化的,这就是比赛题,以后要尽量避免这种情况







0 0
原创粉丝点击