给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置
她在行走过程中,不能转太多弯了,否则她会晕倒的。
(每次在一个方向上一直走到底,并push纪录,然后再一个个吐)
每次对一个方向搜到底,转弯时记录一下,若一个点已经超出要求,则跳过。
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<queue> using namespace std; struct node { int x,y; int t; }; char p[205][205]; int n,m; int x1,x2,y1,y2,kk; int vis[205][205]; int dir[][2] = {{-1,0},{0,1},{1,0},{0,-1}}; bool judge(int x ,int y) { if(x < 0 || x >= n ||y <0 ||y >= m) return false; if(p[x][y] == '*') return false; return true; } bool solve() { queue<node>que; node cur,q,k; cur.x = x1; cur.y = y1; cur.t = -1; vis[x1][y1] = 1; que.push(cur); while(!que.empty()) { k =que.front(); que.pop(); if(k.t >= kk) continue; for(int i = 0; i < 4; i++) { q.x = k.x + dir[i][0]; q.y = k.y + dir[i][1]; q.t = k.t+1; while(1) { if(!judge(q.x,q.y)) break; if(q.x==x2 && q.y==y2) return true; if(!vis[q.x][q.y]) { que.push(q); vis[q.x][q.y]=1; } q.x+=dir[i][0]; q.y+=dir[i][1]; } } } return false; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(vis,0,sizeof(vis)); for(int i = 0; i < n; i++) scanf("%s",p[i]); scanf("%d%d%d%d%d",&kk,&y1,&x1,&y2,&x2); y1--,x1--,y2--,x2--; bool whe = solve(); if(whe) printf("yes "); else printf("no "); } return 0; }