//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;
}
UVA 225 剪枝+dfs
本文介绍了一种基于深度优先搜索的算法,用于寻找从起点到终点的合法路径,并统计所有可能的路径数量。该算法应用于特定大小的迷宫中,需要避开预设障碍物,通过递归方式检查每个方向是否可行。
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
132

被折叠的 条评论
为什么被折叠?



