The Worm Turns

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 851    Accepted Submission(s): 318

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

 
Input
Input 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.
 
Output
For 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
 
一般来说能用dfs搜索的都能用bfs,然而有些题并不是那么好用bfs实现的,如同这道题。。。
开始的方向限定了,还有可以一直向同一个方向走的情况。同时可以利用dfs的回退功能,使得每次的搜索之前无需对标记进行一次清空
 
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
short mat[][];
int n,m,ans,ansk,ansx,ansy;
short book[][];
char direc[]= {'E','N','S','W'};
int nx[]= {, -, , };
int ny[]= {, , , -};
int w,wx,wy;
void dfs(int x,int y,int k,int s)
{
if(s>ans)
{
ans=s;
ansk=w;
ansx=wx,ansy=wy;
}
int tx=x+nx[k],ty=y+ny[k];
if(tx>=&&tx<n&&ty>=&&ty<m&&!book[tx][ty]&&!mat[tx][ty])
{
book[tx][ty]=;
dfs(tx,ty,k,s+);
book[tx][ty]=;
}
else
{
if(s==) return; //一开始的方向是限定的,不能改变,
for(int i=; i<; i++) //同时这也是dfs不超时的一个重要条件。
{
if(i==k) continue;
tx=x+nx[i];
ty=y+ny[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&!book[tx][ty]&&!mat[tx][ty])
{
book[tx][ty]=;
dfs(tx,ty,i,s+);
book[tx][ty]=;
}
}
}
} int main()
{
int k,x,y,ca=;
while(scanf("%d%d",&n,&m),n+m!=)
{
memset(mat,,sizeof(mat));
memset(book,,sizeof(book));
scanf("%d",&k);
for(int i=; i<k; i++)
{
scanf("%d%d",&x,&y);
mat[x][y]=;
}
ans=;
ansk=;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(mat[i][j]) continue;
for(int k=; k<; k++)
{
book[i][j]=;
w=k;wx=i;wy=j;
dfs(i,j,k,);
book[i][j]=;
}
}
}
printf("Case %d: %d %d %d %c\n",ca++,ans+,ansx,ansy,direc[ansk]);
}
return ;
}

hdu 2782 dfs(限定)的更多相关文章

  1. hdu 3500 DFS(限定)

    Fling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submi ...

  2. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  3. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  4. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  5. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  6. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  7. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  8. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  9. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

随机推荐

  1. js添加页面元素

    js动态创建html元素需要使用到下面这些常见的js方法. getElementById();//返回带有指定 ID 的元素. getElementsByTagName();//返回包含带有指定标签名 ...

  2. ios9--UIImageView的帧动画

    // // ViewController.m // 05-UIImageView的帧动画 // #import "ViewController.h" @interface View ...

  3. Codesys——PLCopen基本运动控制功能块的使用方法总结

    MC_Halt 在MC_MoveVelocity模式下,用MC_Halt停止其轴,当前轴的状态由 ContinuousMotion(当前转速)--->DiscreteMotion(速度不为0)- ...

  4. C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)

    一.绘制虚线的方法 GDI绘制,使用的是System.Drawing.Pen Pen pen = new Pen(Color.Red, 1);            pen.DashStyle = S ...

  5. E20171014-hm

    Sibling   n. 兄弟,姐妹; [生] 同科,同属; [人] 氏族成员;

  6. bzoj1433[ZJOI2009]假期的宿舍(匈牙利)

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2544  Solved: 1074 [Submit][St ...

  7. Java注解Annotation的用法 - 自定义Annotation实现

    Java注解又称Java标注,是Java语言5.0版本开始支持加入源代码的特殊语法元数据. Java语言中的类.方法.变量.参数和包等都可以被标注.和Javadoc不同,Java标注可以通过反射获取标 ...

  8. 【NOIP练习赛】开车

    [NOIP练习赛]T2.开车 Description 老司机小 Q 要在一个十字路口指挥车队开车,这个十字路口可 以描述为一个 n*n 的矩阵,其中第 2 行到第 n-1 行都各有一道横向车 道,第 ...

  9. Android源码下载方法

    1. 下载 repo 工具 mkdir ~/bin PATH=~/bin:$PATH curl https://storage.googleapis.com/git-repo-downloads/re ...

  10. 这辈子写过的比较有意思的几个sql

    递归 with myRecursion as( select * from recursion where id=1 union all select r.* from myRecursion m,r ...