讲实话,做得很糟糕,不过这是我广搜的处女题,而且调试了两天才Accept,前面基本上什么错误都出现了,特别是MLE,不会剪枝,做得很吃力,为自己喝彩!
更搞笑的是10000MS的题目,我刚好用了9890MS
Problem : 1175 ( 连连看 ) Judge Status : Accepted
RunId : 5590624 Language : G++ Author : ssun
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<string>
#include<queue>
using namespace std;
const int N = 1005;
int m,n,q;
int map[N][N];
int visit[N][N];
int x1,y1,x2,y2;
int direct[4][2] = {0,-1,1,0,-1,0,0,1};
struct point{
int x;
int y;
int dir;
int round;
};
bool bfs(int x,int y,int dir, int round)
{
int i;
queue<point> pp;
point now = {x,y,dir,round};
pp.push(now);
while(!pp.empty())
{
point temp = pp.front();
pp.pop();
if(temp.round > 2) continue;
for(i=0; i<4; i++)
{
now.x = temp.x + direct[i][0];
now.y = temp.y + direct[i][1];
if(temp.dir==i || temp.dir == 4 ) now.round = temp.round;
else now.round = temp.round + 1;
now.dir = i;
if(now.x == x2 && now.y == y2 && now.round<=2) return true;
if(!map[now.x][now.y] && !visit[now.x][now.y] && now.x >0 && now.y >0 && now.x < n+1 && now.y < m+1)
{
pp.push(now);
}
}
visit[temp.x][temp.y] = 1;
}
return false;
}
int main()
{
int i,j;
while(cin>>n>>m && (m!=0||n!=0))
{
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
cin>>map[i][j];
}
}
cin>>q;
while(q--)
{
memset(visit,0,sizeof(visit));
cin>>x1>>y1>>x2>>y2;
if(map[x1][y1] != map[x2][y2])
{
cout<<"NO"<<endl;
continue;
}
if(map[x1][y1]!=0 && bfs(x1,y1,4,0))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}
/*
5 5
1 2 3 4 5
0 0 0 0 0
0 0 0 0 0
0 0 0 2 2
5 4 1 2 1
3
1 5 5 1
1 1 5 5
1 2 5 4
4 4
1 2 3 4
0 0 0 0
0 0 0 0
3 2 1 4
3
1 1 4 3
1 3 3 1
*/