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 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.

 

 

//题意看了好久好久才明白,第一行,代表城市的长 W ,宽 H ,然后是 小偷逗留的时间 T

第二行一个整数 n 代表警察搜查的次数

然后 n 行,意思是,时间 t 时,搜查的矩形范围的左上角坐标,和右下角坐标,代表小偷在 t 时刻不在这个范围内

输出的是有关小偷的各个时刻在什么位置的线索,能确定就输出,不能确定就不要输出,没有任何线索输出 Nothing known

如果小偷不可能在城市,输出 The robber has escaped.

 

DFS搜索,难得是读题啊。。。虽然我看了题解,,,地图要用一个三维数组,不但要记录位置,还要记录时间,

还要记录这个时间,这个位置的可达状态,0表不可达,1表可达,-1代表不确定,然后应该没啥难的了。。。

10ms

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 using namespace std;
 6 
 7 int W,H,T,q,ans;
 8 int Map[102][102][102];// (x,y) 时间z
 9 struct Node
10 {
11     int x,y;
12     Node(int a,int b):x(a),y(b)
13     {}
14 };
15 vector<Node> G[102];
16 
17 int dx[]={0, 0,1,-1,0};
18 int dy[]={1,-1,0, 0,0};
19 int dfs(int x,int y,int tt)
20 {
21 
22     if (Map[x][y][tt]!=-1)//如果这个状态的情况已经确定了
23         return Map[x][y][tt];
24     if (tt==T)//到最后封锁时间,说明这个状态可以
25     {
26         ans++;
27         Map[x][y][tt]=1;
28         G[tt].push_back(Node(x,y));
29         return 1;
30     }
31     Map[x][y][tt]=0;
32     for (int i=0;i<5;i++)
33     {
34         int tx=x+dx[i];
35         int ty=y+dy[i];
36         if (tx>=1&&tx<=W&&ty>=1&&ty<=H)
37         {
38             if (dfs(tx,ty,tt+1)==1)//如果接下来的状态可以到,说明自己也可以到
39                 Map[x][y][tt]=1;
40         }
41     }
42     if (Map[x][y][tt]==1)
43     {
44         G[tt].push_back(Node(x,y));
45     }
46     return Map[x][y][tt];
47 }
48 
49 int main()
50 {
51     int cas=1;
52     while(scanf("%d%d%d",&W,&H,&T)&&W+H+T)
53     {
54         memset(Map,-1,sizeof(Map));
55         scanf("%d",&q);
56         for (int i=1;i<=q;i++)
57         {
58             int t,lx,ly,rx,ry;
59             scanf("%d%d%d%d%d",&t,&lx,&ly,&rx,&ry);
60             for (int j=lx;j<=rx;j++)
61                 for (int k=ly;k<=ry;k++)
62                     Map[j][k][t]=0;
63         }
64         for (int i=1;i<=T;i++) G[i].clear();
65         ans=0;
66         for (int i=1;i<=W;i++)
67         {
68             for (int j=1;j<=H;j++)
69                 if (Map[i][j][1]==-1)
70                 {
71                     dfs(i,j,1);
72                 }
73         }
74         printf("Robbery #%d:\n",cas++);
75         if (ans==0)
76             printf("The robber has escaped.\n");
77         else
78         {
79             int res=0;
80             for (int i=1;i<=T;i++)
81             if (G[i].size()==1)
82             {
83                 res++;
84                 printf("Time step %d: The robber has been at %d,%d.\n",i,G[i][0].x,G[i][0].y);
85             }
86             if (res==0)
87                 printf("Nothing known.\n");
88         }
89         printf("\n");
90     }
91     return 0;
92 }
View Code

 

posted @ 2016-12-13 21:59  happy_codes  阅读(295)  评论(0编辑  收藏  举报