题目地址:点击打开链接
思路:用DFS容易超时,用BFS在一个方向搜到底,要多减枝
超时代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int visit[110][110]; char map[110][110]; int m,n,endi,endj,flag,k; int dir[4][2] = {{-1,0,},{1,0},{0,-1},{0,1}}; void dfs(int x,int y,int direction,int sum) { int i,newx,newy; if(flag) return; if(sum > k) return; if(x == endi && y == endj) { flag = 1; return; } for(i=0; i<4; i++) { newx = x + dir[i][0]; newy = y + dir[i][1]; if(newx > 0 && newy > 0 && newx <= m && newy <= n && map[newx][newy] != '*' && !visit[newx][newy]) { visit[newx][newy] = 1; if(direction == i) dfs(newx,newy,i,sum); else dfs(newx,newy,i,sum+1); visit[newx][newy] = 0; } } } int main() { int t,i,j,x1,y1; cin>>t; while(t--) { memset(visit,0,sizeof(visit)); flag = 0; cin>>m>>n; for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { cin>>map[i][j]; } } cin>>k>>y1>>x1>>endj>>endi; for(i=0; i<4; i++) { dfs(x1,y1,i,0); } if(flag) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; }看到大神用DFS秒过,减的太好了,写个思路吧,DFS不用visit数组标记也行,只要把map改一下回溯就行,减值主要靠的是把
错误代码:
#include<iostream> #include<queue> using namespace std; char map[110][110]; int dir[4][2] = {{-1,0,},{1,0},{0,-1},{0,1}}; int m,n,k,endi,endj; struct node { int x; int y; int dir; }a[10010]; bool check(int x,int y) { if(x>0 && y>0 && x<=m && y<=n && map[x][y] != '*') return true; return false; } void bfs(int x,int y) { if(x == endi && y == endj) { cout<<"yes"<<endl; return; } int i,newx,newy; queue<node> q; node in,out; in.x = x; in.y = y; in.dir = -1; q.push(in); while(!q.empty()) { out = q.front(); q.pop(); for(i=0; i<4; i++) { newx = out.x + dir[i][0]; newy = out.y + dir[i][1]; while(check(newx,newy)) { in.x = newx; in.y = newy; in.dir = out.dir + 1; q.push(in); map[newx][newy] = '*';//不能用这个标记已经走过,会导致,以后能走的路却不能走 if(newx == endi && newy == endj && in.dir <= k) { cout<<"yes"<<endl; return; } newx += dir[i][0]; newy += dir[i][1]; } } } cout<<"no"<<endl; } int main() { int t,i,j,x1,y1; cin>>t; while(t--) { cin>>m>>n; for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { cin>>map[i][j]; } } cin>>k>>y1>>x1>>endj>>endi; bfs(x1,y1); } return 0; }
#include<iostream> #include<queue> #include<cstring> using namespace std; int visit[110][110]; char map[110][110]; int dir[4][2] = {{-1,0,},{1,0},{0,-1},{0,1}}; int m,n,k,endi,endj; struct node { int x; int y; int dir; }a[10010]; bool check(int x,int y) { if(x>0 && y>0 && x<=m && y<=n && map[x][y] != '*') return true; return false; } void bfs(int x,int y) { if(x == endi && y == endj) { cout<<"yes"<<endl; return; } int i,newx,newy; queue<node> q; node in,out; in.x = x; in.y = y; in.dir = -1; q.push(in); while(!q.empty()) { out = q.front(); q.pop(); for(i=0; i<4; i++) { newx = out.x + dir[i][0]; newy = out.y + dir[i][1]; while(check(newx,newy)) { if(!visit[newx][newy]) { in.x = newx; in.y = newy; in.dir = out.dir + 1; q.push(in); visit[newx][newy] = 1; if(newx == endi && newy == endj && in.dir <= k) { cout<<"yes"<<endl; return; } } newx += dir[i][0]; newy += dir[i][1]; } } } cout<<"no"<<endl; } int main() { int t,i,j,x1,y1; cin>>t; while(t--) { memset(visit,0,sizeof(visit)); cin>>m>>n; for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { cin>>map[i][j]; } } cin>>k>>y1>>x1>>endj>>endi; bfs(x1,y1); } return 0; }