zoj 3938 Defuse the Bomb

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

分析:一步步模拟过程即可!

#include<cstdio>
#include<iostream>
using namespace std;
int a[5][2];//P L
int map[6][6];
int stage1(){
    if(map[0][0]==1){
        a[0][0]=2;
        a[0][1]=map[0][2];
    }
    if(map[0][0]==2){
        a[0][0]=2;
        a[0][1]=map[0][2];
    }
    if(map[0][0]==3){
        a[0][0]=3;
        a[0][1]=map[0][3];
    }
    if(map[0][0]==4){
        a[0][0]=4;
        a[0][1]=map[0][4];
    }
}
int stage2(){
    if(map[1][0]==1){
        for(int k=1;k<5;k++){
            if(map[1][k]==4){
                a[1][0]=k;
                break;
            }
        }
        a[1][1]=4;
    }
    if(map[1][0]==2){
        a[1][0]=a[0][0];
        a[1][1]=map[1][a[1][0]];

    }
    if(map[1][0]==3){
        a[1][0]=1;
        a[1][1]=map[1][1];
    }
    if(map[1][0]==4){
        a[1][0]=a[0][0];
        a[1][1]=map[1][a[1][0]];
    }
}
int stage3(){
    if(map[2][0]==1){
        a[2][1]=a[1][1];
        for(int k=1;k<5;k++){
            if(map[2][k]==a[2][1]){
                a[2][0]=k;
                break;
            }
        }
    }
    if(map[2][0]==2){
        a[2][1]=a[0][1];
        for(int k=1;k<5;k++){
            if(map[2][k]==a[2][1]){
                a[2][0]=k;
                break;
            }
        }
    }
    if(map[2][0]==3){
        a[2][0]=3;
        a[2][1]=map[2][3];
    }
    if(map[2][0]==4){
        for(int k=1;k<5;k++){
            if(map[2][k]==4){
                a[2][0]=k;
                break;
            }
        }
        a[2][1]=4;
    }
}
int stage4(){
    if(map[3][0]==1){
        a[3][0]=a[0][0];
        a[3][1]=map[3][a[3][0]];
    }
    if(map[3][0]==2){
        a[3][0]=1;
        a[3][1]=map[3][1];
    }
    if(map[3][0]==3){
        a[3][0]=a[1][0];
        a[3][1]=map[3][a[3][0]];
    }
    if(map[3][0]==4){
        a[3][0]=a[1][0];
        a[3][1]=map[3][a[3][0]];
    }
}
int stage5(){
    if(map[4][0]==1){
        a[4][1]=a[0][1];
        for(int k=1;k<5;k++){
            if(map[4][k]==a[4][1]){
                a[4][0]=k;
                break;
            }
        }
    }
    if(map[4][0]==2){
        a[4][1]=a[1][1];
        for(int k=1;k<5;k++){
            if(map[4][k]==a[4][1]){
                a[4][0]=k;
                break;
            }
        }
    }
    if(map[4][0]==3){
        a[4][1]=a[3][1];
        for(int k=1;k<5;k++){
            if(map[4][k]==a[4][1]){
                a[4][0]=k;
                break;
            }
        }
    }
    if(map[4][0]==4){
        a[4][1]=a[2][1];
        for(int k=1;k<5;k++){
            if(map[4][k]==a[4][1]){
                a[4][0]=k;
                break;
            }
        }
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                cin>>map[i][j];
            }
        }
        stage1();
        stage2();
        stage3();
        stage4();
        stage5(); 
        for(int i=0;i<5;i++){
            for(int j=0;j<2;j++){
                cout<<a[i][j];
                if(j==0) cout<<' ';
            }
            cout<<endl;
        }
    }
    return 0;
}

你可能感兴趣的:(zoj 3938 Defuse the Bomb)