标签:
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 into a conflict with the
Scientific Community, which wishes to study the field, but the KMC intends to hold on to the significant
mineral wealth located there.
The field is represented by a rectangle of size N ×M cells. All coordinates are positive integers. The left
upper corner is located at (1, 1) and the right bottom corner is at (N,M). The Scientific Community
have sent a T-280 unit to the field. You are one of the KMC members, your task is to find the unit
and destroy it. You have an orbital base equipped with a scanner at your disposal. You can examine
any single cell and if scan reveals the unit, a detachment is sent to destroy it. After every scan the unit
changes its location (i, j) by moving into one of four (three if the unit is in a border cell or two 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 out so you are allowed to make not greater than 50000 scans to reveal the T-280 location.
Although the initial position of the unit is unknown, each its move provides you with information. You
are forbidden to scan cells, which definitely cannot hold the unit according to the gathered information.
Input
This is an interactive problem. Your program should first read a line with two integers N and M from
the standard input (where N is 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 is placed in a separate line and is either ’U’, ’D’, ’L’, ’R’
(meaning that you have missed and the unit has moved in the given direction) or ’DETECTED’ (meaning
that your last scan has revealed the unit’s position).
Output
For each scan print its coordinates i, j in a separate line (where i is the row number and j is the column
number). You have to 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 have 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 use the following statements:
In C 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
My Solution
交互题 队列优化
用 lCur 和 uCur记录 机器的运行路径, 然后枚举起点就好
其中WA5, 后来发现要记录最大的和最小的 lCur, uCur, 以为可能中间过程越界
然后就开始TLE了尴尬
后来才想到队列优化
因为本来是一个vis[][]布尔数组来记录有没有枚举过那个起点, 但每次要forfor枚举然后用vis[][]判断, 这样可能是 4*1e4步内的化, 可能要接近 4*1e4 * (200 * 200) == 4* 1e8了显然还是超时的
所以队列优化可以 O(n^4)的复杂度降低到 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; int 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)); } } while(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 2010 Problem K. KMC Attacks 交互题、队列优化、枚举
标签:
原文地址:http://blog.csdn.net/prolightsfxjh/article/details/52167292