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. Java 数组的 12 个方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  2. 43. ExtJs控件属性配置详细

    转自:https://www.cnblogs.com/mannixiang/p/6558225.html 序言:    1.本文摘自网络,看控件命名像是4.0以前的版本,但控件属性配置仍然可以借鉴(不 ...

  3. vs2010打开vs2012项目

    修改.sln文件的前两行 修改前: Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 修 ...

  4. App设计师常用的10大网页和工具大盘点

    1.Adobe Photoshop 老牌的设计工具,不用解释 2.Adobe Illustrator 同上,不解释 3.Balsamiq Mockup 网址:http://balsamiq.com/ ...

  5. [App Store Connect帮助]一、 App Store Connect 使用入门(1)App Store Connect 工作流程

    您使用 App Store Connect 提交并管理您在 App Store 中销售的 App,使用 TestFlight 分发您 App 的 Beta 版本,接受法律协议,输入您的税务和银行业务信 ...

  6. [Apple开发者帐户帮助]八、管理档案(2)创建临时配置文件(iOS,tvOS,watchOS)

    创建临时配置文件以在设备上运行您的应用程序而无需Xcode.在开始之前,您需要一个App ID,一个分发证书和多个注册设备. 有关完整的临时配置文件工作流程,请转到Xcode帮助中的分发到已注册设备( ...

  7. [Swift通天遁地]二、表格表单-(3)在表格中嵌套另一个表格并使Cell的高度自适应

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. markdownpad2下载安装教程

    1.下载安装 http://markdownpad.com/download/markdownpad2-setup.exe 直接下载,安装过程中提醒要安装微软的一个什么环境,不用理会直接跳过,实测没有 ...

  9. 枚举详解之EnumSet、EnumMap用法

    枚举简单例子 /** * @author shuliangzhao * @Title: Color * @ProjectName design-parent * @Description: TODO ...

  10. build.gradle(Mdule.app)依赖库相关

    dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompati ...