1.没想到会卡在输入上。。。
//拐弯策略,就是广搜题,只不过加了一个限制条件。即:拐弯数受限。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int MAX = 115;
char map_[MAX][MAX];
int vis[MAX][MAX];
int dir[4][2]={
{
-1,0},{
1,0},{
0,-1},{
0,1}};
struct node{
int x;
int y;
int step;//绝了。
};
int num,row,col;
int judging(int x,int y)
{
if(x>=0&&x<row&&y>=0&&y<col&&(map_[x][y]=='.'||map_[x][y]=='#'))
{
return 1;
}
return 0;
}
int bfs(int x,int y)
{
memset(vis, 0, sizeof(vis));
queue<node>q;
node s,c;
int xx,yy;
s.x=x;
s.y=y;
s.step=-1;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
for(int i=0;i<4;i++)
{
xx=s.x+dir[i][0];
yy=s.y+dir[i][1];
while(judging(xx,yy))
{
//cout<<xx<<yy<<"-----"<<endl;
if(vis[xx][yy]==0)
{
vis[xx][yy]=1;
c.x=xx;
c.y=yy;
c.step=s.step+1;
//cout<<"c.step="<<c.step<<endl;
if(map_[xx][yy]=='#'&&c.step<=num)
{
cout<<"yes"<<endl;
return 0;
}
q.push(c);
}
xx=xx+dir[i][0];
yy=yy+dir[i][1];
}
}
}
cout<<"no"<<endl;
return 0;
}
int main()
{
int t,x1,x2,y1,y2;
cin>>t;
while(t--)
{
cin>>row>>col;
for(int i=0;i<row;i++)
{
scanf("%s",map_[i]);
}
cin>>num>>y1>>x1>>y2>>x2;
map_[x2-1][y2-1]='#';//卡在这里
/*
for(int i=0;i<row;i++)
{
printf("%s\n",map_[i]);
}*/
if(x1==x2&&y1==y2)
{
cout<<"yes"<<endl;
}
else
{
//cout<<x1<<y1<<"----"<<endl;
bfs(x1-1,y1-1);
}
}
return 0;
}
别人的代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
int n,m,k;
char map[110][110];
int vis[110][110];
int step[4][2]= {
{
-1,0},{
1,0},{
0,-1},{
0,1}};
struct node
{
int x,y,step;
};
int check(int x,int y)
{
if(x>=1&&y>=1&&x<=n&&y<=m&&(map[x][y]=='.'||map[x][y]=='#'))
return 1;
return 0;
}
int BFS(int x,int y)
{
queue<node>q;
memset(vis,0,sizeof(vis));
node z,c;
z.x=x;
z.y=y;
z.step=-1;
q.push(z);
int nx,ny;
while(!q.empty())
{
z=q.front();
q.pop();
for(int i=0; i<4; i++)
{
nx=z.x+step[i][0];
ny=z.y+step[i][1];
//printf("nx=%d ny=%d\n",nx,ny);
while(check(nx,ny))
{
//也剪了一下
if(vis[nx][ny]==0)
{
vis[nx][ny]=1;//把同一直线的路都堵死了,剩下的情况只有拐弯了。
c.x=nx;//至于c是下一个行进点
c.y=ny;
//printf("(%d,%d)-->",c.x,c.y);
//拐弯机制【难以想到】
//在同一直线上利用优先队列进行回找,首先找上下方向,构成拐弯。
c.step=z.step+1;
cout<<"c.step="<<c.step<<endl;
if(map[nx][ny]=='#'&&c.step<=k)
{
printf("yes\n");
return 0;
}
//牛皮 ,先进先出,后进后出。
q.push(c);//谈不上什么最小步数,只是解决了拐弯机制,这个题目就解决了。
}
nx+=step[i][0];
ny+=step[i][1];
}
}
}//cout<<endl;
printf("no\n");
}
int main ()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
getchar();
for(int i=1; i<=n; i++)
{
for(int ii=1; ii<=m; ii++)
{
scanf("%c",&map[i][ii]);
}
getchar();
}
node a,b;
scanf("%d%d%d%d%d",&k,&a.y,&a.x,&b.y,&b.x);
map[b.x][b.y]='#';
//剪枝
if(a.x==b.x&&a.y==b.y)
printf("yes\n");
else
BFS(a.x,a.y);
}
return 0;
}