Triangle Array Problem
来源:互联网 发布:黄金历史查询软件 编辑:程序博客网 时间:2024/06/05 16:54
描述
The following is a triangle array with 45 numbers from 1, 2, 3, …, 9.
In the above array we find the road 139654278 from A to the bottom. Now your job is find such a
road from A to the bottom.
输入
The input contains several cases. On the first line is a integer n, which is the triangle arrays. Then
follows the descriptions of the n cases. the data of each case consists of 9 lines. On the i-th line contains i symbols from 1 to 9. There is a blank line between two descriptions.
输出
For each triangle, determine if you can find the road from A to the bottom. The road must contain
the 9 symbols from 1 to 9, and they are different. You are not allowed to return to the above line. First
you print “Case #:”, where # is the number of the case. If you can not find such road you print
“Impossible”, otherwise print “Possible”.
样例输入
1
3 6
9 7 8
2 6 3 9
3 1 5 8 5
5 9 4 6 3 2
8 3 2 9 4 9 1
6 5 7 4 1 2 8 7
7 1 4 8 7 4 5 6 2
4
3 6
9 7 8
2 6 3 9
3 1 5 8 5
5 9 4 6 3 2
8 3 2 9 4 9 1
6 5 7 4 1 2 8 7
7 1 4 8 7 4 5 6 2
样例输出
Case 2:Impossible
#include<iostream>#include<stdio.h>using namespace std;int cp[9][9], sam[9]={1,3,9,6,5,4,2,7,8};bool flag;void dfs(int x,int y,int k){ if(x==9) { flag=true; return ; } if(cp[x][y]==sam[k]) { if(cp[x+1][y]==sam[k+1]) dfs(x+1,y,k+1); if(cp[x+1][y+1]==sam[k+1] && !flag) dfs(x+1,y+1,k+1); }}int main(){ int ca, i, j, k; scanf("%d",&ca); k=1; while(k<=ca) { for(i=0;i<9;i++) for(j=0;j<=i;j++) cin>>cp[i][j]; flag=false; dfs(0,0,0); cout<<"Case "<<k<<":"; if(flag) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; k++; } return 0;}
- Triangle Array Problem
- [Array]Triangle
- Array Pascal's Triangle
- [Array] Pascal's Triangle
- LeetCode刷题【Array】 Triangle
- Arithmetic problem | The Triangle
- LeetCode[Array]: Pascal's Triangle
- [leetcode][array] Pascal's Triangle
- Array Pascal's Triangle II
- [Array]Pascal's Triangle II
- Point inside Triangle/Rectangle Problem
- FOJ Problem 1004 Number Triangle
- FOJ Problem 1004 Number Triangle
- Problem G: Array C
- Matrix Problem : Array Practice
- LeetCode[Array]: Pascal's Triangle II
- [leetcode][array] Pascal's Triangle II
- 【leetcode】Array——Triangle(120)
- Oracle子查询的IN、Any、All
- Codeforces Round #196 (Div. 2) A,B,C
- Android 图片加载总结帖
- JAVA中子类构造函数的继承问题
- Linux性能监测工具Nmon介绍及其使用
- Triangle Array Problem
- 回调函数
- vim大小写转换
- 关于Eclipse4.3 for Java EE 中 ProjectExplorer下文件的排序问题
- Change file permissions
- 如何以命令行方式启动LINUX
- Erlang程序的性能测试工具(1)
- spoj FTOUR2(树的分治)
- linux 临时补充