Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divided into cells, and each cell contains either food or a rock. Winston wanders aimlessly for a while until he gets hungry; then he immediately eats the food in his cell, chooses one of the four directions (north, south, east, or west) and crawls in a straight line for as long as he can see food in the cell in front of him. If he sees a rock directly ahead of him, or sees a cell where he has already eaten the food, or sees an edge of the rectangular patch, he turns left or right and once again travels as far as he can in a straight line, eating food. He never revisits a cell. After some time he reaches a point where he can go no further so Winston stops, burps and takes a nap.

For instance, suppose Winston wakes up in the following patch of earth (X's represent stones, all other cells contain food):

If Winston starts eating in row 0, column 3, he might pursue the following path (numbers represent order of visitation):

In this case, he chose his path very wisely: every piece of food
got eaten. Your task is to help Winston determine where he should begin
eating so that his path will visit as many food cells as possible.

InputInput will consist of multiple test cases.
Each test case begins with two positive integers, m and n , defining the
number of rows and columns of the patch of earth. Rows and columns are
numbered starting at 0, as in the figures above. Following these is a
non-negative integer r indicating the number of rocks, followed by a
list of 2r integers denoting the row and column number of each rock. The
last test case is followed by a pair of zeros. This should not be
processed. The value m×n will not exceed 625.OutputFor each test case, print the test case number (beginning with 1), followed by four values:

amount row column direction

where amount is the maximum number of pieces of food that Winston is
able to eat, (row, column) is the starting location of a path that
enables Winston to consume this much food, and direction is one of E, N,
S, W, indicating the initial direction in which Winston starts to move
along this path. If there is more than one starting location, choose the
one that is lexicographically least in terms of row and column numbers.
If there are optimal paths with the same starting location and
different starting directions, choose the first valid one in the list E,
N, S, W. Assume there is always at least one piece of food adjacent to
Winston's initial position.Sample Input

5 5
3
0 4 3 1 3 2
0 0

Sample Output

Case 1: 22 0 3 W
分析
题目大意就是给你一个地图,地图中只有两种元素,墙跟平地,然后要你求出一个人能在这个地图中走的最大距离。这个人一旦开始走路,那么他走的方向将是不变的。除非遇到墙,或者遇到地图的边,亦或者那个格子已经走过了,这个时候这个人才开始更换方向。
然后要你求出这个人能走的最大距离的起始点的位置,输出最大距离,起始点坐标,还有一开始走的方向。(E,N,S,W),如果最大距离相同,那么要输出起始点坐标的字典序最小的那个。
还有,如果四个方向都能达到最大,那么选择的方向的优先级由(E,N,S,W)往下排列。
就是爆搜,题目给的时间很大。我们可以枚举起点,对于每一个点搜索。关于输出,在搜索时按(E,N,S,W)的顺序,在更新答案时,只有当前答案大于记录答案才更新。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstring>
#include <climits>
#define MAXN 626
#define X rx+dx[i]
#define Y ry+dy[i]
using namespace std;
int t=;
int r,m,n,g[MAXN][MAXN],ans[MAXN][MAXN],sx,sy,step,ansdd,ansx,ansy,md,maxx;
int ansd[MAXN][MAXN];
int dx[]={,,-,,},
dy[]={,,,,-};
void dfs(int x,int y)
{
for(int i=;i<=;i++)
{
int rx=x,ry=y;
if(X>=&&X<n&&Y>=&&Y<m&&g[X][Y]==)
{
if(x==sx&&y==sy) md=i;
g[X][Y]=++step;
if(step>ans[sx][sy])
ans[sx][sy]=step,
ansd[sx][sy]=md;
rx+=dx[i];ry+=dy[i];
while(X>=&&X<n&&Y>=&&Y<m&&g[X][Y]==)
{
g[X][Y]=++step;
if(step>ans[sx][sy])
ans[sx][sy]=step,
ansd[sx][sy]=md;
rx+=dx[i];ry+=dy[i];
}
dfs(rx,ry);
while(rx!=x||ry!=y)
{
g[rx][ry]=;
step--;
rx-=dx[i];ry-=dy[i];
}
}
}
}
void init()
{
memset(g,,sizeof(g));
memset(ans,,sizeof(ans));
memset(ansd,,sizeof(ansd));
ansdd=ansx=ansy=md=maxx=;
}
int main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
while()
{
init();
++t;
scanf("%d%d",&n,&m);
if(n==&&m==)return ;
scanf("%d",&r);
for(int i=;i<=r;i++)
{
int x,y;
scanf("%d%d",&x,&y);
g[x][y]=-;
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(g[i][j]!=-)
{
step=;sx=i;sy=j;
g[sx][sy]=-;
dfs(sx,sy);
g[sx][sy]=;
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(ans[i][j]>maxx)
{
maxx=ans[i][j];
ansx=i;ansy=j;
ansdd=ansd[i][j];
}
printf("Case %d: %d %d %d ",t,maxx+,ansx,ansy);
if(ansdd==) printf("E\n");
else if(ansdd==) printf("N\n");
else if(ansdd==) printf("S\n");
else if(ansdd==) printf("W\n");
}
return ;
}

HDU 2782 The Worm Turns (DFS)的更多相关文章

  1. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...

  2. hdu 2782 dfs(限定)

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. The Worm Turns

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. TOJ 1191. The Worm Turns

    191.   The Worm Turns Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 5465   Accepted Run ...

  5. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  6. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  7. TJU ACM-ICPC Online Judge—1191 The Worm Turns

    B - The Worm Turns Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Su ...

  8. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  9. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

随机推荐

  1. Vue系列(二):发送Ajax、JSONP请求、Vue生命周期及实例属性和方法、自定义指令与过渡

    上一篇:Vue系列(一):简介.起步.常用指令.事件和属性.模板.过滤器 一. 发送AJAX请求 1. 简介 vue本身不支持发送AJAX请求,需要使用vue-resource.axios等插件实现 ...

  2. (转)彻底学会使用epoll(一)——ET模式实现分析

    注:之前写过两篇关于epoll实现的文章,但是感觉懂得了实现原理并不一定会使用,所以又决定写这一系列文章,希望能够对epoll有比较清楚的认识.是请大家转载务必注明出处,算是对我劳动成果的一点点尊重吧 ...

  3. 菜鸟学Struts——I18N对国际化的支持

    大家肯定都喜欢玩游戏吧. 对于是一个游戏迷的话,肯定玩过不少很棒的经典单机游戏.比方说,国产的<古墓丽影>.<刺客信条>.<鬼泣>国产的仙剑.古剑等.在众多游戏系列 ...

  4. map和multimap映射容器

    map容器 map所处理的数据与数据库表具有键值的记录非常相似,在键值与映射数据之间,建立一个数学上的映射关系.map容器的数据结构仍然採用红黑树进行管理.插入的元素键值不同意反复,所使用的结点元素的 ...

  5. cocos2d-x3.2 下使用多线程

    事实上在cocos2dx下使用多线程事实上就是用C++去写,这里提供几个简单的样例: 原文地址:http://blog.csdn.net/qqmcy/article/details/36227377 ...

  6. PCA(Principal Components Analysis)主成分分析

    全是图片..新手伤不起.word弄的,结果csdn传不了..以后改. .

  7. OC 自己定义 setDateFormat 显示格式

    -(NSString *)getStringFromDate:(NSDate *)aDate { NSDateFormatter *dateFormater=[[NSDateFormatter all ...

  8. bzoj5105: [CodePlus2017]晨跑(LCM)

    5105: [CodePlus2017]晨跑 题目:传送门 题解: 没有很懂Code Puls 的操作...一道签到的三个数的LCM??? 代码: #include<cstdio> #in ...

  9. Pocket英语语法---四、should的同义词是谁

    Pocket英语语法---四.should的同义词是谁 一.总结 一句话总结:should表示劝告,建议,命令,其同义词是ought to,should强调主观看法,ought to强调客观要求.在疑 ...

  10. ThinkPHP新建控制器

    ThinkPHP新建控制器 一.效果图 二.步骤 1.新建控制器文件 2.编写控制器文件 3.访问控制器 三.注意事项