#301 (Div.2) C. Ice Cave

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.