Codeforces 143A Help Vasilisa the Wise 2(暴力)
来源:互联网 发布:linux 安装php ssh2 编辑:程序博客网 时间:2024/06/05 03:04
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.
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.
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.
3 74 65 5
1 23 4
11 1013 85 16
4 79 1
1 23 45 6
-1
10 1010 1010 10
-1
Pay attention to the last test from the statement: it is impossible to open the box because for that Vasilisa the Wise would need 4 identical gems containing number "5". However, Vasilisa only has one gem with each number from 1 to 9.
题解:
很水的暴力题,四重循环,dfs都可以过
代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>using namespace std;struct node{ int a[5]; int r1,r2,c1,c2,d1,d2; void done() { r2=a[3]+a[4]; r1=a[1]+a[2]; d1=a[1]+a[4]; c1=a[1]+a[3]; c2=a[2]+a[4]; d2=a[2]+a[3]; }}t[10005];int b[10];int ans;int vis[10];void dfs(int step){ if(step==4) { for(int i=0;i<4;i++) { t[ans].a[i+1]=b[i]; } t[ans].done(); ans++; return ; } for(int i=1;i<10;i++) { if(!vis[i]) { b[step]=i; vis[i]=1; dfs(step+1); vis[i]=0; } }}int main(){ memset(vis,0,sizeof(vis)); ans=0; dfs(0); int r1,r2,c1,c2,d1,d2,tag=0; scanf("%d%d%d%d%d%d",&r1,&r2,&c1,&c2,&d1,&d2); for(int i=0;i<ans;i++) { if(t[i].r1==r1&&t[i].r2==r2&&t[i].c1==c1&&t[i].c2==c2&&t[i].d1==d1&&t[i].d2==d2) { printf("%d %d\n",t[i].a[1],t[i].a[2]); printf("%d %d\n",t[i].a[3],t[i].a[4]); tag=1; break; } } if(!tag) printf("-1\n"); return 0;}
- Codeforces 143A Help Vasilisa the Wise 2(暴力)
- A. Help Vasilisa the Wise 2 codeforces-problem-143A
- A. Help Vasilisa the Wise 2
- codeforce 143 A Help Vasilisa the Wise 2
- code forces Help Vasilisa the Wise 2
- CF—98A—Help Victoria the Wise
- Help Victoria the Wise(cf 07/22/2011)
- Codeforces 585A Gennady the Dentist(暴力)
- Codeforces 601A The Two Routes(暴力)
- codeforces 3-A. Shortest path of the king(暴力)
- Codeforces 487A Fight the Monster(暴力)
- Codeforces 612A The Text Splitting 【暴力】
- Codeforces Round #271 (Div. 2) --A Keyboard (暴力)
- Codeforces Round #283 (Div. 2)---A. Minimum Difficulty (暴力)
- Codeforces Round #281 (Div. 2)A(构造+暴力模拟)
- Codeforces Round #344 (Div. 2) A 题题解 (暴力)
- Codeforces Round #354 (Div. 2)(A)暴力,模拟
- Codeforces Round #433 (Div. 2) A.Fraction(暴力)
- 自己的HADOOP平台(二):在Hadoop平台上搭载Flume
- Java面向对象 备忘录
- 权限学习 -- Spring Security拦截器
- 算法分析——Hanoi塔问题
- 没办法还是锁机制的讲解
- Codeforces 143A Help Vasilisa the Wise 2(暴力)
- 前端学习总结(一)HTML5基础
- 补♂课第10场解题报告
- 【学习C++】学习C++ -> 向量(vector)
- 数字反转模版
- jQuery的选择器范例
- 欢迎使用CSDN-markdown编辑器
- code forces Amusing Joke
- C和指针之结构体大小和成员变量位置距离结构开始存储的位置偏移字节