Triangle Array Problem

描述

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”.

样例输入

2
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 1:Possible
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值