The Worm Turns
The Worm Turns |
| Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
| Total Submission(s): 106 Accepted Submission(s): 54 |
|
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 |
|
Sample Output
Case 1: 22 0 3 W |
|
Source
2008 East Central Regional Contest
|
|
Recommend
lcy
|
/*
题意:给你一个n*m的矩阵,一个人在矩阵中走路,除非遇到走过的墙壁,或者走过的地方才会
转向,让输出走的最远的路的路程起点和初始转的方向 初步思路:先每个格试试爆搜,每次搜到的都记录一下 #RE:最多626个格,但不一定最大25*25
*/
#include<bits/stdc++.h>
using namespace std;
int n,m;
int mapn[][];
int mark[][];//记录从某一点
int mark_d[][];
int t,x,y;
int vis[][];
int d;
char dirct[]={'E','N','S','W'};
int dir[][]={,,-,,,,,-};
int step;
int sx,sy,rx,ry;
void dfs(int x,int y){
// cout<<x<<" "<<y<<endl;
for(int i=;i<;i++){
int rx=x,ry=y;
if(rx+dir[i][]>=&&rx+dir[i][]<n&&ry+dir[i][]>=&&ry+dir[i][]<m&&mapn[rx+dir[i][]][ry+dir[i][]]==){//这步路是合理的
if(x==sx&&y==sy) d=i;
mapn[rx+dir[i][]][ry+dir[i][]]=++step;
if(step>mark[sx][sy])
mark[sx][sy]=step,
mark_d[sx][sy]=d;
rx+=dir[i][];ry+=dir[i][];
while(rx+dir[i][]>=&&rx+dir[i][]<n&&ry+dir[i][]>=&&ry+dir[i][]<m&&mapn[rx+dir[i][]][ry+dir[i][]]==){
mapn[rx+dir[i][]][ry+dir[i][]]=++step;
if(step>mark[sx][sy])
mark[sx][sy]=step,
mark_d[sx][sy]=d;
rx+=dir[i][];ry+=dir[i][];
}
dfs(rx,ry);
while(rx!=x||ry!=y){
mapn[rx][ry]=;
step--;
rx-=dir[i][];ry-=dir[i][];
}
}
}
}
int ca=;
void init(){
memset(mark,,sizeof mark);
memset(mark_d,,sizeof mark_d);
memset(mapn,,sizeof mapn);
ca++;
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
init();
// cout<<n<<" "<<m<<endl;
scanf("%d",&t);
for(int i=;i<t;i++){
scanf("%d%d",&x,&y);
mapn[x][y]=-;
} // for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// cout<<mapn[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl; for(int i=;i<n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j]==-) continue;
sx=i;sy=j;
step=;mapn[sx][sy]=-;
dfs(sx,sy);
mapn[sx][sy]=;
}
}
// cout<<"ok"<<endl;
int maxn=-;
int resx,resy;
int resd; // for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// cout<<mark[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl; for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(mark[i][j]>maxn)
{
maxn=mark[i][j];
resx=i;resy=j;
resd=mark_d[i][j];
}
printf("Case %d: %d %d %d %c\n",ca,maxn+,resx,resy,dirct[resd]);
}
return ;
}
The Worm Turns的更多相关文章
- TOJ 1191. The Worm Turns
191. The Worm Turns Time Limit: 1.0 Seconds Memory Limit: 65536K Total Runs: 5465 Accepted Run ...
- TJU ACM-ICPC Online Judge—1191 The Worm Turns
B - The Worm Turns Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ...
- HDU 2782 The Worm Turns (DFS)
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...
- ZOJ 1056 The Worm Turns
原题链接 题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活. 解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标.每读入一条指令,判断其是否越界 ...
- 【HDOJ】2782 The Worm Turns
DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 血族第四季/全集The Strain迅雷下载
当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
随机推荐
- day20<IO流>
IO流(IO流概述及其分类) IO流(FileInputStream) IO流(read()方法返回值为什么是int) IO流(FileOutputStream) IO流(FileOutputStre ...
- MVC发布网站
首先Vs打开解决方案 在Global.asax中加入下列代码,否则会出现CSS JS失效 BundleTable.EnableOptimizations = false; 用户 'NT AUTHORI ...
- Hive基础(4)---Hive的内置服务
版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] 一:Hive的几种内置服务 ...
- 关于逆元的概念、用途和可行性的思考(附51nod 1013 和 51nod 1256)
[逆元的概念] 逆元和单位元这个概念在群中的解释是: 逆元是指数学领域群G中任意一个元素a,都在G中有唯一的逆元a',具有性质a×a'=a'×a=e,其中e为该群的单位元. 群的概念是: 如果独异 ...
- bzoj3156 防御准备 - 斜率优化
Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战线花费值. Sample Input 102 3 ...
- Easyui后台管理角色权限控制
最近需要做一个粗略的后台管理的权限,根据用户的等级来加载相应的菜单,控制到子菜单.使用的是Easyui这个框架. 1.我使用的mysql数据库.在这里我就建立四张表,角色表(tb_users),菜单表 ...
- javascript中的DOM介绍(一)
一.基础知识点 1.DOM是文档对象模型,是针对HTML和XML文档的一个API(应用程序接口) 2.DOM描绘了一个层次化的节点数,允许开发人员进行添加,移除个修改等操作 3.IE浏览器中所有的DO ...
- (转)TabIndex 属性
html中的tabIndex属性可以设置键盘中的TAB键在控件中的移动顺序,即焦点的顺序. 把控件的tabIndex属性设成1到32767的一个值,就可以把这个控件加入到TAB键的序列中. 这 ...
- Apache配置腾讯云SSL证书指引
一.安装Apache 1) 使用yum安装Apache # yum install httpd 2) 修改测试页面 # vim /var/www/html/index.heml PS:修改为测试内容, ...
- win10 uwp ContentDialog 点确定不关闭
微软的ContentDialog不是一直有,而是UWP新的,可以使用Content放用户控件,使用很好,但是一点不好的是,默认的一点击下面按钮就会退出. 我们有时候需要ContentDialog用户输 ...

