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
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[626][626];
int mark[626][626];//记录从某一点
int mark_d[626][626];
int t,x,y;
int vis[626][626];
int d;
char dirct[4]={‘E‘,‘N‘,‘S‘,‘W‘};
int dir[4][2]={0,1,-1,0,1,0,0,-1};
int step;
int sx,sy,rx,ry;
void dfs(int x,int y){
    // cout<<x<<" "<<y<<endl;
    for(int i=0;i<4;i++){
        int rx=x,ry=y;
        if(rx+dir[i][0]>=0&&rx+dir[i][0]<n&&ry+dir[i][1]>=0&&ry+dir[i][1]<m&&mapn[rx+dir[i][0]][ry+dir[i][1]]==0){//这步路是合理的
            if(x==sx&&y==sy) d=i;
            mapn[rx+dir[i][0]][ry+dir[i][1]]=++step;
            if(step>mark[sx][sy])
                mark[sx][sy]=step,
                mark_d[sx][sy]=d;
            rx+=dir[i][0];ry+=dir[i][1];
            while(rx+dir[i][0]>=0&&rx+dir[i][0]<n&&ry+dir[i][1]>=0&&ry+dir[i][1]<m&&mapn[rx+dir[i][0]][ry+dir[i][1]]==0){
                mapn[rx+dir[i][0]][ry+dir[i][1]]=++step;
                if(step>mark[sx][sy])
                    mark[sx][sy]=step,
                    mark_d[sx][sy]=d;
                rx+=dir[i][0];ry+=dir[i][1];
            }
            dfs(rx,ry);
            while(rx!=x||ry!=y){
                mapn[rx][ry]=0;
                step--;
                rx-=dir[i][0];ry-=dir[i][1];
            }
        }
    }
}
int ca=0;
void init(){
    memset(mark,0,sizeof mark);
    memset(mark_d,0,sizeof mark_d);
    memset(mapn,0,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=0;i<t;i++){
            scanf("%d%d",&x,&y);
            mapn[x][y]=-1;
        }

        // 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=0;i<n;i++){
            for(int j=0;j<=m;j++){
                if(mapn[i][j]==-1) continue;
                sx=i;sy=j;
                step=0;mapn[sx][sy]=-1;
                dfs(sx,sy);
                mapn[sx][sy]=0;
            }
        }
        // cout<<"ok"<<endl;
        int maxn=-1;
        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=0;i<n;i++)
            for(int j=0;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+1,resx,resy,dirct[resd]);
    }
    return 0;
}
时间: 2024-11-07 04:17:36

The Worm Turns的相关文章

HDU 2782 The Worm Turns (DFS)

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 f

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

50道hdu基础搜索总结(转)

Dfs: 大部分是直接递归枚举,即求满足约束条件下的解,虽不用剪枝,但也需要代码能力. 练习递归枚举的题目: 1241       Oil Deposits (dfs的连通块个数) 1016       Prime Ring Problem 1584       蜘蛛牌(简单dfs,简单的剪枝,还有人用DP做(???)) 1426       Sudoku Killer(练习递归的好题目 or Dancing links(???)) 2510       符号三角形(打表题,写写打表程序还是不错

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

【转载】夜深人静写算法(四)——差分约束

[转载]夜深人静写算法(四) - 差分约束  目录     一.引例       1.一类不等式组的解   二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列       5.Bellman-Ford       6.SPFA       7.Floyd-Warshall   三.差分约束        1.数形结合        2.三角不等式        3.解的存在性        4.最大值

搜索题推荐

(转自网络博客): POJ POJ 1376 – Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=1376 题意:略 解法:bfs,A*-. POJ 2688 – Cleaning Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=2688 题意:bfs后转换为tsp问题 解法:bfs+dp,bfs+dfs 相关:http://hi.baidu.com/zfy0701/blo