题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175
思路:在DFS里增加dir,turn,分别表示方向,拐弯次数,如果turn超过2次则必须返回,DFS必须剪枝,否则超时,下面代码:593MS 3348K
先上DFS的代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <fstream>
using namespace std;
int maze[1010][1010];
bool visit[1010][1010];
int y;
int n,m,x1,x2,y2;
bool flag;
void dfs(int x1,int y1,int dir,int turn)// dir:方向,turn:转弯次数
{
if(x1<=0||x1>n||y1<=0||y1>m)return ;//边界
if(flag)return ; //已到达
if(turn>=3)return ; //折线超过2次返回
if(x1==x2&y1==y2){
flag=1;
return ;
}
if(turn==2){ //这个剪枝:若已经拐弯2次,此时若x2,y2和x1,y1没有在一条直线上则剪掉
if( !(dir==1&&x1>x2&&y1==y2||dir==2&&x1<x2&&y1==y2||dir==3&&y1>y2&&x1==x2||dir==4&&y1<y2&&x1==x2) )
return ;
}
if(maze[x1][y1]!=0)return ;
if(visit[x1][y1])return ;
visit[x1][y1]=1;
if(dir==1){ //往上走,用dir==1表示,此时三种情况,1:继续往上走,不拐
//2:往右走,拐一次,3:往左走,拐一次。以下类似。
dfs(x1-1,y1,1,turn);
dfs(x1,y1-1,3,turn+1);
dfs(x1,y1+1,4,turn+1);
}
else if(dir==2){
dfs(x1+1,y1,2,turn);
dfs(x1,y1-1,3,turn+1);
dfs(x1,y1+1,4,turn+1);
}
else if(dir==3){
dfs(x1-1,y1,1,turn+1);
dfs(x1+1,y1,2,turn+1);
dfs(x1,y1-1,3,turn);
}
else if(dir==4){
dfs(x1-1,y1,1,turn+1);
dfs(x1+1,y1,2,turn+1);
dfs(x1,y1+1,4,turn);
}
visit[x1][y1]=0;
}
int main()
{
while(cin>>n>>m)
{
if(n==0&&m==0)break;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>maze[i][j];
int q;
cin>>q;
while(q--)
{
cin>>x1>>y>>x2>>y2;
flag=0;
if(x1==x2&&y==y2){
cout<<"NO"<<endl;
continue;
}
if(maze[x1][y]!=maze[x2][y2]||maze[x1][y]==0||maze[x2][y2]==0){
cout<<"NO"<<endl;
continue;
}
//若同一位置,或者消去的两个不同,或者其中有个0则不能消去
memset(visit,false,sizeof(visit));
flag=0;
dfs(x1-1,y,1,0); //上
dfs(x1+1,y,2,0); //下
dfs(x1,y-1,3,0); //左
dfs(x1,y+1,4,0); //右
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
bfs的代码效率比较低,我一直以为dfs时间复杂度一直比bfs的高,但是由于剪枝的关系,这道题bfs的AC时间是2718MS,4倍的差距啊,先上代码研读一下再说:
#include <iostream>
#include <queue>
using namespace std;
const int N = 1001;
bool flag;
int n,m,sx,sy,ex,ey;
int hash[N][N],map[N][N];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node{
int x,y,turn,d;
}start;
queue<node> q;
inline bool in(const node &p){
if(p.x<0 || p.y<0 || p.x>=n || p.y>=m)
return false;
return true;
}
void bfs(){
node now,t;
while(!q.empty()){
now=q.front(),q.pop();
if(now.x==ex && now.y==ey && now.turn<=2){
flag=true;
return;
}
for(int i=0;i<4;i++){
t.x=now.x+dir[i][0],t.y=now.y+dir[i][1];
if(now.d==i)
t.turn=now.turn,t.d=now.d;
else
t.turn=now.turn+1,t.d=i;
if(in(t) && (map[t.x][t.y]==0||t.x==ex&&t.y==ey) && hash[t.x][t.y]>=t.turn)
hash[t.x][t.y]=t.turn,q.push(t);
}
}
}
int main(){
int i,j,t;
while(scanf("%d %d",&n,&m),n||m){
for(i=0;i<n;i++)
for(j=0;j<m;j++) scanf("%d",&map[i][j]);
scanf("%d",&t);
while(t--){
scanf("%d %d %d %d",&sx,&sy,&ex,&ey);
sx--,sy--,ex--,ey--;
if((map[sx][sy]!=map[ex][ey]) || map[sx][sy]==0 || map[ex][ey]==0 || (sx==ex&&sy==ey)){
puts("NO");
continue;
}
for(i=0;i<n;i++)
for(j=0;j<m;j++) hash[i][j]=11;
while(!q.empty()) q.pop();
for(i=0;i<4;i++){
start.x=sx,start.y=sy,start.turn=0,start.d=i;
q.push(start);
}
flag=false,hash[sx][sy]=0;
bfs();
puts(flag ? "YES":"NO");
}
}
return 0; }