1. Title Description: Click to open the link
2. Solving ideas: The problem is solved by using BFS. Because there are two situations when walking, when encountering ' X ' will fall to the next level, when encountered '. ' is still at this level, just '. ' To become ' X '. Then you can search directly with BFS. If you encounter an ' x ', you just need to determine if it is the end, otherwise skip, if you encounter '. ', then change it to ' X ' and merge into the queue. During the game I have been wandering between DFS and BFS, but in fact it is not difficult to find that if using DFS, there may be no moving situation, need to go back. Time complexity will be relatively high, so naturally think of BFS.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 500+5int N, m;int s, T, E, D;int DX [] = {1,-1, 0, 0};int dy[] = {0, 0, 1,-1};char g[n][n];typedef pair<int, Int>p;bool bfs () {Queue<p>q;q.pu SH (P (s,t)); G[s][t] = ' X '; while (!q.empty ()) {s = Q.front (). First, T = Q.front (). second; Q.pop (); for (int i = 0; i < 4; I + +) {int xx = s + dx[i];int yy = t + dy[i];if (XX < 0 | | xx >= n | | yy < 0 | yy >= m) continue;if (g[xx][yy] = = ' X ') {if (xx = = E&&yy = = d) return true;continue;} G[XX][YY] = ' X '; Q.push (P (xx, yy));}} return false;} int main () {//freopen ("T.txt", "R", stdin), while (~SCANF ("%d%d", &n, &m) {for (int i = 0; i < n; i++) scanf ("%s", G[i]), scanf ("%d%d%d%d", &s, &t, &e, &d), s--, T--, e--, d--;p rintf ("%s\n", BFS ()? "YES": "NO");} return 0;}
#301 (Div.2) C. Ice Cave