POJ 3083 Children of the Candy Corn

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7104 Accepted: 3108

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-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)  

As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.  

Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').  

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

Source

对于这个题,昨天晚上静下心来好好想想发现对于求最短的距离bfs就可以,这是很简单的,但是怎么才能绕墙呢,看了看discuss大部分都是用dfs,我想了想不太会,然后就在那画图,发现这个题其实不用dfs就可以,绕墙到目的地,只会有一条路径,只需要记录刚走过的两个点,判断走的方向 ,对于每种走的方向考虑的情况都是一样的,以沿着左边的墙向上走为例,主要有以下几种情况
1 左边不是墙了,这个时候就需要向左走一个。
2 左边是墙,这个时候需要向前走,这个时候两种情况 (1):前方不是墙直接向前走就可以了
                                                                                        (2):前方是墙,这个时候就要右拐,也分两种情况:
                                                                                                                  《1》 右拐不是墙,直接走可以了。
                                                                                                                   《2》 右拐是墙,这个时候就要退回去了
靠着左边的墙走,会有四种方向,这四种方向考虑的情况都是上面那样,对于向左拐还是右拐,可以开数组,存储一下方向,到时候直接用就行。
这段代码是我刚写出的代码,发现向左和向右处理的都差不多,就是换了换数组,就做了简化处理。两段代码都贴一下吧
#include <iostream>
#include <string.h>
using namespace std;
int vex1[]={0,0,-1,1},vey1[]={-1,1,0,0};
int vex2[]={0,0,1,-1},vey2[]={1,-1,0,0};
int vectorx[]={-1,1,0,0},vectory[]={0,0,1,-1};
int status[50][50],a[50][50],res1,res2,res3,pos_stax,pos_stay,pos_endx,pos_endy;
int n,m;
int main()
{
    void deal_left();
    void deal_right();
    void deal();
    int i,j,s,t;
    char c;
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>c;
                if(c=='#')
                {
                    a[i][j]=0;
                }else if(c=='S')
                {
                    a[i][j]=0;
                    pos_stax=i; pos_stay=j;
                }else if(c=='E')
                {
                    a[i][j]=1;
                    pos_endx=i; pos_endy=j;
                }else if(c=='.')
                {
                    a[i][j]=1;
                }
            }
        }
        deal_left();
        deal_right();
        deal();
        cout<<res1<<" "<<res2<<" "<<res3<<endl;
    }
    return 0;
}
void deal_left()
{
    int i,j,x1,y1,x2,y2,x3,y3,s,x4,y4,t,temp1,temp2;
    s=2; x1=pos_stax; y1=pos_stay;
    for(i=0;i<=3;i++)
    {
        x2=x1+vex1[i]; y2=y1+vey1[i];
        if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2])
        {
            break;
        }
    }
    while(1)
    {
        x4=x2-x1; y4=y2-y1;
        if(x4<0&&y4==0)
        {
            t=0;
        }else if(x4>0&&y4==0)
        {
            t=1;
        }else if(x4==0&&y4>0)
        {
            t=2;
        }else if(x4==0&&y4<0)
        {
            t=3;
        }
        if(a[x2+vex1[t]][y2+vey1[t]])
        {
            x1=x2; y1=y2;
            x2=x2+vex1[t]; y2=y2+vey1[t];
            s+=1;
        }else
        {
            x3=x2+vectorx[t]; y3=y2+vectory[t];
            if(a[x3][y3])
            {
                x1=x2; y1=y2;
                x2=x3; y2=y3;
            }else
            {
                x3=x2+vex2[t]; y3=y2+vey2[t];
                if(a[x3][y3])
                {
                    x1=x2; y1=y2;
                    x2=x3; y2=y3;
                }else
                {
                    temp1=x1; temp2=y1;
                    x1=x2;    y1=y2;
                    x2=temp1; y2=temp2;
                }
            }
            s++;
        }
        if(x2==pos_endx&&y2==pos_endy)
        {
            res1=s;
            break;
        }
    }
}
void deal_right()
{
    int i,j,x1,y1,x2,y2,x3,y3,s,x4,y4,t,temp1,temp2;
    int sum=0;
    s=2; x1=pos_stax; y1=pos_stay;
    for(i=0;i<=3;i++)
    {
        x2=x1+vex1[i]; y2=y1+vey1[i];
        if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2])
        {
            break;
        }
    }
    while(1)
    {
        x4=x2-x1; y4=y2-y1;
        if(x4<0&&y4==0)
        {
            t=0;
        }else if(x4>0&&y4==0)
        {
            t=1;
        }else if(x4==0&&y4>0)
        {
            t=2;
        }else if(x4==0&&y4<0)
        {
            t=3;
        }
        if(a[x2+vex2[t]][y2+vey2[t]])
        {
            x1=x2; y1=y2;
            x2=x2+vex2[t]; y2=y2+vey2[t];
            s+=1;
        }else
        {
            x3=x2+vectorx[t]; y3=y2+vectory[t];
            if(a[x3][y3])
            {
                x1=x2; y1=y2;
                x2=x3; y2=y3;
            }else
            {
                x3=x2+vex1[t]; y3=y2+vey1[t];
                if(a[x3][y3])
                {
                    x1=x2; y1=y2;
                    x2=x3; y2=y3;
                }else
                {
                    temp1=x1; temp2=y1;
                    x1=x2;    y1=y2;
                    x2=temp1; y2=temp2;
                }
            }
            s++;
        }
        if(x2==pos_endx&&y2==pos_endy)
        {
            res2=s;
            break;
        }
    }
}
void deal()
{
    int i,j,base,top,x,y,xend,yend;
    int queue[100000][2];
    int sum[50][50];
    memset(status,0,sizeof(status));
    memset(sum,0,sizeof(sum));
    base=top=0;
    queue[top][0]=pos_stax; queue[top++][1]=pos_stay;
    sum[pos_stax][pos_stay]=1; status[pos_stax][pos_stay]=1;
    while(base<top)
    {
        x=queue[base][0]; y=queue[base++][1];
        for(i=0;i<=3;i++)
        {
            xend=x+vex1[i]; yend=y+vey1[i];
            if(xend>=1&&xend<=n&¥d>=1&¥d<=m&&!status[xend][yend]&&a[xend][yend])
            {
                sum[xend][yend]=sum[x][y]+1;
                status[xend][yend]=1;
                queue[top][0]=xend; queue[top++][1]=yend;
                if(xend==pos_endx&¥d==pos_endy)
                {
                    break;
                }
            }
        }
        if(i!=4)
        {
            break;
        }
    }
    res3=sum[pos_endx][pos_endy];
}

左右方向放在一个函数中处理。
#include <iostream>
#include <string>
using namespace std;
int bx1[]={0,0,-1,1},by1[]={-1,1,0,0};
int bx2[]={0,0,1,-1},by2[]={1,-1,0,0};
int vectorx[]={-1,1,0,0},vectory[]={0,0,1,-1};
int status[50][50],a[50][50],res1,res2,res3,pos_stax,pos_stay,pos_endx,pos_endy;
int n,m;
int main()
{
    void deal_left_Or_right(int vex1[5],int vey1[5],int vex2[5],int vey2[5],int &k);
    void deal();
    int i,j,s,t;
    char c;
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>c;
                if(c=='#')
                {
                    a[i][j]=0;
                }else if(c=='S')
                {
                    a[i][j]=0;
                    pos_stax=i; pos_stay=j;
                }else if(c=='E')
                {
                    a[i][j]=1;
                    pos_endx=i; pos_endy=j;
                }else if(c=='.')
                {
                    a[i][j]=1;
                }
            }
        }
        deal_left_Or_right(bx1,by1,bx2,by2,res1);
        deal_left_Or_right(bx2,by2,bx1,by1,res2);
        deal();
        cout<<res1<<" "<<res2<<" "<<res3<<endl;
    }
    return 0;
}
void deal_left_Or_right(int vex1[5],int vey1[5],int vex2[5],int vey2[5],int &k)
{
    int i,j,x1,y1,x2,y2,x3,y3,s,x4,y4,t,temp1,temp2;
    s=2; x1=pos_stax; y1=pos_stay;
    for(i=0;i<=3;i++)
    {
        x2=x1+vex1[i]; y2=y1+vey1[i];
        if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2])
        {
            break;
        }
    }
    while(1)
    {
        x4=x2-x1; y4=y2-y1;
        if(x4<0&&y4==0)
        {
            t=0;
        }else if(x4>0&&y4==0)
        {
            t=1;
        }else if(x4==0&&y4>0)
        {
            t=2;
        }else if(x4==0&&y4<0)
        {
            t=3;
        }
        if(a[x2+vex1[t]][y2+vey1[t]])
        {
            x1=x2; y1=y2;
            x2=x2+vex1[t]; y2=y2+vey1[t];
            s+=1;
        }else
        {
            x3=x2+vectorx[t]; y3=y2+vectory[t];
            if(a[x3][y3])
            {
                x1=x2; y1=y2;
                x2=x3; y2=y3;
            }else
            {
                x3=x2+vex2[t]; y3=y2+vey2[t];
                if(a[x3][y3])
                {
                    x1=x2; y1=y2;
                    x2=x3; y2=y3;
                }else
                {
                    temp1=x1; temp2=y1;
                    x1=x2;    y1=y2;
                    x2=temp1; y2=temp2;
                }
            }
            s++;
        }
        if(x2==pos_endx&&y2==pos_endy)
        {
            k=s;
            break;
        }
    }
}
void deal()
{
    int i,j,base,top,x,y,xend,yend;
    int queue[100000][2];
    int sum[50][50];
    memset(status,0,sizeof(status));
    memset(sum,0,sizeof(sum));
    base=top=0;
    queue[top][0]=pos_stax; queue[top++][1]=pos_stay;
    sum[pos_stax][pos_stay]=1; status[pos_stax][pos_stay]=1;
    while(base<top)
    {
        x=queue[base][0]; y=queue[base++][1];
        for(i=0;i<=3;i++)
        {
            xend=x+bx1[i]; yend=y+by1[i];
            if(xend>=1&&xend<=n&¥d>=1&¥d<=m&&!status[xend][yend]&&a[xend][yend])
            {
                sum[xend][yend]=sum[x][y]+1;
                status[xend][yend]=1;
                queue[top][0]=xend; queue[top++][1]=yend;
                if(xend==pos_endx&¥d==pos_endy)
                {
                    break;
                }
            }
        }
        if(i!=4)
        {
            break;
        }
    }
    res3=sum[pos_endx][pos_endy];
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
未来社区的建设背景和需求分析指出,随着智能经济、大数据、人工智能、物联网、区块链、云计算等技术的发展,社区服务正朝着数字化、智能化转型。社区服务渠道由分散向统一融合转变,服务内容由通用庞杂向个性化、服务导向转变。未来社区将构建数字化生态,实现数据在线、组织在线、服务在线、产品智能和决策智能,赋能企业创新,同时注重人才培养和科研平台建设。 规划设计方面,未来社区将基于居民需求,打造以服务为中心的社区管理模式。通过统一的服务平台和应用,实现服务内容的整合和优化,提供灵活多样的服务方式,如推送式、订阅式、热点式等。社区将构建数据与应用的良性循环,提高服务效率,同时注重生态优美、绿色低碳、社会和谐,以实现幸福民生和产业发展。 建设运营上,未来社区强调科学规划、以人为本,创新引领、重点突破,统筹推进、整体提升。通过实施院落+社团自治工程,转变政府职能,深化社区自治法制化、信息化,解决社区治理中的重点问题。目标是培养有活力的社会组织,提高社区居民参与度和满意度,实现社区治理服务的制度机制创新。 未来社区的数字化解决方案包括信息发布系统、服务系统和管理系统。信息发布系统涵盖公共服务类和社会化服务类信息,提供政策宣传、家政服务、健康医疗咨询等功能。服务系统功能需求包括办事指南、公共服务、社区工作参与互动等,旨在提高社区服务能力。管理系统功能需求则涉及院落管理、社团管理、社工队伍管理等,以实现社区治理的现代化。 最后,未来社区建设注重整合政府、社会组织、企业等多方资源,以提高社区服务的效率和质量。通过建立社区管理服务综合信息平台,提供社区公共服务、社区社会组织管理服务和社区便民服务,实现管理精简、高效、透明,服务快速、便捷。同时,通过培育和发展社区协会、社团等组织,激发社会化组织活力,为居民提供综合性的咨询和服务,促进社区的和谐发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值