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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std; int W,H,T,q,ans;
int Map[][][];// (x,y) 时间z
struct Node
{
int x,y;
Node(int a,int b):x(a),y(b)
{}
};
vector<Node> G[]; int dx[]={, ,,-,};
int dy[]={,-,, ,};
int dfs(int x,int y,int tt)
{ if (Map[x][y][tt]!=-)//如果这个状态的情况已经确定了
return Map[x][y][tt];
if (tt==T)//到最后封锁时间,说明这个状态可以
{
ans++;
Map[x][y][tt]=;
G[tt].push_back(Node(x,y));
return ;
}
Map[x][y][tt]=;
for (int i=;i<;i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if (tx>=&&tx<=W&&ty>=&&ty<=H)
{
if (dfs(tx,ty,tt+)==)//如果接下来的状态可以到,说明自己也可以到
Map[x][y][tt]=;
}
}
if (Map[x][y][tt]==)
{
G[tt].push_back(Node(x,y));
}
return Map[x][y][tt];
} int main()
{
int cas=;
while(scanf("%d%d%d",&W,&H,&T)&&W+H+T)
{
memset(Map,-,sizeof(Map));
scanf("%d",&q);
for (int i=;i<=q;i++)
{
int t,lx,ly,rx,ry;
scanf("%d%d%d%d%d",&t,&lx,&ly,&rx,&ry);
for (int j=lx;j<=rx;j++)
for (int k=ly;k<=ry;k++)
Map[j][k][t]=;
}
for (int i=;i<=T;i++) G[i].clear();
ans=;
for (int i=;i<=W;i++)
{
for (int j=;j<=H;j++)
if (Map[i][j][]==-)
{
dfs(i,j,);
}
}
printf("Robbery #%d:\n",cas++);
if (ans==)
printf("The robber has escaped.\n");
else
{
int res=;
for (int i=;i<=T;i++)
if (G[i].size()==)
{
res++;
printf("Time step %d: The robber has been at %d,%d.\n",i,G[i][].x,G[i][].y);
}
if (res==)
printf("Nothing known.\n");
}
printf("\n");
}
return ;
}
Robbery(记忆化搜索)的更多相关文章
- uva 707(记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- loj 1044(dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
- HDU1978 记忆化搜索
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- bzoj4562: [Haoi2016]食物链--记忆化搜索
这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...
随机推荐
- Makefile学习之显示命令与出错命令
显示命令: 1.在makefile中 如果在命令行下添加“@”符号,则只执行,不显示命令: 2.在执行make时,make -n 表示只显示命令而不执行: make -s 表示只执行命令而不显示: 3 ...
- 项目笔记:导出XML和导出全部XML功能
前台代码: //导出一条Xml function btn_createXml(){ var title =$("#editButton").attr("title&quo ...
- 机器学习第2课:单变量线性回归(Linear Regression with One Variable)
2.1 模型表示 之前的房屋交易问题为例,假使我们回归问题的训练集(Training Set)如下表所示: 我们将要用来描述这个回归问题的标记如下: m 代表训练集中实 ...
- JPEG编码(一)
JPEG编码介绍. 转自:http://blog.chinaunix.net/uid-20451980-id-1945156.html JPEG(Joint Photographic Experts ...
- kali渗透综合靶机(一)--Lazysysadmin靶机
kali渗透综合靶机(一)--Lazysysadmin靶机 Lazysysadmin靶机百度云下载链接:https://pan.baidu.com/s/1pTg38wf3oWQlKNUaT-s7qQ提 ...
- document.querySelector()与document.querySelectorAll()
document.querySelector()与document.querySelectorAll() CreationTime--2018年7月1日10点49分 Author:Marydon ...
- (九)Thymeleaf用法——Themeleaf注释
4. 注释 模板名称:comment.html 4.1 标准 HTML/XML注释 语法:<!-- --> 4.2 解析器级注释块(Parser-level ...
- kafka 0.8.1 新producer 源码简单分析
1 背景 最近由于项目需要,需要使用kafka的producer.但是对于c++,kafka官方并没有很好的支持. 在kafka官网上可以找到0.8.x的客户端.可以使用的客户端有C版本客户端,此客户 ...
- CoreImage的模糊滤镜
//1.原始图片 UIImage * image = [UIImage imageNamed:@"1.jpg"]; /****************core image***** ...
- 在Linux平台使用VNC连接树莓派
你的Linux发行版本号可能已经包括了Remote Desktop Viewer(远程桌面)程序.你能够通过这个程序使用VNC连接你的树莓派.通常能够在Applications/Internet菜单以 ...