codeforces A. Table 解题报告

题目链接:http://codeforces.com/problemset/problem/359/A

题目意思:给出一个n行m列的table,你需要选择一个good cell(假设为(x, y), 1<=x <=n,1<=y <=m)和任意的一个corner((1, 1), (n, 1), (1, m), (n, m)),此时你可以把这个good cell 和 corner所围住的区域上色,这个区域(p, q)满足 min(good cell的横坐标,corner的横坐标) ≤ p ≤ max(good cell的横坐标,corner的横坐标), min(good cell的纵坐标,corner的纵坐标) ≤ q ≤ max(good cell的纵坐标,corner的纵坐标).。需要找出一个上色方案并输出总共需要的次数。当然,已上色的区域可以重复再上色。

     首先考虑特殊的位置,第1行、第1列、第n行、第m列。只要满足有一个good cell在这些位置,则上色次数最少是2次(离它最远的两个corner)。

     接着是考虑一般的位置,也就是除第1行、第1列、第n行、第m列的位置里有good cell,那么要分两种情况讨论:1、特殊位置里也有good cell(最少次数为2次);2、特殊位置里没有good cell(最少次数为4次)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int i, j, n, m, t, f2, f;
 9     while (scanf("%d%d", &n, &m) != EOF)
10     {
11     //    freopen("in.txt", "r", stdin);
12         f2 = f = 0;
13         for (i = 1; i <= n; i++)
14         {
15             for (j = 1; j <= m; j++)
16             {
17                 scanf("%d", &t);
18                 if (t == 1 && (i == 1 || j == 1 || i == n || j == m) && !f2)    // good cell在特殊位置里
19                 {
20                     f = 2;
21                     f2 = 1;
22                 }
23             }
24         }
25         if (f2)
26             printf("2
");
27         else
28             printf("4
");    
29     }
30     return 0;
31 }
原文地址:https://www.cnblogs.com/windysai/p/3413208.html