模拟 Codeforces Round #288 (Div. 2) A. Pasha and Pixels

本文介绍了一道模拟题的解题思路及代码实现。题目要求在n*m的空白方格中,通过k次操作涂色,判断何时能形成2*2的黑色方格。文章详细展示了如何用C++进行逻辑判断,并附上了完整的源代码。

 

题目传送门

 1 /*
 2     模拟水题:给定n*m的空白方格,k次涂色,将(x,y)处的涂成黑色,判断第几次能形成2*2的黑色方格,若不能,输出0
 3     很挫的判断四个方向是否OK
 4 */
 5 #include <cstdio>
 6 #include <iostream>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <cstring>
10 #include <map>
11 using namespace std;
12 
13 const int MAXN = 1e3 + 10;
14 const int INF = 0x3f3f3f3f;
15 int a[MAXN][MAXN];
16 
17 bool lose(int i, int j, int n, int m)
18 {
19     if (a[i][j] == 1)
20     {
21         if (j < m && a[i][j+1] == 1)
22         {
23             if (i < n && a[i+1][j] == 1)
24             {
25                 if (a[i+1][j+1] == 1)
26                 {
27                     return true;
28                 }
29             }
30         }
31         if (j>1 && a[i][j-1] == 1)
32         {
33             if (i < n && a[i+1][j-1] == 1)
34             {
35                 if (a[i+1][j] == 1)
36                 {
37                     return true;
38                 }
39             }
40         }
41         if (j > 1 && a[i][j-1] == 1)
42         {
43             if (i > 1 && a[i-1][j-1] == 1)
44             {
45                 if (a[i-1][j] == 1)
46                 {
47                     return true;
48                 }
49             }
50         }
51         if (j < m && a[i][j+1] == 1)
52         {
53             if (i > 1 && a[i-1][j] == 1)
54             {
55                 if (a[i-1][j+1] == 1)
56                 {
57                     return true;
58                 }
59             }
60         }
61     }
62 
63     return false;
64 }
65 
66 int main(void)
67 {
68     #ifndef ONLINE_JUDGE
69         freopen ("A.in", "r", stdin);
70     #endif
71 
72     int n, m, k;
73     while (~scanf ("%d%d%d", &n, &m, &k))
74     {
75         memset (a, 0, sizeof (a));
76 
77         bool flag = false;    int ans = -1;
78         for (int i=1; i<=k; ++i)
79         {
80             int x, y;
81             scanf ("%d%d", &x, &y);
82             if (a[x][y] == 1)    continue;
83 
84             a[x][y] = 1;
85             if (lose (x, y, n, m) && !flag)
86             {
87                 flag = true;    ans = i;
88             }
89         }
90 
91         if (!flag)    printf ("%d\n", 0);
92         else    printf ("%d\n", ans);
93     }
94 
95     return 0;
96 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4366604.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值