SDUT 1269 走迷宫
原题地址
记录一下第一次写的DFS类型的方向类问题
这种问题首先要记录问题中可以走的方向,然后根据题的不同使用不同的特殊条件判定和输出。
这道题主要坑的地方在于方向必须是上左下右,不然就是WA。
附上代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iomanip>
//#include <string.h>
#include <bits/stdc++.h>
#define INF 999999999
using namespace std;
/*struct shi
{
int x;
int T;
} s[100000];
int cmp(shi a,shi b)
{
return a.x<b.x;
}
long long cmd(long long a, long long b)
{
return a > b;
}*/
int dp[17][17];
int mox[]={0,-1,0,1};
int moy[]={-1,0,1,0};
int X[400]={0},Y[400]={0};
bool vis[17][17]= {0};
int sx,sy;
int ex,ey;
int n,r;
int falg,step;
void dfs(int a,int b)
{
vis[a][b]=1;
if(a<1||b<1||a>n||b>r)
return ;
if(a==ex&&b==ey)
{
falg=0;
for(int i=0;i<step;i++)
{
if(i!=step-1)
cout<<"("<<X[i]<<","<<Y[i]<<")"<<"->";
else
cout<<"("<<X[i]<<","<<Y[i]<<")";
}
cout<<endl;
return ;
}
for(int i=0;i<4;i++){
int xx=a+mox[i];
int yy=b+moy[i];
if(vis[xx][yy]!=1&&dp[xx][yy])
{
X[step]=xx;
Y[step]=yy;
step++;
dfs(xx,yy);
vis[xx][yy]=0;
step--;
}
}
}
int main()
{
//int n,r;
cin >>n>>r;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=r; j++)
{
cin >>dp[i][j];
}
}
cin >>sx>>sy;
cin>>ex>>ey;
step=0,falg=1;
X[0]=sx;
Y[0]=sy;
step++;
dfs(sx,sy);
if(falg)
cout<<-1<<endl;
return 0;
}