搜索 HOJ 1136 Robbery

Robbery

My Tags   (Edit)
  Source : ACM ICPC Mid-Central European 1999
  Time limit : 3 sec   Memory limit : 32 M

Submitted : 16, Accepted : 9

Inspector Robstop is very angry. Last night, a bank has been robbed and the robber has not been caught. And this happened already for the third time this year, even though he did everything in his power to stop the robber: as quickly as possible, all roads leading out of the city were blocked, making it impossible for the robber to escape. Then, the inspector asked all the people in the city to watch out for the robber, but the only messages he got were of the form ``We don't see him."

But this time, he has had enough! Inspector Robstop decides to analyze how the robber could have escaped. To do that, he asks you to write a program which takes all the information the inspector could get about the robber in order to find out where the robber has been at which time.

Coincidentally, the city in which the bank was robbed has a rectangular shape. The roads leaving the city are blocked for a certain period of time t, and during that time, several observations of the form ``The robber isn't in the rectangle Ri at time ti'' are reported. Assuming that the robber can move at most one unit per time step, your program must try to find the exact position of the robber at each time step.


Input 

The input contains the description of several robberies. The first line of each description consists of three numbers W, H, t (1 <= W,H,t <= 100) where W is the width, H the height of the city and t is the time during which the city is locked.

The next contains a single integer n (0 <= n <= 100), the number of messages the inspector received. The next n lines (one for each of the messages) consist of five integers ti, Li, Ti, Ri, Bi each. The integer ti is the time at which the observation has been made (1 <= ti <= t), and Li, Ti, Ri, Bi are the left, top, right and bottom respectively of the (rectangular) area which has been observed. (1 <= Li <= Ri <= W, 1 <= Ti <= Bi <= H; the point (1, 1) is the upper left hand corner, and (W, H) is the lower right hand corner of the city.) The messages mean that the robber was not in the given rectangle at time ti.

The input is terminated by a test case starting with W = H = t = 0. This case should not be processed.


Output 

For each robbery, first output the line ``Robbery #k:'', where k is the number of the robbery. Then, there are three possibilities:

If it is impossible that the robber is still in the city considering the messages, output the line ``The robber has escaped.''

In all other cases, assume that the robber really is in the city. Output one line of the form ``Time step : The robber has been at x,y." for each time step, in which the exact location can be deduced. (x and y are the column resp. row of the robber in time step .) Output these lines ordered by time .

If nothing can be deduced, output the line ``Nothing known." and hope that the inspector will not get even more angry.

Output a blank line after each processed case.


Sample Input

4 4 5
4
1 1 1 4 3
1 1 1 3 4
4 1 1 3 4
4 4 2 4 4
10 10 3
1
2 1 1 10 10
0 0 0


Sample Output

Robbery #1:
Time step 1: The robber has been at 4,4.
Time step 2: The robber has been at 4,3.
Time step 3: The robber has been at 4,2.
Time step 4: The robber has been at 4,1.

Robbery #2:
The robber has escaped.



题意:一个长方形的城市里面出现了一名盗贼,然后在t秒内,城市被完全封锁,然后在某些时间知道某些区域内一定不会出现这名盗贼,然后最后要问在某一个时间内能否确定这个盗贼的位置,并把它输出。

思路:其实我们把每个时间能去的地方做好标记,然后进行搜索就行了。然后输出答案的时候对每一个时间能去到的位置进行搜索,如果有多个能去到的,就说明这个时间不能确定盗贼在哪个位置,不必输出,如果每一个时间都能到达多个位置,那么最后输出Nothing know. 注意 句号。。。。 然后如果第一秒内没有任何一个位置能作为起点,那么就是盗贼不可能在这个城市了。详情请看代码

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define LL long long
#define mp make_pair
bool G[110][110][110];
int reach[110][110][110];
int Move[2][5] = { { 0,1,0,-1 , 0} , {1,0,-1,0 , 0} };
int W , H , T , n , Case;

void init()
{
	memset(G,0,sizeof(G));
	memset(reach,-1,sizeof(reach));
	for (int i = 1 ; i <= H ; ++i)
		for (int j = 1 ; j <= W ; ++j)
			for (int k = 0 ; k <= T+1 ; ++k)
				G[k][i][j] = true;
}

void input()
{
	scanf("%d",&n);
	while (n--)
	{
		int ti , Li , Ti , Bi , Ri;
		scanf("%d%d%d%d%d",&ti,&Li,&Ti,&Ri,&Bi);
		for (int i = Ti ; i <= Bi ; ++i)
			for (int j = Li ; j <= Ri ; ++j)
				G[ti][i][j] = false;
	}
}

int dfs(int r,int c,int t)
{
	if (t==T) return reach[t][r][c] = 1;
	if (reach[t][r][c]!=-1) return reach[t][r][c];
	for (int i = 0 ; i < 5 ; ++i)
	{
		int rr = r+Move[0][i];
		int cc = c+Move[1][i];
		if (G[t+1][rr][cc] && dfs(rr,cc,t+1)==1)
			reach[t][r][c] = 1;
	}
	if (reach[t][r][c]==-1) return reach[t][r][c] = 0;
	return reach[t][r][c];
}


void solve()
{
	printf("Robbery #%d:\n",Case);
	int cnt = 0;
	int r , c;
	for (int i = 1 ; i <= H ; ++i)
		for (int j = 1 ; j <= W ; ++j) if (G[1][i][j] && dfs(i,j,1))
			++cnt;
	if (cnt==0)
		printf("The robber has escaped.\n");
	else 
	{
		bool flag = false;
		for (int t = 1 ; t <= T ; ++t) 
		{
			cnt = 0;
			for (int i = 1 ; i <= H ; ++i)
				for (int j = 1 ; j <= W ; ++j) if (reach[t][i][j]==1)
					++cnt , r = i , c = j;
			if (cnt==1) 
			{
				flag = true;
				printf("Time step %d: The robber has been at %d,%d.\n",t,c,r);
			}
		}
		if (!flag) printf("Nothing known.\n");
	}
	printf("\n");
}

int main()
{
	Case = 0;
	while (scanf("%d%d%d",&W,&H,&T))
	{
		if (T+W+H==0) return 0;
		++Case;
		init();
		input();
		solve();
	}
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值