hdu5254 棋盘占领 dfs

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5254

题意:

题解:

暴力

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define MS(a) memset(a,0,sizeof(a))
 5 #define MP make_pair
 6 #define PB push_back
 7 const int INF = 0x3f3f3f3f;
 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 9 inline ll read(){
10     ll x=0,f=1;char ch=getchar();
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 //////////////////////////////////////////////////////////////////////////
16 const int maxn = 5e2+10;
17 
18 int f[maxn][maxn];
19 
20 int main(){
21     int T=read();
22     for(int cas=1; cas<=T; cas++){
23         MS(f);
24         int n=read(),m=read();
25         int g = read();
26         for(int i=1; i<=g; i++){
27             int x=read(),y=read();
28             f[x][y] = 1;
29         }
30         while(1){
31             bool add = false;
32             for(int i=1; i<=n; i++){
33                 for(int j=1; j<=m; j++){
34                     if(f[i][j]) continue;
35                     if(f[i-1][j] && f[i][j-1] && i>1 && j>1){
36                         f[i][j] = 1;
37                         add = true;
38                         continue;
39                     }
40 
41                     if(f[i-1][j] && f[i][j+1] && i>1 && j<m){
42                         f[i][j] = 1;
43                         add = true;
44                         continue;
45                     }
46 
47                     if(f[i+1][j] && f[i][j-1] && i<n && j>1){
48                         f[i][j] = 1;
49                         add = true;
50                         continue;
51                     }
52 
53                     if(f[i+1][j] && f[i][j+1] && i<n && j<m){
54                         f[i][j] = 1;
55                         add = true;
56                         continue;
57                     }
58                 }
59             }
60             if(!add) break;
61         }
62         int ans = 0;
63         for(int i=1; i<=n; i++)
64             for(int j=1; j<=m; j++)
65                 ans += f[i][j];
66         cout << "Case #" << cas << ":
" << ans << endl;
67     }
68 
69     return 0;
70 }
原文地址:https://www.cnblogs.com/yxg123123/p/6827687.html