uva707 - Robbery 记忆化搜索

Robbery 

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 file contains the description of several robberies. The first line of each description consists of three numbers WHt ( $1 \le W, H, t \le 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 \le n \le 100$), the number of messages the inspector received. The next n lines (one for each of the messages) consist of five integers tiLiTiRiBi each. The integer tiis the time at which the observation has been made ( $1 \le t_i \le t$), and LiTiRiBi are the left, top, right and bottom respectively of the (rectangular) area which has been observed. ($1 \le L_i \le R_i \le W, 1 \le T_i \le B_i \le 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 $\tau$: 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 $\tau$.) Output these lines ordered by time $\tau$.

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.

  W*H的格子,总共T时间,给你N个信息,每个信息告诉你左上角和右下角坐标围成的区域在某一时间没有抢劫犯。如果抢劫犯逃走了输出The robber has escaped.,否则输出能确定抢劫犯位置的时间和坐标,如果一个时间都不能确定输出Nothing known.。

  记忆化搜索,dp[t][x][y]表示时间t坐标x,y能否确定小偷存在,0表示不存在,1表示不确定。最后搜完以后对于某个t,如果只有一个1,那确定肯定在那一点。关键是搜索,最开始把不能的地方dp都置0,只要搜DFS(1,i,j)就行了,因为有些点根本就不可达。还有一点是他可以不动,写四个方向的写惯了,还把这里漏了=。=

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
using namespace std;

typedef pair<int,int> pii;

const int MAXN=110;
const int MAXM=MAXN*MAXN/2;
const int MAXNODE=32;
const int LOGMAXN=50;
const int INF=0x3f3f3f3f;

int move[5][2]={0,0,-1,0,0,-1,0,1,1,0};
int W,H,T,N;
int dp[MAXN][MAXN][MAXN],no[MAXN][MAXN][MAXN];

int DFS(int t,int x,int y){
    int& ans=dp[t][x][y];
    if(ans!=-1) return ans;
    if(t>=T) return ans=1;
    ans=0;
    for(int i=0;i<5;i++){
        int X=x+move[i][0],Y=y+move[i][1];
        if(X>0&&X<=W&&Y>0&&Y<=H) if(DFS(t+1,X,Y)){
            ans=1;
        }
    }
    return ans;
}

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d%d%d",&W,&H,&T)!=EOF&&(W||H||T)){
        scanf("%d",&N);
        int t,x1,y1,x2,y2;
        memset(dp,-1,sizeof(dp));
        for(int i=0;i<N;i++){
            scanf("%d%d%d%d%d",&t,&x1,&y1,&x2,&y2);
            for(int j=x1;j<=x2;j++)
                for(int k=y1;k<=y2;k++) dp[t][j][k]=0;
        }
        for(int i=1;i<=W;i++)
            for(int j=1;j<=H;j++) DFS(1,i,j);
        printf("Robbery #%d:\n",++cas);
        int flag=0;
        for(int i=1;i<=T;i++){
            int cnt=0,x,y;
            for(int j=1;j<=W;j++)
                for(int k=1;k<=H;k++) if(dp[i][j][k]==1){
                    cnt++;
                    x=j;
                    y=k;
                }
            if(cnt==0){
                flag=2;
                break;
            }
            if(cnt==1){
                flag=1;
                printf("Time step %d: The robber has been at %d,%d.\n",i,x,y);
            }
        }
        if(!flag) printf("Nothing known.\n");
        else if(flag==2) printf("The robber has escaped.\n");
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值