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 ...
随机推荐
- 简易RPC框架-私有协议栈
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- GCD之并行串行区别
1.用户自定义线程队列,创建时很容易创建 注意创建时的第一个参数:标记值,方便调试查看 1 2 dispatch_queue_t serialqueue=dispatch_queue_create(& ...
- window、linux系统与linux服务器之间使用svn同步及自动部署代码的方法
摘要: 在家用PC,在公司用办公电脑对一个项目的代码进行修改时,会遇到代码同步的问题.本文讲解了代码同步及自动部署的解决办法. 实现方法: 1.首先在linux服务器上和linux上安装svn(sud ...
- 如何从两个List中筛选出相同的值
问题 现有社保卡和身份证若干,想要匹配筛选出一一对应的社保卡和身份证. 转换为List socialList,和List idList,从二者中找出匹配的社保卡. 模型 创建社保卡类 /** * @a ...
- 第6章 Overlapped I/O, 在你身后变戏法 ---被激发的 File Handles -3
最简单的 overlapped I/O 类型,是使用它自己的文件 handle 作为同步机制.首先你以 FILE_FLAG_OVERLAPPED 告诉 Win32 说你不要使用默认的同步 I/O.然后 ...
- spring-mvc List及数组的配置接收
数组接收 前台传递数组id 后台接收方式: public WebReturnObject deleteBatch(@RequestParam("id[]") String[] id ...
- GitHub Desktop客户端打开文件乱码问题解决方案
今天在使用GitHub Desktop客户端的时候,发添加本地仓库后文件内容显示为乱码. 1.现象 如下图所示: 2.原因分析 后来分析得知原来是由于编码不统一造成 的. 具体来说,我在window ...
- shim 和 polyfill
在前端,有两个词经常被提及:shim 和 polyfill.最近在翻译文章时又遇到了 polyfill 这个词,准备把这两个概念理清楚. 关于 JavaScript 的兼容性问题,通常有不同的解决方案 ...
- Chinese Rings hdu 2842 矩阵快速幂
Chinese Rings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- eclipse的插件开发-启动时间
今天晚上看<深入理解java虚拟机>时,作者在书中有一段,eclipse优化的章节,其中涉及到了eclipse启动时间检测的插件开发 于是翻了翻资料,也开发了一个自己的插件 如图是开发后启 ...

