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. 20170623_oracle基础知识_常见问题

    1 如何配置网络服务?两种连接 Oracle 工具? 1) 打开Net Manager 2) 选择服务命名,点击“+ ”号 3 ) 网络服务名:  remote协议:tcp/ip 主机名:ip地址 端 ...

  2. java 多线程——同步 学习笔记

      一.实例的同步方法 public synchronized void add(int value){ this.count += value; } Java 实例方法同步是同步在拥有该方法的对象上 ...

  3. IJ-java-com-util-common:

    ylbtech-IJ-java-com-util-common: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出 ...

  4. awk数据预处理

    { && $~/192.168/) host_name = $ ;i<NF;++i) { if($i~/192.168/) { split($i, a, "=" ...

  5. 在LNMP或Nginx上配置NameCheap免费SSL证书

  6. sql数据库CHECKDB时报x个分配错误和x个一致性错误

    --1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...

  7. PCB SVN 服务端VisualSVN Server与TortoiseSVN

    PCB 工程系统SVN源代码招病毒破坏以后,一周时间都没有源代码同步更新了,今天终于将SVN源代码数据恢复并重建SVN服务器,这里将SVN搭建安装过程整理如下 一.服务端SVN安装 1.下载地址:ht ...

  8. .net 必看书籍1

    我们2个网站运营群,有很多技术高手,同时也有大部分技术新人,如何从传统asp转到.net,从传统table转到div+css布局,从传统技术转到ajax,从小型程序转到高性能并发的大型程序,我花了2小 ...

  9. RocketMQ(1)--helloworld

    双Master方式: 服务器环境 序号 IP 角色 模式 1 192.168.32.135 nameServer1,brokerServer1  Master1 2 192.168.32.136 na ...

  10. [python] ThreadPoolExecutor线程池

    初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...