Cf 254d rats (enumeration + BFS + various chaos)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: Give a map where there are some mice and two bombs in some locations. The attack range can spread in four directions every second and cannot penetrate the wall. The bomb will pop up in a few seconds and ask if two locations can be found. Place the bomb and all the mice will be killed. Http://codeforces.com/contest/254/problem/D
First, we can find out all the R and use the number of R to pruning once. We can find that D is actually very small. Maybe this is a breakthrough. In the extreme condition of each bomb, the range that can spread in D seconds can be calculated as a diamond. The maximum coverage of the two bombs can also be calculated. You can cut it down here. For many cases of R, the pass is dropped directly. Good Then consider the feasible range. The location where a bomb can arrive is definitely less than D in the distance from Manhattan. Take every mouse as the center. Take out all feasible points first (= at least cover a mouse before you can start to think about it. In fact, this can also be trimmed. We consider that when a bomb spreads, the most extreme case is that the outermost circle is a bomb, and then it spreads in the outermost circle, it is equivalent to spreading a bomb for 2 * D times. You can calculate the maximum number of locations that are feasible. If there are too many feasible locations, it means that the mouse is too scattered and not feasible. Then, for the feasible solution to BFs, record which mice can be reached, and for each possible feasible solution, use the set to record the mouse labels. In the end, enumerate and combine the set to determine whether all rats can be handled. It took me a long time to add a small pruning, and it was too difficult to merge the set. In fact, complexity is feasible. The maximum number of BFS statuses is about 226. the maximum number of feasible solutions is about 1200.
# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1600005 # define M 40 # define n 1005 # define maxn 2000005 # define EPS 1e-7 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MP (a, B) make_pair (a, B) # define MEM (A, B) memset (a, B, sizeof ()) # define ll long # define mod 1000000007 # define lson step <1 # define rson step <1 | 1 # define sqr (A) (a) * ()) # define key_value ch [CH [root] [1] [0] # define test puts ("OK"); # define PI ACOs (-1.0) # define lowbit (X) (x) & (-(X) # pragma comment (linker, "/Stack: 1024000000,1024000000") # define VI vecto R <int> using namespace STD; int row, Col, D; char STR [N] [N]; vector <pair <int, int> rat, TMP; int P [N] [N]; set <int> S [N]; int way [4] [2] = {0, 1, 0,-1, 0, -1,0}; bool flag [N] [N]; bool check (int x, int y) {If (x> = 0 & Y> = 0 & x <row & Y <Col & STR [x] [Y]! = 'X') return true; return false;} int main () {freopen ("input.txt", "r", stdin); freopen ("output.txt", "W ", stdout); While (scanf ("% d", & Row, & Col, & D )! = EOF) {rat. clear (); MEM (p,-1); For (INT I = 0; I <row; I ++) {scanf ("% s ", STR [I]); For (Int J = 0; j <Col; j ++) if (STR [I] [J] = 'R') {rat. pb (MP (I, j); P [I] [J] = rat. size () ;}// pruning, two positions, extended D times, can calculate the maximum number of lattice int T = d + 1; if (rat. size ()> 2*(2 * sqr (t)-2 * t + 1) {puts ("-1"); continue;} For (INT I = 0; I <rat. size (); I ++) {for (Int J =-D; j <= D; j ++) {for (int K =-D; k <= D; k ++) {If (ABS (j) + ABS (k)> d) continue; int x = rat [I]. first + J; int y = rat [I]. Second + k; If (check (x, y) TMP. pb (MP (x, y) ;}} sort (TMP. begin (), TMP. end (); int size = unique (TMP. begin (), TMP. end ()-TMP. begin (); T * = 2; If (size> 2*(2 * sqr (t)-2 * t + 1) {puts ("-1 "); continue;} For (INT I = 0; I <size; I ++) {queue <pair <int, int>, int> que; while (! Que. empty () que. pop (); MEM (flag, false); flag [TMP [I]. first] [TMP [I]. second] = true; que. push (MP (TMP [I]. first, TMP [I]. second), 0); While (! Que. empty () {pair <int, int>, int> U = que. front (), V; que. pop (); If (STR [U. first. first] [U. first. second] = 'R') s [I]. insert (P [U. first. first] [U. first. second]); If (U. second> = d) continue; For (INT I = 0; I <4; I ++) {v = u; V. first. first + = way [I] [0]; V. first. second + = way [I] [1]; V. second + = 1; if (check (v. first. first, V. first. second )&&! Flag [v. first. first] [v. first. second]) {flag [v. first. first] [v. first. second] = true; que. push (v) ;}}} bool ans = false; For (INT I = 0; I <size &&! Ans; I ++) {for (Int J = I + 1; j <size &&! Ans; j ++) {// Well, this pruning is directly from t to 100 ms, and it is too slow for me to use set (s [I]. size () + s [J]. size () <rat. size () continue; set <int> temp (s [I]); For (set <int >:: iterator it = s [J]. begin (); it! = S [J]. end (); It ++) temp. insert (* It); If (temp. size () = rat. size () {ans = true; printf ("% d \ n", TMP [I]. first + 1, TMP [I]. second + 1, TMP [J]. first + 1, TMP [J]. second + 1) ;}}} if (! Ans) puts ("-1"); For (INT I = 0; I <size; I ++) s [I]. Clear () ;}return 0 ;}


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.