In fact, this question is very simple. bfs or dfs finds all connected O, and if there is one of these O, it will remain unchanged; otherwise it will be surrounded, all of them can be changed to X, but bfs has not been written for a long time, which leads to no re-determination when entering the team, resulting in a large number of points joining the team more than once. So it's time for Large dataset. It is easy to pass after heavy judgment, 64 ms. Let's just move on to the Code:
/*** @ File Surrounded_Regions.cpp * @ Brief * @ author Brian * @ version 1.0 * @ date 2013-09-07 */# include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <memory. h> # include <algorithm> # include <math. h >#include <queue >#include <vector> using namespace std; class Solution {private: struct point {int x; int y; point (int _ x, int _ y): x (_ x), y (_ y) {}point () {}}; int start; Int end; point Points [100000]; void bfs (vector <char> & board, int r, int c, int row, int col) {vector <point> buf; queue <point> q; bool reachBoundary = false; start = 0; end = 0; // q. push (point (r, c); Points [end ++] = point (r, c); // while (! Q. empty () {while (start <end) {/* point p = q. front (); q. pop (); */point p = Points [start ++]; board [p. x] [p. y] = 'V'; // search up, down, left, right reachBoundary | = expand (board, q, p. x-1, p. y, row, col, 'O'); reachBoundary | = expand (board, q, p. x + 1, p. y, row, col, 'O'); reachBoundary | = expand (board, q, p. x, p. y-1, row, col, 'O'); reachBoundary | = expand (board, q, p. x, p. y + 1, row, col, 'O');} char ch = reachBoundary? 'K': 'X'; for (int I = 0; I <end; I ++) {point p = Points [I]; board [p. x] [p. y] = ch ;}int expand (vector <char> & board, queue <point> & q, int x, int y, int row, int col, char c) {if (x <0 | x> = row | y <0 | y> = col) return true; else {if (board [x] [y] = c) {// q. push (point (x, y); Points [end ++] = point (x, y); // This is judged and reused to prevent the same point from joining the team multiple times. When the next time we reach this point, // if judgment is not true, which prevents us from joining again. Board [x] [y] = c + 1;} return false;} public: Solution () {} void solve (vector <char> & board) {int row = board. size (); if (row = 0) return; int col = board [0]. size (); for (int r = 0; r <row; r ++) {for (int c = 0; c <col; c ++) {if (board [r] [c] = 'O') bfs (board, r, c, row, col) ;}} for (int r = 0; r <row; r ++) {for (int c = 0; c <col; c ++) {if (board [r] [c] = 'k ') board [r] [c] = 'O' ;}}}; int main () {Solution s; char src [30] [30] ={{ "xooooooooooooooooo" },{ "regular" },{ "ooooooxoooooooooooox" },{ "ooxoooooooooooooxo" },{ "regular "}, {"xoooxoooxoxoxoxo" },{ "yellow" },{ "xooxooooooxxooxoox" },{ "ooooooooxooxoooxox "}, {"OOOOXOXOOXXOOOOOXOOO" },{ "xxoooxoooooooooooooo" },{ "oxoxoooxoxoxoxoxoo" },{ "OOXOOOOOOOXOOOOOXOXO" },{ "xxooooooxoxxoooxoo" },{ "OOXOOOOOOOXOOXOXOXOO "}, {"oooxoooxxxooxoooxo" },{ "OOOOOOOOOOOOOOOOOOOO" },{ "XOOOOXOOOXXOOXOXOXOO" }}; // char src [30] [30] ={{ "OO "}, {"OO" }}; vector <char> board; int n = 20; for (int I = 0; I <n; I ++) {vector <char> row; for (int j = 0; j <n; j ++) {row. push_back (src [I] [j]);} board. push_back (row);} s. solve (board); return 0 ;}