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 ...
随机推荐
- 前端系列——jquery前端国际化解决方案“填坑日记”
前言:最近,新的平台还没有开发完成,原来的老项目又提出了新的需求:系统国际化.如果是前后端完全分离的开发模式,要做国际化,真的太简单了,有现成的解决方案,基于Node构建的时下热门的任何一种技术选型都 ...
- shell脚本获取文件中key/value的小方法
方法有N种,awk.sad.grep.cut... 以上几种方式不写了,就写两个不太常用到的. 废话少说,直接上代码: cat a.txt aa.gif=aaaa.gif bb.gif=bbbb.gi ...
- xgboost安装指南(win10,win7 64位)
---恢复内容开始--- Win7 64位系统下安装XGBoost 1. 环境介绍 计算机系统:win7 64位 Xgboost版本:xgboost0.6 2. 依赖软件环境 1) python 64 ...
- unable to dequeue a cell with identifier MealTableViewCell
1 问题描述 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable ...
- Ngnix技术研究系列2-基于Redis实现动态路由
上篇博文我们写了个引子: Ngnix技术研究系列1-通过应用场景看Nginx的反向代理 发现了新大陆,OpenResty OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台 ...
- 项目管理软件之争,禅道和JIRA大对比
本文摘要: 一. 产品介绍 二. 界面设计 1. 界面颜色设计 2. 布局结构 三. 功能区别 四. 价格对比 五. 后期服务 六. 优缺点 七. 总结 说到项目管理软件,不得不提的是禅道和JIRA. ...
- 在CentOS 7 上安装docker
Docker CE Install yum-utils, which provides the yum-config-manager utility: $ sudo yum install -y yu ...
- asp.net提高程序性能的技巧(一)
[摘 要] 我只是提供我几个我认为有助于提高写高性能的asp.net应用程序的技巧,本文提到的提高asp.net性能的技巧只是一个起步,更多的信息请参考<Improving ASP.NET Pe ...
- LCT学习笔记
最近自学了一下LCT(Link-Cut-Tree),参考了Saramanda及Yang_Zhe等众多大神的论文博客,对LCT有了一个初步的认识,LCT是一种动态树,可以处理动态问题的算法.对于树分治中 ...
- Asp.Net MVC4 系列-- 进阶篇之路由(1)
创建一个路由 打开 RouteConfig.cs ,发现已经创建了一个默认路由 : routes.MapRoute( name:"Default", url:"{con ...

