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. 【转】android-修改TextView中部分文字的颜色

    textView = (TextView) findViewById(R.id.textview); SpannableStringBuilder builder = new SpannableStr ...

  2. 超级有用的git reset --hard和git revert命令

    很多时候,git新手容易误操作,比如,在levelIISZ-1.4.dev分支下,运行了git pull idc cpp-1.0的结果,这样做麻烦很大,经常导致maven项目格式不正确,这个时候,可以 ...

  3. [Swift通天遁地]七、数据与安全-(19)使用Swift实现原生的SHA1加密

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

  4. [Swift通天遁地]八、媒体与动画-(6)使用开源类库快速实现滑入动画

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

  5. ckeditor 工具栏的配置

    config.toolbar =    [       ['Undo','Redo'],            ['Font','FontSize'],            ['Bold','Ita ...

  6. yield from (python生成器)

    #生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in 'AB': # yield i yield from 'AB' # 就相当于上面的f ...

  7. 关于将电脑背景+chrome等网页改成护眼豆沙绿

    常用电脑的人都知道,白色等其他对比度大的颜色对眼伤害大,所以需换成柔和的豆沙绿,可长时间保证眼睛的不疲劳 windows浏览器: >>>>在桌面点右键,依次选属性(proper ...

  8. 【转】Linux下使用locale命令设置语言环境

    转自:http://www.cnblogs.com/dolphi/p/3622570.html locale命令设置语言环境 在Linux中通过locale来设置程序运行的不同语言环境,locale由 ...

  9. DAO模式详解

    DAO模式 数据访问层(DAO): 数据的增.删.改.查操作: 业务逻辑层(service): 业务来往的操作,需要调用数据访问层则调用数据访问层,传递数据: 表现层(UI): 呈现数据,用户交互. ...

  10. SQL Server应用模式之OLTP系统性能分析

    OLTP系统的最大特点,是这类应用里有大量的,并发程度比较高的小事务,包括SELECT.INSERT.UPDATE和DELETE. 这些操作都比较简单,事务时间也不会很长,但是要求的返回时间很严格,基 ...