zoukankan      html  css  js  c++  java
  • 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
    ",ca,maxn+1,resx,resy,dirct[resd]); 
        }
        return 0;
    }
  • 相关阅读:
    Ibatis入门基本语法(转) good
    zip文件压缩(转)
    联合创始人股权分配,五五分是最糟糕的做法(转)
    家长如何检查孩子的家庭作业
    oracle存储过程实例
    MachineKey
    写写我那天參加过的《文明之光》书友会
    各种加解密算法比較
    算法分析---查找最大回文子串
    随机数是骗人的,.Net、Java、C为我作证
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6412023.html
Copyright © 2011-2022 走看看