2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
no yes
这道题我必须严重的吐槽下
之前是用DFS做的,然后果断就超时了
再用BFS做,但是无论如何样例都过不了,我就郁闷了
找了两个多小时,最后看人家代码才惊讶的发现,原来题目中的X是代表列,Y是代表行!
我擦!
以后得把题目看清楚了T_T
#include <stdio.h> #include <string.h> #include <queue> using namespace std; int n,m,x1,y1,x2,y2,k; int map[105][105],ans; int to[4][2] = {1,0,-1,0,0,1,0,-1}; struct node { int x,y,step; }; int check(int x,int y) { if(x<0 || y<0 || x>=n || y>=m) return -1; return map[x][y]; } int bfs() { int i,flag; queue<node> Q; node a,next; a.x = x1; a.y = y1; a.step = -1; map[a.x][a.y] = -1; Q.push(a); while(!Q.empty()) { a = Q.front(); Q.pop(); if(a.step>k) return -1; if(a.x == x2 && a.y == y2) return 1; for(i = 0; i<4; i++) { next.x = a.x+to[i][0]; next.y = a.y+to[i][1]; flag = check(next.x,next.y); while(flag!=-1) { if(flag==0) { next.step = map[next.x][next.y] = a.step+1; Q.push(next); } next.x+=to[i][0]; next.y+=to[i][1]; flag = check(next.x,next.y); } } } return -1; } int main() { int t; char s[105]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); int i,j; for(i = 0; i<n; i++) { scanf("%s",s); for(j = 0; s[j]; j++) { if(s[j]=='.') map[i][j]=0; else if(s[j]=='*') map[i][j]=-1; } } scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2); x1--; x2--; y1--; y2--; ans = bfs(); if(ans==-1) printf("no\n"); else printf("yes\n"); } return 0; }