zoukankan      html  css  js  c++  java
  • poj 3083 Children of the Candy Corn(bfs+dfs 数组模拟方向)

    好纠结啊,方向转晕了~~~~~先贴个半山寨的代码

    #include <cstdio>
    #include<string.h>
    #define MAX 45

    struct node
    {
    int x,y;
    }stack[2000];

    int flag[MAX][MAX];
    char map[MAX][MAX];
    int dirl[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; // Left first
    int dirr[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; //Right first
    int w,h, d1,d2;
    int start[2],end[2];

    int dfs(int x,int y,int d,int dir[][2])
    {
    int sx,sy,temp;
    if(x==end[0] && y==end[1]) return 1;

    for(int i=0; i<4; i++)
    {
    temp=(d+i)%4;
    sx=x+dir[temp][0];
    sy=y+dir[temp][1];
    if(sx>=0 && sx<h && sy>=0 && sy<w && flag[sx][sy]==1)
    break;
    }

    return dfs(sx,sy,(temp+3)%4,dir)+1;
    }

    int bfs()
    {
    int last=1,first=0,top=1,di=0,sum=0;
    stack[di].x=start[0],stack[di].y=start[1];
    while(di!=top)
    {
    int x=stack[di].x,y=stack[di++].y;
    if(x==end[0]&&y==end[1]) {sum++;break;}
    last--; flag[x][y]=0;
    for(int i=0;i<4;i++)
    {
    int a=x+dirl[i][0],b=y+dirl[i][1];
    if(a<h && a >=0 &&b>=0&&b < w&& flag[a][b])
    {
    first++;
    flag[a][b]=0;
    stack[top].x=a;
    stack[top++].y=b;
    }
    }
    if(!last)
    {
    last=first;first=0;sum++;
    }
    }
    return sum;
    }

    int main()
    {
    int n;
    //freopen("e:/1.txt","r",stdin);
    scanf("%d",&n);
    while(n--)
    {
    scanf("%d%d",&w,&h);
    memset(flag,0,sizeof(flag));
    for(int i=0; i<h; i++)
    {
    scanf("%s",map[i]);
    for(int j=0; j<w; j++)
    {
    if(map[i][j]=='.')
    flag[i][j]=1;
    else if(map[i][j]=='S')
    {
    start[0]=i;
    start[1]=j;
    }
    else if(map[i][j]=='E')
    {
    end[0]=i;
    end[1]=j;
    flag[i][j]=1;
    }
    }
    }
    if(start[0]==0)
    { // S on the down-side
    d1=1; d2=1;
    }
    else if(start[0]==h-1)
    { //upside
    d1=3; d2=3;
    }
    else if(start[1]==w-1)
    { //right side
    d1=2; d2=0;
    }
    else
    { //left side
    d1=0; d2=2;
    }
    printf("%d ",dfs(start[0],start[1],d1,dirl)); //Left
    printf("%d ",dfs(start[0],start[1],d2,dirr)); //Right
    printf("%d\n",bfs());
    }
    return 0;
    }



    Just a little, maybe change the world
  • 相关阅读:
    splay
    开车旅行(2012day1T3)
    LCT入门
    最小瓶颈路
    poj 3041 Asteroids
    sql waitfor 延时执行
    [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
    SQL Server中行列转换
    sql中 with rollup 、with cube、grouping 统计函数用法
    sql 分组后 组内排名
  • 原文地址:https://www.cnblogs.com/skyming/p/2404570.html
Copyright © 2011-2022 走看看