您的位置:首页 > 其它

hdu 1175 连连看(dfs+剪枝)

2012-10-28 22:25 316 查看
一些细节没处理好,wa了很多次。

剪枝不强,跑了6000+ms。

View Code

/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>

#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

const double eps = 1e-6;

int maz[1005][1005];
int dx[]={1,0,0,-1};
int dy[]={0,1,-1,0};
bool OK;
int n,m;
int x1,y1,x2,y2;
bool check(int x,int y)
{
if(x<1 || x>n || y<1 || y>m)return false;
return true;
}
void dfs(int x,int y,int turn,int dire)
{
if(turn > 2 || !check(x,y))return;
if(OK)return ;
if(x == x2 && y == y2)
{
OK = 1;
return;
}
for(int i=0;i<4;i++)
{
if(dire + i == 3)continue;
int xx = x + dx[i];
int yy = y + dy[i];
if((xx != x2 || yy != y2) && maz[xx][yy] != 0)continue;
if(dire != i && dire != -1)dfs(xx,yy,turn + 1,i);
else dfs(xx,yy,turn,i);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
#endif
while(~scanf("%d%d",&n,&m),n,m)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
scanf("%d",&maz[i][j]);
}
int q;
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(maz[x1][y1] != maz[x2][y2]
|| maz[x1][y1] == 0 || maz[x2][y2] == 0)
{
puts("NO");
continue;
}
OK=0;
dfs(x1,y1,0,-1);
if(OK)puts("YES");
else puts("NO");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: