bfs训练 uva 10384

网上没有这道题的解题报告 我贴下

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1325


dfs实现,有2种实现方法,代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

int g[6][8],dx[]={0,-1,0,1},dy[]={-1,0,1,0};
char res[10010],op[]="WNES";
int x,y,maxdepth;

inline bool get(int x,int k)
{
    return (x>>k)&1;
}

inline void set(int &x,int k,int v)
{
    if(v)x|=(1<<k);
    else x&=(~(1<<k));
}

bool dfs(int depth,int x,int y)
{
    if(depth==maxdepth)
        return x==0||x==5||y==0||y==7;
    
    for(int d=0,nx,ny;d<4;d++)
    {
        nx=x+dx[d],ny=y+dy[d];
        if(!get(g[x][y],d))
        {
            res[depth]=op[d];
            if(dfs(depth+1,nx,ny))return true;
        }
        else if(!get(g[nx][ny],d))
        {
            set(g[x][y],d,0),set(g[nx][ny],d,1),set(g[nx][ny],d^2,0),set(g[nx+dx[d]][ny+dy[d]],d^2,1);
            res[depth]=op[d];
            if(dfs(depth+1,nx,ny))return true;
            set(g[x][y],d,1),set(g[nx][ny],d,0),set(g[nx][ny],d^2,1),set(g[nx+dx[d]][ny+dy[d]],d^2,0);
        }
    }
    
    return false;
}

int main()
{
    for(int i=0;i<6;i++)g[i][0]=g[i][7]=15;
    for(int j=0;j<8;j++)g[0][j]=g[5][j]=15;
    
    while(scanf("%d%d",&y,&x),x)
    {
        memset(res,0,sizeof(res));
        
        for(int i=1;i<=4;i++)for(int j=1;j<=6;j++)
            scanf("%d",&g[i][j]);
        
        for(maxdepth=2;;maxdepth++)
            if(dfs(0,x,y))break;
        
        printf("%s\n",res);
    }
    
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 10
char dir[]="WNES";
int dirv[4]={1,2,4,8};
int dirv2[4]={4,8,1,2};
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
int grid[N][N];
int ans[N*N];
bool vis[N][N];
int nn=4,mm=6,maxd;
using namespace std;
bool dfs(int x,int y,int deep)
{
    int x1,y1,x2,y2;
    if (deep==maxd||vis[x][y]) return false;
    if (x==0||x==nn+1||y==0||y==mm+1)
        return true;
    vis[x][y]=true;
    for (int i=0;i<4;i++)
    {
        x1=x+dx[i];y1=y+dy[i];
        x2=x1+dx[i];y2=y1+dy[i];
        if ((grid[x][y]&dirv[i])==0)
        {
            ans[deep]=i;
            if (dfs(x1,y1,deep+1))
            {
                vis[x][y]=false;
                return true;
            }
        }
        else if ((grid[x1][y1]&dirv[i])==0)
        {
            ans[deep]=i;
            grid[x2][y2]+=dirv2[i];
            grid[x1][y1]+=dirv[i]-dirv2[i];
            grid[x][y]-=dirv[i];
            if (dfs(x1,y1,deep+1))
            {
                vis[x][y]=false;
                grid[x2][y2]-=dirv2[i];
                grid[x1][y1]-=dirv[i]-dirv2[i];
                grid[x][y]+=dirv[i];
                return true;
            }
            grid[x2][y2]-=dirv2[i];
            grid[x1][y1]-=dirv[i]-dirv2[i];
            grid[x][y]+=dirv[i];
        }
    }
    vis[x][y]=false;
    return false;
}
int main()
{
    int x,y;

    for (int i=1;i<=nn;i++)
    {
        grid[i][0]=1;
        grid[i][mm+1]=4;
    }
    for (int i=1;i<=mm;i++)
    {
        grid[0][i]=2;
        grid[nn+1][i]=8;
    }
    while (scanf("%d%d",&y,&x))
    {
        if (x+y==0) break;
        for (int i=1;i<=nn;i++)
            for (int j=1;j<=mm;j++)
                scanf("%d",grid[i]+j);
       maxd=1;
       while (!dfs(x,y,0)) maxd++;
        for (int i=0;i<maxd-1;i++)
            printf("%c",dir[ans[i]]);
        putchar('\n');
    }
    return 0;
}



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页