A. Help Vasilisa the Wise 2 (逻辑)-------C语言—菜鸟级

举报
Fivecc 发表于 2022/08/06 00:58:24 2022/08/06
【摘要】 A. Help Vasilisa the Wise 2 time limit per test2 seconds memory limit per test256 megabytes inputstand...

A. Help Vasilisa the Wise 2
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box’s secret is, since she cannot open it again. She hopes that you will help her one more time with that.

The box’s lock looks as follows: it contains 4 identical deepenings for gems as a 2 × 2 square, and some integer numbers are written at the lock’s edge near the deepenings. The example of a lock is given on the picture below.

The box is accompanied with 9 gems. Their shapes match the deepenings’ shapes and each gem contains one number from 1 to 9 (each number is written on exactly one gem). The box will only open after it is decorated with gems correctly: that is, each deepening in the lock should be filled with exactly one gem. Also, the sums of numbers in the square’s rows, columns and two diagonals of the square should match the numbers written at the lock’s edge. For example, the above lock will open if we fill the deepenings with gems with numbers as is shown on the picture below.

Now Vasilisa the Wise wants to define, given the numbers on the box’s lock, which gems she should put in the deepenings to open the box. Help Vasilisa to solve this challenging task.

Input
The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers d1 and d2 that define the required sums of numbers on the main and on the side diagonals of the square (1 ≤ r1, r2, c1, c2, d1, d2 ≤ 20). Correspondence between the above 6 variables and places where they are written is shown on the picture below. For more clarifications please look at the second sample test that demonstrates the example given in the problem statement.

Output
Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number “-1” (without the quotes).

If there are several solutions, output any.

Examples
inputCopy
3 7
4 6
5 5
outputCopy
1 2
3 4
inputCopy
11 10
13 8
5 16
outputCopy
4 7
9 1
inputCopy
1 2
3 4
5 6
outputCopy
-1
inputCopy
10 10
10 10
10 10
outputCopy
-1
水题一个 满足条件 唯一 枚举第一空为 1~9 其他根据规则限定再判断

#include<stdio.h>
int main()
{
 int r1,r2,c1,c2,d1,d2,i,x1,x2,x3,x4;
   while(scanf("%d%d%d%d%d%d",&r1,&r2,&c1,&c2,&d1,&d2)!=EOF)
   {
   	
   	for(i=1;i<=9;i++)
   	{
   		x1=i; x4=d1-x1;
   		x2=r1-x1;x3=r2-x4;
   	  if(x4>9||x2>9||x3>9)continue;
      if(x2==x4||x2==x1||x2==x3||x3==x4||x3==x1||x4==x1||x4<=0||x2<=0||x3<=0)continue;
      if(x2!=(d2-x3)||x2!=(c2-x4))continue;
      if(x3!=(d2-x2)||x3!=(c1-x1))continue;
      if(x4!=(c2-x2))continue;
      if(x1!=(c1-x3))continue;
      break;	
   	}
	   if(i<=9)  printf("%d %d\n%d %d\n",x1,x2,x3,x4);
   	   else printf("-1\n");
   }

return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

文章来源: fivecc.blog.csdn.net,作者:Five-菜鸟级,版权归原作者所有,如需转载,请联系作者。

原文链接:fivecc.blog.csdn.net/article/details/80293262

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。