CodeForces - B. Berland Crossword

原题链接 Problem - 1494B - Codeforceshttps://codeforces.com/problemset/problem/1494/B

原题:

(翻译在下面)

Berland crossword is a puzzle that is solved on a square grid with nn rows and nn columns. Initially all the cells are white.

To solve the puzzle one has to color some cells on the border of the grid black in such a way that:

  • exactly UU cells in the top row are black;
  • exactly RR cells in the rightmost column are black;
  • exactly DD cells in the bottom row are black;
  • exactly LL cells in the leftmost column are black.

Note that you can color zero cells black and leave every cell white.

Your task is to check if there exists a solution to the given puzzle.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of testcases.

Then the descriptions of tt testcases follow.

The only line of each testcase contains 55 integers n,U,R,D,Ln,U,R,D,L (2≤n≤1002≤n≤100; 0≤U,R,D,L≤n0≤U,R,D,L≤n).

Output

For each testcase print "YES" if the solution exists and "NO" otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

原题翻译:

你得到了一个全白的正方形网格, 可以将其中任意方格染黑,以寻找解决方案使

  • 最上面一行恰好 UU 个方格是黑色的;
  • 最右边一列恰好 RR 个方格是黑色的;
  • 最下面一行恰好 DD 个单元格是黑色的;
  • 最左边一列恰好 LL 个单元格是黑色的。

当然,也可以不进行操作。

输入:

        第一行为一个整数 t, 接下来是t组样例。

        每组样例为五个整数,分别为n,U, R, D, L (2 ≤ n ≤ 100; 0 ≤ U, R, D, L ≤ n)。

输出:

        如果存在解决方案,则输出“YES”,否则输出“NO”。

样例:

input

4

5 2 5 3 1

3 0 0 0 0

4 4 1 4 0

2 1 1 1 1 

output 

YES

YES

NO

YES

此为样例1,2,4解决方案之一 

题解: 

我们可以看出相对于边上中间的点,每条边的交点更加重要(会同时影响两条边),所以我们针对四角的格子(之后简称为角格)进行讨论。

而不管如何,因为角格只有涂黑与不涂黑的分别,所以针对角格,列举出全部的情况也只有16种。
因此我们可以枚举出四角的每种情况,同时再分析四条边是否能根据要求涂黑, 从而得到涂黑方案是否可能存在

        比如当n=3即边长为3时,列举四角格子的第一种情况0 0 0 0(即四角均为白色),分别分析四条边,若有边的黑格数要求为2或3,则不符合要求;

        列举最后一种情况1 1 1 1时,若有边的要求为0或1,则不符合要求。

        检查代码可以看下面的check函数 

#include <stdio.h>
using namespace std;

int n, u, r, d, l;
// 将网格的 边长 及 每条边所需涂黑的数量 定义为全局变量,这样就不用传输过多的变量给check函数

int check(int a, int b, int c, int k) // 左上起顺时针的四个角格
{     //printf("a%d b%d c%d k%d  u%d r%d d%d l%d\n", a, b, c, k, a, b, c, k);
      // 输出过程变量

   // 角格涂黑数量 大于 一边所需涂黑数量 则返回False
   // 如 需涂黑1格,但两角格均被涂黑
   // 角格涂黑数量 小于 一边所需涂黑数量 减去 非角格数量(除了角格能涂的最大格子数) 则返回False
   // 如 需涂黑n格,但两角格均为白色
   if (a+b>u || a+b+n-2 < u) return 0;
   if (b+c>r || b+c+n-2 < r) return 0;
   if (c+k>d || c+k+n-2 < d) return 0; 
   if (k+a>l || k+a+n-2 < l) return 0;

   return 1; // 如果四条边都有可能则返回True
}

int main()
{
   int i;
   int a, b, c, k;
   int N;
   scanf("%d", &N);
   for (i = 0; i < N; i++)
   {
      scanf("%d %d %d %d %d", &n, &u, &r, &d, &l);
      bool flag = false;
      for (a = 0; a <= 1; a++)
         for (b = 0; b <= 1; b++)
            for (c = 0; c <= 1; c++)
               for (k = 0; k <= 1; k++) // debug时发现如果定义为d会冲突...
                  if (check(a, b, c, k)) flag = true; // 枚举所有方案并检查

      if (flag) printf("YES\n");
      else printf("NO\n");
   }
                           //getchar();
                           //getchar();
   return 0;
}

最后debug了半天...发现原来是变量冲突了,但因为有一个d是全局变量,所以不会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值