Moscow subregional problem K. KMC Attacks interaction, queue optimization, enumeration

Source: Internet
Author: User

ACM ICPC 2010-2011 Neerc Moscow subregional Contest Moscow, October 24, 2010

Problem K. KMC Attacks
Time Limit:2 seconds
Memory limit:256 Megabytes


Warrant VI is a remote planet located in the Koprulu Sector. Warrant VI features a strange huge field,
Which was discovered by the Kel-morian Combine (KMC). The KMC is involved to a conflict with the
Scientific Community, which wishes to study the field, and the KMC intends to the significant
Mineral wealth located there.


The field is represented by a rectangle of size nxm cells. All coordinates is positive integers. The left
Upper corner are located at (1, 1) and the right bottom corner are at (n,m). The scientific Community
has sent a T-280 unit to the field. You were one of the KMC members, your task was to find the unit
and destroy it. Orbital base equipped with a scanner at your disposal. You can examine
any single cell and if scan reveals the unit, a detachment are sent to destroy it. After every scan the unit
changes it location (i, j) by moving to one of four (three if the unit was in a border cell Or if it is in
a corner cell) adjacent cells. The unit has a tracking device. For every unit's move you receive a signal:
' U ' (for-up, I-decreases by one), ' D ' (for-down, I-increases by one), ' L ' ( For left, J-decreases by one), or ' R '
(for-right, J-increases by one).


Time is running off so you were allowed to do not greater than 50000 scans to reveal the T-280 location.
Although the initial position of the unit is unknown, and each it move provides you with information. You
is forbidden to scan cells, which definitely cannot hold the unit according to the gathered information.


Input
This was an interactive problem. Your program should first read a line with the integers N and M from
The standard input (where N was the height and M is the width of the field, 1≤n,m≤200). Then, for
Every scan your program should print a corresponding request to the standard output (see below) and
Read a reply from the standard input. Each reply are placed in a separate line and are either ' U ', ' D ', ' L ', ' R '
(meaning that we have missed and the unit had moved in the given direction) or ' detected ' (meaning
That's your last scan had revealed the unit's position).


Output
For each scan print it coordinates I, j in a separate line (where I am the row number and J is the column
Number). You have the-flush the standard output after printing each request.


Examples
stdin stdout
1 2 1 1
L 1 1
Detected

Note
The output pipe from your program to the Interactor program and the input pipe back has the limited
Capacities. Your program must follow the Above-described protocol to avoid deadlock. Deadlock condition
is reported as a time-limit exceeded error.


To flush the standard output stream with the following statements:
In C with use Fflush (stdout); In C + + use Cout.flush (); In Java use System.out.flush ();

Once The unit is detected, your program must close the standard output stream and terminate with zero
Exit code.

Source

Moscow Subregional 2010


My Solution

Interactive Problem Queue optimization

Use Lcur and ucur to record the running path of the machine, and then lift the point.

One of the WA5, later found to record the largest and smallest lcur, ucur, thought that the intermediate process could cross over

And then started the tle awkward

And then we thought about queue optimization

Because it would have been a vis[][] Boolean array to record whether there is no enumeration of that starting point, but each time to Forfor enumeration and then use vis[][] to judge, this may be 4*1e4 in the step, may be close to 4*1E4 * (* * * *) = = 1e8, obviously still time-out

So queue optimization can reduce the complexity of O (n^4) to O (n^2)


#include <iostream> #include <cstdio> #include <cstdlib> #include <queue>using namespace std; typedef long LONG ll;const int MAXN = 2*1e2 + 8;bool vis[maxn][maxn];char msg[16];queue<pair<int, int> > Que;i    NT Main () {#ifdef LOCAL freopen ("A.txt", "R", stdin);    Freopen ("B.txt", "w", stdout);    int T = 1;    while (t--) {#endif//LOCAL int R, c, lcur = 0, ucur = 0, Maxlcur, Maxucur, Minlcur, minucur;    maxlcur = maxucur = 0;    minlcur = minucur = 0;    scanf ("%d%d", &r, &c);        for (int i = 1; I <= R; i++) {for (int j = 1; J <= C; j + +) {Que.push (Make_pair (i, j)); }} and (True) {while (!que.empty ()) {if (Que.front (). First-minucur <= R && Que.front (                ). first-maxucur > 0 && que.front (). second-minlcur <= C && que.front (). second-maxlcur > 0) {                printf ("%d%d\n", Que.front (). First-ucur, Que.front (). Second-lcur);Fflush (stdout);                Que.pop ();            Break        } que.pop ();        } scanf ("%s", msg);        if (msg[1] = = ' E ') exit (0);        else if (msg[0] = = ' L ') {lcur++; maxlcur = max (lcur, maxlcur);}        else if (msg[0] = = ' R ') {lcur--; minlcur = min (lcur, minlcur);}        else if (msg[0] = = ' U ') {ucur++; maxucur = max (ucur, maxucur);}        else if (msg[0] = = ' D ') {ucur--; minucur = min (ucur, minucur);}        cout<<maxlcur<<endl;        cout<<minlcur<<endl;        cout<<maxucur<<endl;    cout<<minucur<<endl;    } #ifdef LOCAL printf ("\ n"); } #endif//LOCAL return 0;}


Thank you!

------ from Prolights

Moscow subregional problem K. KMC Attacks interaction, queue optimization, enumeration

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.