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
3
0 4 3 1 3 2
0 0
 
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的更多相关文章

  1. TOJ 1191. The Worm Turns

    191.   The Worm Turns Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 5465   Accepted Run ...

  2. 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 ...

  3. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  4. ZOJ 1056 The Worm Turns

    原题链接 题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活. 解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标.每读入一条指令,判断其是否越界 ...

  5. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...

  6. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  7. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  8. 血族第四季/全集The Strain迅雷下载

    当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...

  9. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

随机推荐

  1. 如何安装和配置 Rex-Ray?- 每天5分钟玩转 Docker 容器技术(74)

    Rex-Ray 是一个优秀的 Docker volume driver,本节将演示其安装和配置方法. Rex-Ray 以 standalone 进程的方式运行在 Docker 主机上,安装方法很简单, ...

  2. Scaffolding Template on Asp.Net Core Razor Page

    Scaffolding Template Intro 我们知道在Asp.Net MVC中,如果你使用的EF的DBContext的话,你可以在vs中通过右键解决方案-添加控制器-添加包含视图的控制器,然 ...

  3. 替换应用程序DLL动态库的详细方法步骤 (gts.dll为例)

    在C++ builder编译器IDE软件下 1.View -Project Manageer --找到需要替换的x.dll(gts.dll)对应的x.lib(gts.lib),然后Remove2.Pr ...

  4. 前端基础之JavaScript

    什么是JavaScript? JavaScript,也称ECMAScript,是一种基于对象和事件驱动并具有相对安全性并广泛用于客户端网页开发的脚本语言,同时也是一种广泛用于客户端Web开发的脚本语言 ...

  5. ORACLE 本地冷迁移

    需求:把oracle数据库的数据文件,redo文件,控制文件迁移到本地的其它目录. 1.测试环境: 操作系统redhat 6.3,数据库oracle 11.2.0.1.0 [root@dbtest1 ...

  6. Java高级工程师进阶路线

    第一部分:宏观方面 一. JAVA.要想成为JAVA(高级)工程师肯定要学习JAVA.一般的程序员或许只需知道一些JAVA的语法结构就可以应付了.但要成为JAVA(高级) 工程师,您要对JAVA做比较 ...

  7. 学习札记 ----wind7下如何安装SqlServer数据库

    1.控制面板 ---找到程序和功能选项 如下图所示: 2.打开程序和功能后进入如下图所示的界面,点击打开或关闭window功能. 3.启动window7自带的IIS功能.如下图所示: 4.如上动作准备 ...

  8. 使用微软URLRewriter.dll的url实现任意后缀名重写

    <?xml version="1.0"?> <!--先引用URLRewriter.dll,放置于Bin目录--> <configuration> ...

  9. Kettle文本文件输出和输入控件使用中,换行符导致的问题处理

    1.如下图通过输入控件从数据库读取数据然后生成TXT文本文件,TXT文件生成原则是每一条数据生成一行数据,第二条数据换行保存 2.如下图所示,使用文本文件输入控件读入上图生成的文件,文件读入原则是按行 ...

  10. 阿里巴巴 Java 开发规约插件初体验

    阿里巴巴 Java 开发手册 又一次来谈<阿里巴巴 Java 开发手册>,经过这大半年的版本迭代,这本阿里工程师们总结出来避免写出那么多 Bug 的规范,对于 Java 开发者简直就是必备 ...