/*
题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎
DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了;2. 若两点相邻,
那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了;3. 普通的搜索看是否能到达,
若能还是要讨论终点踩几脚的问题:)
DFS 耗时大,险些超时,可以用BFS来做
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std; const int MAXN = 5e2 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int sx, sy, ex, ey;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dx[] = {, -, , };
int dy[] = {, , -, }; void DFS(int x, int y)
{
vis[x][y] = true;
if (maze[x][y] == 'X') return ; for (int i=; i<; ++i)
{
int tx = x + dx[i];
int ty = y + dy[i]; if (tx <= n && tx >= && ty <= m && ty >= && !vis[tx][ty])
{
DFS (tx, ty);
}
} return ;
} int main(void) //Codeforces Round #301 (Div. 2) C. Ice Cave
{
//freopen ("C.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
memset (vis, , sizeof (vis));
for (int i=; i<=n; ++i)
{
scanf ("%s", maze[i]+);
}
scanf ("%d%d", &sx, &sy);
scanf ("%d%d", &ex, &ey); int cnt = ; bool flag = false;
for (int i=; i<; ++i)
{
int tx = ex + dx[i];
int ty = ey + dy[i];
if (tx == sx && ty == sy) flag = true;
if (tx <= n && tx >= && ty <= m && ty >= && maze[tx][ty] == '.') cnt++;
} if (sx == ex && sy == ey)
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
else if (flag)
{
if (maze[ex][ey] == 'X') puts ("YES");
else
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
}
else
{
maze[sx][sy] = '.';
DFS (sx, sy);
if (vis[ex][ey])
{
if (maze[ex][ey] == 'X') puts ("YES");
else if (cnt >= ) puts ("YES");
else puts ("NO");
}
else puts ("NO");
}
} return ;
}
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <iostream>
using namespace std; const int MAXN = 5e2 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int sx, sy, ex, ey;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dx[] = {, -, , };
int dy[] = {, , -, }; bool BFS(void)
{
queue<pair<int, int> > Q;
Q.push (make_pair (sx, sy)); while (!Q.empty ())
{
int x = Q.front ().first; int y = Q.front ().second; Q.pop ();
for (int i=; i<; ++i)
{
int tx = x + dx[i];
int ty = y + dy[i]; if (tx == ex && ty == ey) return true; if (tx <= n && tx >= && ty <= m && ty >= && maze[tx][ty] == '.')
{
maze[tx][ty] = 'X';
Q.push (make_pair (tx, ty));
}
}
} return false;
} int main(void) //Codeforces Round #301 (Div. 2) C. Ice Cave
{
//freopen ("C.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
memset (vis, , sizeof (vis));
for (int i=; i<=n; ++i)
{
scanf ("%s", maze[i]+);
}
scanf ("%d%d", &sx, &sy);
scanf ("%d%d", &ex, &ey); int cnt = ; bool flag = false;
for (int i=; i<; ++i)
{
int tx = ex + dx[i];
int ty = ey + dy[i];
if (tx == sx && ty == sy) flag = true;
if (tx <= n && tx >= && ty <= m && ty >= && maze[tx][ty] == '.') cnt++;
} if (sx == ex && sy == ey)
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
else if (flag)
{
if (maze[ex][ey] == 'X') puts ("YES");
else
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
}
else
{
maze[sx][sy] = '.';
//DFS (sx, sy);
if (BFS () == true)
{
if (maze[ex][ey] == 'X') puts ("YES");
else if (cnt >= ) puts ("YES");
else puts ("NO");
}
else puts ("NO");
}
} return ;
}
BFS做法