#include<iostream> #include<queue> using namespace std; char map[110][110]; int visited[110][110]; typedef struct s_e { int x,y,dir,num; }; int m,n,k; s_e start,end,now; queue<s_e>q; int d[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int BFS() { s_e temp;cout<<k<<endl; q.push(start);visited[start.x][start.y]=0; while(!q.empty()) { temp=q.front(); if(temp.x==end.x&&temp.y==end.y) {cout<<temp.num<<endl; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) cout<<visited[i][j]<<" "; cout<<endl; } if(temp.num<=k) return 1; } q.pop(); for(int i=0;i<4;i++) { s_e now; now.x=temp.x+d[i][0]; now.y=temp.y+d[i][1]; if(now.x>=1&&now.x<=m&&now.y>=1&&now.y<=n&&map[now.x][now.y]!='*') { if(temp.dir!=i) { now.num=temp.num+1; if(now.num>k){ goto L;} } else now.num=temp.num; now.dir=i; if(now.num<visited[now.x][now.y])//谁 的visited值小就有机会进入 并压入队列 { visited[now.x][now.y]=now.num; q.push(now); } } L:; } } return 0; } int main() { int i,j,t; cin>>t; while(t--) { cin>>m>>n; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { cin>>map[i][j]; visited[i][j]=11; } cin>>k>>start.y>>start.x>>end.y>>end.x; start.dir=-1;start.num=-1; if(BFS()) cout<<"yes"<<endl; else cout<<"no"<<endl; } system("pause"); return 0; } http://acm.hdu.edu.cn/showproblem.php?pid=1728 没有过 问题visited[] k从小到大错 从大到小 对