POJ1071 Illusive Chase(DFS)
来源:互联网 发布:手机java 编辑:程序博客网 时间:2024/05/07 20:36
http://poj.org/problem?id=1071
llusive Chase
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1035 Accepted: 463
Description
Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audience of youngsters, placed around an m * n field. Tom which is turned off initially is placed in some arbitrary point in the field by a volunteer from the audience. At time zero of the show, Tom is turned on by a remote control. Poor Tom is shown a holographic illusion of Jerry in a short distance such that a direct path between them is either vertical or horizontal. There may be obstacles in the field, but the illusion is always placed such that in the direct path between Tom and the illusion, there would be no obstacles. Tom tries to reach Jerry, but as soon as he gets there, the illusion changes its place and the chase goes on. Let's call each chase in one direction (up, down, left, and right), a chase trip. Each trip starts from where the last illusion was deemed and ends where the next illusion is deemed out. After a number of chase trips, the holographic illusion no more shows up, and poor Tom wonders what to do next. At this time, he is signaled that for sure, if he returns to where he started the chase, a real Jerry is sleeping and he can catch it.
To simplify the problem, we can consider the field as a grid of squares. Some of the squares are occupied with obstacles. At any instant, Tom is in some unoccupied square of the grid and so is Jerry, such that the direct path between them is either horizontal or vertical. It's assumed that each time Tom is shown an illusion; he can reach it by moving only in one of the four directions, without bumping into an obstacle. Tom moves into an adjacent square of the grid by taking one and only one step.
The problem is that Tom's logging mechanism is a bit fuzzy, thus the number of steps he has taken in each chase trip is logged as an interval of integers, e.g. 2 to 5 steps to the left. Now is your turn to send a program to Tom's memory to help him go back. But to ease your task in this contest, your program should only count all possible places that he might have started the chase from.
To simplify the problem, we can consider the field as a grid of squares. Some of the squares are occupied with obstacles. At any instant, Tom is in some unoccupied square of the grid and so is Jerry, such that the direct path between them is either horizontal or vertical. It's assumed that each time Tom is shown an illusion; he can reach it by moving only in one of the four directions, without bumping into an obstacle. Tom moves into an adjacent square of the grid by taking one and only one step.
The problem is that Tom's logging mechanism is a bit fuzzy, thus the number of steps he has taken in each chase trip is logged as an interval of integers, e.g. 2 to 5 steps to the left. Now is your turn to send a program to Tom's memory to help him go back. But to ease your task in this contest, your program should only count all possible places that he might have started the chase from.
Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers m and n, which are the number of rows and columns of the grid respectively (1 <= m, n <= 100). Next, there are m lines, each containing n integers which are either 0 or 1, indicating whether the corresponding cell of the grid is empty (0) or occupied by an obstacle (1). After description of the field, there is a sequence of lines, each corresponding to a chase trip of Tom (in order). Each line contains two positive integers which together specify the range of steps Tom has taken (inclusive), followed by a single upper-case character indicating the direction of the chase trip, which is one of the four cases of R (for right), L (for left), U (for up), and D (for down). (Note that these directions are relative to the field and are not directions to which Tom turns). This part of the test case is terminated by a line containing exactly two zeros.
Output
For each test case, there should be a single line, containing an integer indicating the number of cells that Tom might have started the chase from.
Sample Input
26 60 0 0 0 0 00 0 0 1 1 00 1 0 0 0 00 0 0 1 0 00 0 0 1 0 10 0 0 0 0 11 2 R1 2 D1 1 R0 03 40 0 0 00 0 0 00 0 0 01 2 R3 7 U0 0
Sample Output
100
Source
Tehran 2001
给出MAP和指令,求出可能的起点的数量
每个点设为终点,从终点开始DFS,注意四个方向要反过来
一次就AC,好开心>_< ~~~
#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<queue>#include<vector>#define sqr(x) (x)*(x)#define INF 0x1f1f1f1f#define PI 3.1415926535#define mm using namespace std;int m,n,cnt,ans;int map[111][111],vis[111][111];int dir[11111][2];char d[11111];bool check(int x,int y){if (x>=0 && x<m && y>=0 && y<n && map[x][y]==0)return true;elsereturn false;}void dfs(int x,int y,int dd){bool go=true;if (dd<0 && !vis[x][y]){ans++;vis[x][y]=1;//printf("!%d %d MAP%d\n",x,y,map[x][y]);return ;}else{if (d[dd]=='U'){for (int i=1;i<dir[dd][0] && go ;i++){if (!check(x+i,y))go=false;}for (int i=dir[dd][0];i<=dir[dd][1] && go;i++){if (check(x+i,y)){//printf("(%d,%d)%dD ",x,y,dd);dfs(x+i,y,dd-1);}elsego=false;}}if (d[dd]=='D'){for (int i=1;i<dir[dd][0] && go ;i++){if (!check(x-i,y))go=false;}for (int i=dir[dd][0];i<=dir[dd][1] && go;i++){if (check(x-i,y)){//printf("(%d,%d)%dU ",x,y,dd);dfs(x-i,y,dd-1);}elsego=false;}}if (d[dd]=='L'){for (int i=1;i<dir[dd][0] && go ;i++){if (!check(x,y+i))go=false;}for (int i=dir[dd][0];i<=dir[dd][1] && go;i++){if (check(x,y+i)){//printf("(%d,%d)%dR ",x,y,dd);dfs(x,y+i,dd-1);}elsego=false;}}if (d[dd]=='R'){for (int i=1;i<dir[dd][0] && go ;i++){if (!check(x,y-i))go=false;}for (int i=dir[dd][0];i<=dir[dd][1] && go;i++){if (check(x,y-i)){//printf("(%d,%d)%dL ",x,y,dd);dfs(x,y-i,dd-1);}elsego=false;}}}}int main(){int tt;scanf("%d",&tt);while(tt--){scanf("%d%d",&m,&n);for (int i=0;i<m;i++){for (int j=0;j<n;j++){scanf("%d",&map[i][j]);}}cnt=0;while(scanf("%d%d",&dir[cnt][0],&dir[cnt][1]) && dir[cnt][1]){scanf(" %c",&d[cnt++]);}ans=0;memset(vis,0,sizeof(vis));for (int i=0;i<m;i++){for (int j=0;j<n;j++){if (map[i][j]==0 && vis[i][j]==0){dfs(i,j,cnt-1);}}}printf("%d\n",ans);}return 0;}
1 0
- POJ1071 Illusive Chase(DFS)
- poj1071 && zoj1019Illusive Chase(dfs)
- POJ 1071Illusive Chase(DFS)
- zoj 1019 Illusive Chase --- dfs
- hdu 1364 Illusive Chase (dfs)
- hdu Illusive Chase 1364 dfs
- POJ 1071 && ZOJ 1019 Illusive Chase (dfs)
- 【DFS】HDU 1364 && POJ 1071 Illusive Chase
- POJ 1071 & HDU 1364 & ZOJ 1019 Illusive Chase(DFS)
- zoj 1019 Illusive chase
- ZOJ1019 Illusive Chase
- poj 1071 Illusive Chase
- zoj 1019 Illusive Chase
- zoj 1019 Illusive Chase
- zoj 1019 Illusive Chase
- hdu 1364 Illusive Chase
- POJ 1071 Illusive Chase 笔记
- ZOJ 1019 Illusive Chase
- 世界500百强企业中国的CEO对我们的忠告!!!!!我们要告别稚气了
- 再认面向对象技术
- ubuntu下安装ssh
- 大众中央电器管理控制单元09的长编码解释
- FTP文件上传与下载
- POJ1071 Illusive Chase(DFS)
- Mono概述及部分源码解析
- 【Web】local scheme ---- file:///
- git branch -r view the newly created remote branch
- WinCE Remote Desktop Connection
- 25个必须记住的SSH命令
- python dictionary list
- 使用JAVA程序处理图片水印
- ios资料