主要是要记录转弯的次数。
1 #include <iostream> 2 #include <stdio.h> 3 #define ll long long 4 using namespace std; 5 int n,m,gx,gy,p[110][110],k; 6 int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1}; 7 char str[110][110]; 8 bool flag; 9 10 void dfs (int x, int y, int dir) { 11 if (x == gx && y == gy) { 12 if (p[x][y] <= k) 13 flag = true; 14 return ; 15 } 16 if ((x !=gx && y != gy && p[x][y] == k)||p[x][y]>k) 17 return ; 18 for (int i = 0; i < 4; i++) { 19 int tx = x + dx[i]; 20 int ty = y + dy[i]; 21 if (tx < 0 || tx >= m|| ty < 0 || ty >= n) 22 continue; 23 if (str[tx][ty] == '*' || p[tx][ty] < p[x][y]) 24 continue; 25 if (dir != -1 && i != dir && p[tx][ty] == p[x][y] ) 26 continue; 27 if (dir != -1 && i != dir) 28 p[tx][ty]=p[x][y]+1; 29 else p[tx][ty] = p[x][y]; 30 dfs (tx, ty, i); 31 if (flag) return ; 32 } 33 } 34 35 int main(){ 36 int t,qx,qy; 37 scanf("%d",&t); 38 while(t--){ 39 scanf("%d%d",&m,&n); 40 for(int i = 0; i < m; i ++) 41 scanf("%s",str[i]); 42 scanf("%d%d%d%d%d",&k,&qy,&qx,&gy,&gx); 43 qx--;qy--;gx--;gy--; 44 for(int i = 0; i < 110; i ++) 45 for(int j = 0; j < 110; j ++) 46 p[i][j] = 100000; 47 p[qx][qy] = 0; 48 flag = false; 49 dfs(qx,qy,-1); 50 if(flag)puts("yes"); 51 else puts("no"); 52 } 53 return 0; 54 }