您的位置:首页 > 产品设计 > UI/UE

hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

2015-11-25 19:41 567 查看
[align=left]Problem Description[/align]

There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1) and the cell below the up-left cell is (1,0)..... A 4×4 grid is shown below:


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1e12
int n;
int flag;
int p,q;
int dirx[]={0,1,0,-1};
int diry[]={1,0,-1,0};
int vis1

,vis2

;
void dfs(int a,int b,int c,int x,int y,int z){
vis1[a][b]=1;
vis2[x][y]=1;
if(flag==1){
return;
}
if(a==x && b==y){
flag=1;
printf("%d %d\n",a,b);
return;
}

if(p && q){
flag=1;
printf("-1\n");
return;
}
int aa,bb,xx,yy;

if(p){
aa=a;
bb=b;
}else{
aa=a+dirx[c];
bb=b+diry[c];
if(aa<0 || aa>=n || bb<0 || bb>=n || vis1[aa][bb]==1){
c=(c+1)%4;
aa=a+dirx[c];
bb=b+diry[c];
if(aa<0 || aa>=n || bb<0 || bb>=n || vis1[aa][bb]==1){
p=1;
aa=a;
bb=b;
}
}
}

if(q){
xx=x;
yy=y;
}else{
xx=x+dirx[z];
yy=y+diry[z];
if(xx<0 || xx>=n || yy<0 || yy>=n || vis2[xx][yy]==1){
z=(z-1+4)%4;
xx=x+dirx[z];
yy=y+diry[z];
if(xx<0 || xx>=n || yy<0 || yy>=n || vis2[xx][yy]==1){
q=1;
xx=x;
yy=y;
}
}
}
dfs(aa,bb,c,xx,yy,z);
}
int main()
{
while(scanf("%d",&n)==1){
if(n==0){
break;
}
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
int a,b,c,x,y,z;
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d%d",&x,&y,&z);
if(a==x && b==y){
printf("%d %d\n",a,b);
continue;
}
flag=0;
p=q=0;
dfs(a,b,c,x,y,z);

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: