UVA 225 剪枝+dfs

本文介绍了一种基于深度优先搜索的算法,用于寻找从起点到终点的合法路径,并统计所有可能的路径数量。该算法应用于特定大小的迷宫中,需要避开预设障碍物,通过递归方式检查每个方向是否可行。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

//http://acm.hust.edu.cn/vjudge/problem/35587

#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <sstream>
#include <time.h>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <bitset>
using namespace std;
const int MAX=300;
const int dX[4]= {1,0,0,-1};
const int dY[4]= {0,1,-1,0};
const char dD[4]= {'e','n','s','w'};
int T,N,M,Cnt;
char Ans[50];
int G[2*MAX+10][2*MAX+10];
bool check(int n,int x,int y,int m)
{
    int cx=x,cy=y;
    for (int i=1; i<=n; i++)
    {
        cx+=dX[m],cy+=dY[m];
        if (G[cx+MAX][cy+MAX]==-1)
            return false;
    }
    return true;
}
void dfs (int n,int x,int y,int last)
{
    if (n==N+1&&x==0&&y==0)
    {
        Cnt+=1;
        for (int i=1; i<=N; i++)
            cout<<Ans[i];
        cout<<'\n';
        return;
    }
    if (n==N+1)
        return;
    for (int i=0; i<4; i++)
    {
        if (last==i||last+i==3)
            continue;
        if (check(n,x,y,i))
        {
            int nx=x+dX[i]*n,ny=y+dY[i]*n;
            if (G[nx+MAX][ny+MAX])
                continue;
            G[nx+MAX][ny+MAX]=1;
            Ans[n]=dD[i];
            dfs(n+1,nx,ny,i);
            G[nx+MAX][ny+MAX]=0;
        }
    }
}
int main()
{
    cin.sync_with_stdio(false);
    cin>>T;
    int a,b;
    while (T--)
    {
        Cnt=0;
        memset(G,0,sizeof(G));
        cin>>N>>M;
        for (int i=0; i<M; i++)
        {
            cin>>a>>b;
            if (a<MAX&&a>-MAX&&b<MAX&&b>-MAX)
                G[a+MAX][b+MAX]=-1;
        }
        dfs(1,0,0,-1);
        cout<<"Found "<<Cnt<<" golygon(s).\n\n";
    }
    return 0;
}

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值