CF1709A Three Doors 题解

题意

有三扇门,每扇门都有一把钥匙与该门上的数字相对应,若数字一样就可以打开。

其中有两扇门后有钥匙,一扇门后为空。

现拥有一把钥匙 \(x\),给出三扇门后的钥匙(若为 \(0\) 即门后没有钥匙),询问是否可以将三扇门都打开。

思路

想要打开三扇门,就需要有三扇门的钥匙,那么前两扇可以打开的门后就必须要有钥匙。

输入后直接判断,当 \(x=0\)\(a_x=0\)\(a_{a_x}=0\),无法打开三扇门。

#include <cstdio>
#include <iostream>
     
using namespace std;
     
int t;
int x;
int a[4];
     
int main()
{
	scanf("%d",&t);
    while(t--)
    {
    	scanf("%d",&x);//输入x
    	scanf("%d%d%d",&a[1],&a[2],&a[3]);//输入三扇门后的钥匙
    		
    	if(x==0)//如果x=0,无法打开任何一扇
    	{
    		puts("NO");
    		continue;
    	}
    		
  		if(a[x]==0||a[a[x]]==0)//如果门后没有钥匙,无法打开三扇门
  		{
  			puts("NO");
  			continue;
  		}

  		puts("YES");
  	}

  	return 0;
}
posted @ 2022-07-22 14:22  Scorilon  阅读(119)  评论(0)    收藏  举报