【DFS】 hdu2782 The Worm Turns

10 篇文章 0 订阅

The Worm Turns

http://acm.hdu.edu.cn/showproblem.php?pid=2782



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

题目给4s,暴力你懂的,不过细节一定要注意。


#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 630
char mat[MAX][MAX];
bool visit[MAX][MAX];
int dir[4][2]= {0,1,-1,0,1,0,0,-1};
int n,m,maxx;
int xi,yi,ki,xt,yt,kt;
void dfs(int a,int b,int d,int step)//深搜
{
    if(step>maxx)//更新最大值
    {
        maxx=step;
        xi=xt;
        yi=yt;
        ki=kt;
    }
    int tx=a+dir[d][0],ty=b+dir[d][1];
    if(tx<0||tx>=n||ty<0||ty>=m||visit[tx][ty]==true||mat[tx][ty])
    {
        if(step==0) return;//第一步是不允许转向的
        for(int i=0; i<4; ++i)
        {
            if(i==d) continue;
            tx=a+dir[i][0];
            ty=b+dir[i][1];
            if(0<=tx&&tx<n&&0<=ty&&ty<m&&visit[tx][ty]==false&&!mat[tx][ty])
            {
                visit[tx][ty]=true;
                dfs(tx,ty,i,step+1);
                visit[tx][ty]=false;
            }
        }
    }
    else
    {
        visit[tx][ty]=true;
        dfs(tx,ty,d,step+1);
        visit[tx][ty]=false;
    }

}
int main()
{
    int t,a,b;
    char c;
    for(int cas=1; ~scanf("%d%d",&n,&m); ++cas)
    {
        if(n+m==0) break;
        memset(mat,0,sizeof(mat));
        maxx=0;
        scanf("%d",&t);
        for(; t--;)
        {
            scanf("%d%d",&a,&b);
            mat[a][b]=1;
        }
        //枚举每个点
        for(int i=0; i<n; ++i)
            for(int j=0; j<m; ++j)
                for(int k=0; k<4; ++k)
                    if(!mat[i][j])
                    {
                        xt=i;
                        yt=j;
                        kt=k;
                        visit[i][j]=true;
                        dfs(i,j,k,0);
                        visit[i][j]=false;
                    }
        if(ki==0) c='E';
        if(ki==1) c='N';
        if(ki==2) c='S';
        if(ki==3) c='W';
        printf("Case %d: %d %d %d %c\n",cas,maxx+1,xi,yi,c);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值