这道题做的时候感觉会很麻烦,应为要判断上下左右四个方向,最后还是硬着头皮写了那段代码,但是思维有点乱,最终WA,后来看了学长的代码,虽然看懂了,但是感觉还是很冗杂,对脑细胞伤害太大,后来去网上搜了一下解题报告,看到大牛在BFS中使用DFS思想,恍然大悟,以前认为DFS 和 BFS都是遍历,层层搜索,不需要思考太多东西,但是看了人家的解题报告才明白,对问题进行一定的分析才会让算法更加简洁高效。
下面先附上学长的代码:
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<set>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,num) scanf("%d%d%d",&a,&b,&num)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
struct Loc {
int x;
int y;
int step; //记录行走到某个点时转过的弯
int pre; //记录走到某个点的方式(直走,反向,左转或者右转)
};
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
int sa, sb, ea, eb;
queue<Loc> q;
int m, n;
char map[110][110]; //记录一副地图
int vis[110][110]; //记录走到某个点时最小的转弯数目
int k; //最大允许转弯的次数
int flag;
void bfs() {
for (int i = 0; i < 110; i++)
for (int j = 0; j < 110; j++)
vis[i][j] = INF; //初始化转弯的次数(必定要大于题目允许的最大值)
while (!q.empty())
q.pop();
Loc cur, next;
cur.x = sa;
cur.y = sb;
cur.step = -1;
cur.pre = -1; //初始化
//cur.step=0;
vis[cur.x][cur.y] = 0;
q.push(cur);
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur.x == ea && cur.y == eb && cur.step <= k) {
flag = 1;
return;
}
for (int i = 0; i < 4; i++) {
next = cur; //先把之前的方向记录到下一个点
next.x = cur.x + dir[i][0];
next.y = cur.y + dir[i][1];
if (next.x < 0 || next.x >= m || next.y < 0 || next.y >= n
|| map[next.x][next.y] == '*')
continue;
if (next.pre == -1) //如果还未拐过弯
{
next.pre = i; //记录当前的方向
next.step = 0; //初始化转弯次数
if (vis[next.x][next.y] >= next.step){ //如果在这一点的最小转弯次数大于当前走到这一点转弯的次数,那么更新转弯次数,并将符合条件的点入队
vis[next.x][next.y] = next.step;
q.push(next);
}
} else {
if (cur.pre == i) { //如果该点行进的方向与之前的方向相同,那么只需更新转弯次数就好了,方向在之前已经初始化过了
if (vis[next.x][next.y] >= next.step) {
vis[next.x][next.y] = next.step;
q.push(next);
}
}
else { //如果方向不同
next.step += 1;
next.pre = i; //如果不同(注意!这里掉头也算转弯!),那么更新转弯次数以及方向
if (vis[next.x][next.y] >= next.step) //这里判断一下next.setp是否小于k,这样可以进行优化
{
vis[next.x][next.y] = next.step;
q.push(next);
}
}
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
flag = 0;
scanf("%d%d", &m, &n);
for (int i = 0; i < m; i++)
scanf("%s", map[i]);
scanf("%d%d%d%d%d", &k, &sb, &sa, &eb, &ea); //注意:该题输入时是先输入列在输入行
sa--; //我们的输入是从0开始的,而题目中的输入是从1开始的
sb--;
ea--;
eb--;
bfs();
if (flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
下面用BFS解决的代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 110
char s[maxn][maxn];//建图
int vis[maxn][maxn];//作标记
int dx[4]={1,-1,0,0};//方向向量
int dy[4]={0,0,1,-1};
int t,n,m,kk,x1,x2,y1,y2;
struct node
{
int x,y;
int step;
};
bool judge(int x,int y)//约束条件
{
if(x<0||x>=n||y<0||y>=m) return false;
if(s[x][y]=='*') return false;
return true;
}
bool bfs()//BFS具体过程
{
queue<node> q;
node a,k;
a.x=x1,a.y=y1,a.step=-1;
vis[x1][y1]=1;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
if(a.step>=kk) continue;
for(int i=0;i<4;i++)
{
k.x=a.x+dx[i];
k.y=a.y+dy[i];
k.step=a.step+1;//可以想一下这里标记会怎么样
while(1)//这里就是DFS的思想,想不明白可以画图看下
{
if(!judge(k.x,k.y)) break;
if(k.x==x2 && k.y==y2)
return true;
if(!vis[k.x][k.y])
{
q.push(k);
vis[k.x][k.y]=1;
}
k.x+=dx[i];
k.y+=dy[i];
}
}
}
return false;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
scanf("%d%d%d%d%d",&kk,&y1,&x1,&y2,&x2);
x1--,y1--,x2--,y2--;
memset(vis,0,sizeof(vis));
if(bfs())
printf("yes\n");
else
printf("no\n");
}
return 0;
}