zoj-3938
Defuse the Bomb
Time Limit: 2 Seconds Memory Limit: 65536 KB
The bomb is about to explode! Please defuse it as soon as possible!
There is a display showing a number from 1 to 4 on the bomb. Besides this, there are 4 buttons under the display. Each button is labeled by a number from 1 to 4. The numbers on the buttons are always distinct.
There are 5 defusing stages in total. Pressing the correct button can progress the bomb to the next defusing stage. The number on the display and the number on each button may be different in different stages. The bomb will be defused only when all 5 defusing stages get passed. Pressing the incorrect button will cause the bomb to explode immediately. Be careful!
Here is the detailed bomb defusing manual. Button positions are ordered from left to right.
Stage 1:
- If the display is 1, press the button in the second position.
- If the display is 2, press the button in the second position.
- If the display is 3, press the button in the third position.
- If the display is 4, press the button in the fourth position.
Stage 2:
- If the display is 1, press the button labeled “4”.
- If the display is 2, press the button in the same position as you pressed in stage 1.
- If the display is 3, press the button in the first position.
- If the display is 4, press the button in the same position as you pressed in stage 1.
Stage 3:
- If the display is 1, press the button with the same label you pressed in stage 2.
- If the display is 2, press the button with the same label you pressed in stage 1.
- If the display is 3, press the button in the third position.
- If the display is 4, press the button labeled “4”.
Stage 4:
- If the display is 1, press the button in the same position as you pressed in stage 1.
- If the display is 2, press the button in the first position.
- If the display is 3, press the button in the same position as you pressed in stage 2.
- If the display is 4, press the button in the same position as you pressed in stage 2.
Stage 5:
- If the display is 1, press the button with the same label you pressed in stage 1.
- If the display is 2, press the button with the same label you pressed in stage 2.
- If the display is 3, press the button with the same label you pressed in stage 4.
- If the display is 4, press the button with the same label you pressed in stage 3.
Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:
There are 5 lines. Each line contains 5 integers D, B1, B2, B3, B4 indicating the number on the display and the numbers on the buttons respectively. The i-th line correspond to the i-th stage.
Output
For each test case, output 5 lines. The i-th line contains two integers indicating the position and the label of the correct button for the i-th stage.
Sample Input
1
4 2 1 3 4
2 2 4 3 1
4 3 1 4 2
4 3 4 2 1
2 3 1 2 4
Sample Output
4 4
4 1
3 4
4 1
2 1
Hint
Keep talking with your teammates and nobody explodes!
Author: JIANG, Kai
Source: The 13th Zhejiang Provincial Collegiate Programming Contest
题目大意:
按要求要经过 5 个阶段,每个阶段会显示 1—4 中的一个数,且有 1—4 号按钮,
根据显示数的不同来决定按钮的不同,依次显示该按钮的位置和编号。
思路:模拟,
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[10][10];
struct node
{
int x,y;
}num[10000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
scanf("%d",&a[i][j]);
}
}
if(a[1][1]==1)//stage1
{
num[1].x=2;
num[1].y=a[1][3];
}
if(a[1][1]==2)
{
num[1].x=2;
num[1].y=a[1][3];
}
if(a[1][1]==3)
{
num[1].x=3;
num[1].y=a[1][4];
}
if(a[1][1]==4)
{
num[1].x=4;
num[1].y=a[1][5];
}
if(a[2][1]==1)//stage2
{
for(k=2;k<=5;k++)
{
if(a[2][k]==4)
{
num[2].x=k-1;
break;
}
}
num[2].y=4;
}
if(a[2][1]==2||a[2][1]==4)
{
num[2].x=num[1].x;
num[2].y=a[2][num[2].x+1];
}
if(a[2][1]==3)
{
num[2].x=1;
num[2].y=a[2][2];
}
//stage3
if(a[3][1]==1)
{
for(k=2;k<=5;k++)
{
if(a[3][k]==num[2].y)
{
num[3].x=k-1;
}
}
num[3].y=num[2].y;
}
if(a[3][1]==2)
{
for(k=2;k<=5;k++)
{
if(a[3][k]==num[1].y)
{
num[3].x=k-1;
}
}
num[3].y=num[1].y;
}
if(a[3][1]==3)
{
num[3].x=3;
num[3].y=a[3][4];
}
if(a[3][1]==4)
{
num[3].y=4;
for(k=2;k<=5;k++)
{
if(a[3][k]==4)
{
num[3].x=k-1;
break;
}
}
}
//stage4
if(a[4][1]==1)
{
num[4].x=num[1].x;
num[4].y=a[4][num[1].x+1];
}
if(a[4][1]==2)
{
num[4].x=1;
num[4].y=a[4][2];
}
if(a[4][1]==3||a[4][1]==4)
{
num[4].x=num[2].x;
num[4].y=a[4][num[4].x+1];
}
//stage4
if(a[5][1]==1)
{
for(k=2;k<=5;k++)
{
if(a[5][k]==num[1].y)
{
num[5].x=k-1;
}
}
num[5].y=num[1].y;
}
//stage5
if(a[5][1]==2)
{
for(k=2;k<=5;k++)
{
if(a[5][k]==num[2].y)
{
num[5].x=k-1;
}
}
num[5].y=num[2].y;
}
if(a[5][1]==3)
{
for(k=2;k<=5;k++)
{
if(a[5][k]==num[4].y)
{
num[5].x=k-1;
}
}
num[5].y=num[4].y;
}
if(a[5][1]==4)
{
for(k=2;k<=5;k++)
{
if(a[5][k]==num[3].y)
{
num[5].x=k-1;
}
}
num[5].y=num[3].y;
}
for(i=1;i<=5;i++)
{
printf("%d %d\n",num[i].x,num[i].y);
}
}
return 0;
}
还没有评论,来说两句吧...