poj3083 Children of the Candy Corn

这道题有深搜和广搜。深搜还有要求,靠左或靠右。下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向。向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标)。其他方向也可以这样确定。通过上一步方向可以确定下一步应该从哪个方向开始搜。比如说,是向北走的,就必须先搜西,西不可以走,再搜北,如果北还不可以走,再搜东,最后才是南。其他方向的情况也可以这样推出来。最后走到E点完成了。广搜就是最基础的广搜。这道题做了将近10个小时。中途曾几次准备放弃,但最后还是坚持做完了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;

char maze[40][40];
int n,m,ex,ey,sx,sy,flag,s,cx,cy,df;
int dlx[7]={0,1,0,-1,0,1,0};
int dly[7]={1,0,-1,0,1,0,-1};
int drx[7]={0,-1,0,1,0,-1,0};
int dry[7]={1,0,-1,0,1,0,-1};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int isin(int x,int y)
{
    return x>=0&&x<n&&y>=0&&y<m;
}
void lfs(int i,int j)
{
    int x,y,k;
    if(flag) return ;
    if(i==ex&&j==ey) {flag=1;}
    else
    for(k=df;k<7&&!flag;k++)
    {
        cx=dlx[k];
        cy=dly[k];
        x=i+dlx[k];
        y=j+dly[k];

        if(isin(x,y)&&maze[x][y]!=‘#‘){
            s++;
             if(cx==1) df=0;
            //往南的,先搜东
            else if(cx==-1) df=2;
            //往北的,先搜西
            else if(!cx){
            if(cy==1) df=3;
            //往东的,先搜北
            else df=1;
        }
            //printf("(%d %d) ",x,y);
            lfs(x,y);
        }
    }
}
void rfs(int i,int j)
{
    int x,y,k,d;
    if(flag) return ;
    if(i==ex&&j==ey) {flag=1;}
    else
    for(k=df;k<7&&!flag;k++)
    {
        cx=drx[k];
        cy=dry[k];
        x=i+drx[k];
        y=j+dry[k];

        if(isin(x,y)&&maze[x][y]!=‘#‘){
            s++;
             if(cx==1) df=2;
            //往南的,先搜西
            else if(cx==-1) df=0;
            //往北的,先搜东
            else if(!cx){
            if(cy==1) df=3;
            //往东的,先搜南
            else df=1;
        }
            //printf("(%d %d) ",x,y);
            rfs(x,y);
        }
    }
}
struct node
{
    int dis,row,col;
};
queue<node> q;
void  bfs()
{
    int x,y,k,d,i,j;
    node t;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        d=t.dis;
        i=t.row;
        j=t.col;
        for(k=0;k<4;k++)
        {
            x=i+dx[k];
            y=j+dy[k];
            if(maze[x][y]!=‘#‘&&isin(x,y)){
                if(x==ex&&y==ey){
                    s=d+1;
                    return ;
                }
                else{
                    t.dis=d+1;
                    t.row=x;
                    t.col=y;
                    q.push(t);
                    maze[x][y]=‘#‘;
                }
            }

        }
    }
}
int main()
{
    int ca,i,j;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d%d",&m,&n);
        getchar();
        for(i=0;i<n;i++) gets(maze[i]);
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(maze[i][j]==‘S‘){
                    sx=i;sy=j;
                    maze[sx][sy]=‘#‘;
                }
                else if(maze[i][j]==‘E‘){
                    ex=i;ey=j;
                    maze[ex][ey]=‘.‘;
                }
            }
        }
        flag=0;
        s=1;
        if(!sy) df=3;
        else if(!sx) df=0;
        else if(sy==m-1) df=1;
        else if(sx==n-1) df=2;
        lfs(sx,sy);
        if(!sy) df=3;
        else if(!sx) df=0;
        else if(sy==m-1) df=1;
        else if(sx==n-1) df=2;
        cout<<s<<‘ ‘;
        flag=0;
        s=1;
        rfs(sx,sy);
        cout<<s<<‘ ‘;
        while(!q.empty()) q.pop();
        node p;
        p.dis=1;
        p.row=sx;
        p.col=sy;
        q.push(p);
        s=1;
        bfs();
        cout<<s<<endl;
    }
    return 0;
}

  

时间: 2024-10-12 03:19:28

poj3083 Children of the Candy Corn的相关文章

POJ3083——Children of the Candy Corn

Children of the Candy Corn DescriptionThe cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find t

poj3083 Children of the Candy Corn BFS&amp;&amp;DFS

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Accepted: 4841 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombie

dfs/poj3083 Children of the Candy Corn

1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 5 using namespace std; 6 typedef pair<int,int>P; 7 const int dx[4]={1,0,-1,0}; 8 const int dy[4]={0,1,0,-1}; 9 const int INF=1e9; 10 int sx,sy,ex,ey,n,m; 11 char a[50][50];

POJ-3083 Children of the Candy Corn (BFS+DFS)

Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. One popular maze-

POJ 3083 Children of the Candy Corn

Children of the Candy Corn Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 308364-bit integer IO format: %lld      Java class name: Main The cornfield maze is a popular Halloween treat. Visitors are shown the

K - Children of the Candy Corn

K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze fa

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

【POJ 3083】Children of the Candy Corn

POJ[3083]Children of the Candy Corn Dfs+Bfs 分别求沿左墙到达E 沿右墙到达E 还有S到E的最短步数 前两个Dfs实现 最后一个Bfs 耐心写很容易A 主要注意方向问题 dir四个方向 上右下左 刚开始我分别用下标0 1 2 3代表 开dirx diry两个移动数组 假设前一状态朝向0(上) 沿左墙移动即为3 0 1 2(左上右下<顺时针>) 沿右墙即为1 0 3 2(右上左下<逆时针>) 同理其余方向很容易遍历 略自豪的是不断精简代码从6

POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)

题目链接:POJ 3083 Children of the Candy Corn [题意]给出一个迷宫,不超过40*40,'#'代表墙,'.'代表能走,'S'是起点,'E'是终点.分别求出从起点一直沿左走,一直沿右走,走到终点所需要的步数.以及走出迷宫的最小步数. [思路]首先最小步数很简单,一个普通BFS搞定,这道题重点是一直向左走和一直向右走的DFS的方向问题,方向还和游客当时朝向有关.开始一直认为是每次都向左(右)转,直到可以走,然后就一直不对,在google了之后才知道向左走要遵循左上右