hdu1548 奇怪的电梯 dfs dijkstra bfs都可以,在此奉上dfs

题目链接:http://icpc.njust.edu.cn/Problem/Hdu/5706/

简单的规定深度进行搜索,代码如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define dbg(args) cout<<#args<<":"<<args<<endl;
17 #define inf 0x3f3f3f3f
18 #define maxn 1005
19 #define maxm 2000010
20 int n,m,t;
21 char Map[maxn][maxn];
22 int ans1=0,ans2=0; 
23 char s[2][5]={{"cat"},{"girl"}};
24 int dir[][2]={1,0,-1,0,0,1,0,-1};
25 void dfs(int x,int y,int flag,int cur)
26 {
27     int xx,yy;
28     if(flag==1)
29     {
30         if(cur==4)
31         {
32             ans1++;
33             return;
34         }
35         f(i,0,3)
36         {
37              xx=x+dir[i][0];
38              yy=y+dir[i][1];
39             if(xx>=0&&xx<n&&yy>=0&&yy<m&&Map[xx][yy]==s[flag][cur])
40             {
41                 dfs(xx,yy,1,cur+1);
42             }
43         }
44     }
45     else
46     {
47         if(cur==3)
48         {
49             ans2++;
50             return;
51         }
52         f(i,0,3)
53         {
54              xx=x+dir[i][0];
55              yy=y+dir[i][1];
56             if(xx>=0&&xx<n&&yy>=0&&yy<m&&Map[xx][yy]==s[flag][cur])
57             {
58                 dfs(xx,yy,0,cur+1);
59             }
60         }
61     }
62 }
63 int main()
64 {
65     //freopen("input.txt","r",stdin);
66     //freopen("output.txt","w",stdout);
67     std::ios::sync_with_stdio(false);
68     scan(t);
69     while(t--)
70     {
71         ans1=ans2=0;
72         scan(n);
73         scan(m);
74         f(i,0,n-1)scanf("%s",Map[i]);
75         f(i,0,n-1)
76             f(j,0,m-1)
77             {
78                 if(Map[i][j]=='g')
79                 {
80                     dfs(i,j,1,1);
81                 }
82                 if(Map[i][j]=='c')
83                 {
84                     dfs(i,j,0,1);
85                 }
86             }
87             pf("%d %d\n",ans1,ans2);
88     }
89  } 

 

posted @ 2020-03-16 20:26  WA自动机~  阅读(125)  评论(0编辑  收藏  举报