hdu 4741 2013杭州赛区网络赛 dfs ***

起点忘记录了,一直wa

代码写的很整齐,看着很爽

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 #define MOD 1000000007
10 const int INF=0x3f3f3f3f;
11 const double eps=1e-5;
12 typedef long long ll;
13 #define cl(a) memset(a,0,sizeof(a))
14 #define ts printf("*****\n");
15 const int MAXN=1005;
16 int n,m,tt;
17 int dir[4][2]={0,1,1,0,0,-1,-1,0};
18 bool vis1[MAXN][MAXN];
19 bool vis2[MAXN][MAXN];
20 void dfs(int dx,int dy,int dd,int tx,int ty,int td)
21 {
22     if(dx==tx&&dy==ty)
23     {
24         printf("%d %d\n",dx,dy);
25         return;
26     }
27     bool f1=0,f2=0;
28     int ndd=dd;
29     int ndx=dx+dir[dd][0];
30     int ndy=dy+dir[dd][1];
31     if(ndx<0||ndx>=n||ndy<0||ndy>=n||vis1[ndx][ndy])    ndd=(dd+1)%4;
32     ndx=dx+dir[ndd][0];
33     ndy=dy+dir[ndd][1];
34     if(ndx<0||ndx>=n||ndy<0||ndy>=n||vis1[ndx][ndy])
35     {
36         f1=1;
37         ndx=dx,ndy=dy,ndd=dd;
38     }
39 
40 
41     int ntd=td;
42     int ntx=tx+dir[td][0];
43     int nty=ty+dir[td][1];
44     if(ntx<0||ntx>=n||nty<0||nty>=n||vis2[ntx][nty])    ntd=(td-1+4)%4;
45     ntx=tx+dir[ntd][0];
46     nty=ty+dir[ntd][1];
47     if(ntx<0||ntx>=n||nty<0||nty>=n||vis2[ntx][nty])
48     {
49         f2=1;
50         ntx=tx,nty=ty,ntd=td;
51     }
52     if(f1&&f2)
53     {
54         printf("-1\n");
55         return;
56     }
57     vis1[ndx][ndy]=1;
58     vis2[ntx][nty]=1;
59 
60     dfs(ndx,ndy,ndd,ntx,nty,ntd);
61 }
62 int main()
63 {
64     int i,j,k,ca=1;
65     #ifndef ONLINE_JUDGE
66     freopen("1.in","r",stdin);
67     #endif
68     int dx,dy,dd,tx,ty,td;
69     int tot=0;
70     while(scanf("%d",&n)!=EOF&&n)
71     {
72         cl(vis1);
73         cl(vis2);
74         scanf("%d%d%d",&dx,&dy,&dd);
75         scanf("%d%d%d",&tx,&ty,&td);
76         vis1[dx][dy]=1;
77         vis2[tx][ty]=1;
78         dfs(dx,dy,dd,tx,ty,td);
79     }
80 }

 

posted @ 2015-08-30 15:47  miao_a_miao  阅读(193)  评论(0编辑  收藏  举报