hdu 2782 dfs(限定)
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
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.
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.
#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(限定)的更多相关文章
- hdu 3500 DFS(限定)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 2782 The Worm Turns (DFS)
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
随机推荐
- tiny4412学习(一)之从零搭建linux系统(烧写uboot、内核进emmc+uboot启动内核)【转】
本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74080109 版权声明:本文为博主原创文章,转载请注明http://blog.c ...
- B1826 [JSOI2010]缓存交换 贪心+离散化+堆
这个题仔细一想可以直接贪心做,因为队列里下一个出现的早的一定最优.正确性显然.然后我只拿了50,我直接模拟另一个队列暴力修改最后一个点的nxt值,自然会T.但是其实不用修改,直接插入就行了前面的不影响 ...
- bzoj题目分类
转载于http://blog.csdn.net/creationaugust/article/details/513876231000:A+B 1001:平面图最小割,转对偶图最短路 1002:矩阵树 ...
- Web开发必须知道的知识点
Web前端必须知道 一.常用那几种浏览器测试.有哪些内核(Layout Engine) 1.浏览器:IE,Chrome,FireFox,Safari,Opera. 2.内核:Trident,Gecko ...
- 微信接口本地调试(IIS服务器)
1.下载ngrok,并注册获得token.官网下载地址:https://ngrok.com/ 如果你是在官网下载的,到后面映射域名的时候会要求购买他们的服务. 这里我们用一个国内免费的ngrok服务器 ...
- Eclipse中搭建Apache Tomcat7源码调试环境
第一步:获取Apache Tomcat7源码,读者可以从Apache 官方网站获取,官方下载地址: http://tomcat.apache.org/download-70.cgi 注意选择Sourc ...
- brew update失败提示:/System/Library/Frameworks/Ruby.framework/的解决方法
本文由@ray 出品,转载请注明出处. 文章链接:http://www.cnblogs.com/wolfray/p/8040701.html 想用brew安装wget,但是提示失败,然后想先 bre ...
- 安卓发送图片文字,java后台接收
安卓使用retrofit2 和rxjava2 url: @Multipart @POST(UrlTools.STORYUPLOAD) Observable<Result> saveRepo ...
- JS——标记
continue 语句(带有或不带标签引用)只能用在循环中.break 语句(不带标签引用),只能用在循环或 switch 中.通过标签引用,break 语句可用于跳出任何 JavaScript 代码 ...
- 开源业务规则引擎JBoss Drools
Drools 是什么? 规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规 ...