2965 -- The Pilots Brothers' refrigerator
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 27893 | Accepted: 10802 | Special Judge |
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j]
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “?” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+--
----
----
-+--
Sample Output
6
1 1
1 3
1 4
4 1
4 3
4 4
Source
Northeastern Europe 2004, Western Subregion 題意: 給你4*4的矩陣。每個點有兩種狀態,+代表關,-代表開。每個點有一個操作就是該點所在行列所有狀態翻轉。問最少多少次可以全部打開,並且輸出最少個數情況下翻轉的點。 解題思路: 這道題跟1753 -- Flip Game很像,可以用1753的解題思路。這道題用了更簡單的方法。 遍歷矩陣中每一個關閉的點,按照規則將其進行翻轉,將他的反轉次數++。重復上述操作,直到所有的點都是打開狀態。
1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 char m[5][5]; 5 int Count[5][5]; 6 //判斷狀態 7 bool Check() 8 { 9 for(int i=1;i<=4;i++) 10 for(int j=1;j<=4;j++) 11 { 12 if(m[i][j] == ‘+‘) return 0;//沒有被全部打開 13 } 14 return 1; 15 } 16 int getAns() 17 {//計算結果 18 int ans = 0; 19 for(int i=1;i<=4;i++) 20 { 21 for(int j=1;j<=4;j++) 22 { 23 if(Count[i][j]%2 == 0) 24 { 25 Count[i][j] = 0; 26 }else{ 27 Count[i][j] = 1; 28 ans++; 29 } 30 } 31 } 32 return ans; 33 } 34 int changeState() 35 { 36 for(int i=1;i<=4;i++) 37 for(int j=1;j<=4;j++) 38 { 39 if(m[i][j] == ‘+‘)//開關為關閉狀態 40 { 41 m[i][j] = ‘-‘; 42 Count[i][j] += 1; 43 for(int k = 1;k<=4;k++) 44 { 45 if(m[i][k] == ‘+‘) {m[i][k] = ‘-‘;} 46 else{m[i][k] = ‘+‘;} 47 } 48 for(int k=1;k<=4;k++) 49 { 50 if(m[k][j] == ‘+‘) {m[k][j] = ‘-‘;} 51 else{m[k][j] = ‘+‘;} 52 } 53 if(Check()) 54 {//進行結果輸出 55 return getAns(); 56 } 57 } 58 } 59 if(Check() == 0) return changeState(); 60 } 61 62 int main() 63 { 64 char c; 65 //初始化 66 while(true) 67 { 68 for(int i = 1;i<=4;i++) 69 for(int j=1;j<=4;j++) 70 Count[i][j] = 0; 71 for(int i=1;i<=4;i++) 72 { 73 for(int j=1;j<=4;j++) 74 { 75 if((c = getchar()) == EOF) return 0; 76 m[i][j] = c; 77 } 78 c = getchar(); 79 } 80 81 cout<<changeState()<<endl; 82 for(int i=1;i<=4;i++) 83 { 84 for(int j=1;j<=4;j++) 85 { 86 if(Count[i][j] == 1) cout<<i<<" "<<j<<endl; 87 } 88 } 89 } 90 91 return 0; 92 }
對於一個關閉的點,想要把他打開,首先翻轉一下他自己,為了不影響其他的點他所在行列都得翻轉一次。這樣最後是奇數次的即為翻轉點。
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 using namespace std; 5 char m[5][5]; 6 int Count[5][5]; 7 //判斷狀態 8 bool Check() 9 { 10 for(int i=1;i<=4;i++) 11 for(int j=1;j<=4;j++) 12 { 13 if(m[i][j] == ‘+‘) return 0;//沒有被全部打開 14 } 15 return 1; 16 } 17 int getAns() 18 {//計算結果 19 int ans = 0; 20 for(int i=1;i<=4;i++) 21 { 22 for(int j=1;j<=4;j++) 23 { 24 if(Count[i][j]%2 == 0) 25 { 26 Count[i][j] = 0; 27 }else{ 28 Count[i][j] = 1; 29 ans++; 30 } 31 } 32 } 33 return ans; 34 } 35 int main() 36 { 37 char c; 38 while(true) 39 { 40 memset(Count,0,sizeof(Count));//初始化 41 for(int i=1;i<=4;i++) 42 { 43 for(int j=1;j<=4;j++) 44 { 45 if((c = getchar()) == EOF) return 0; 46 m[i][j] = c; 47 if(c == ‘+‘) 48 { 49 for(int k=1;k<=4;k++) 50 { 51 Count[k][j]++; 52 Count[i][k]++; 53 } 54 Count[i][j]--; 55 } 56 } 57 c = getchar(); 58 } 59 ///打印結果 60 cout<<getAns()<<endl; 61 for(int i=1;i<=4;i++) 62 { 63 for(int j=1;j<=4;j++) 64 { 65 if(Count[i][j] == 1) cout<<i<<" "<<j<<endl; 66 } 67 } 68 } 69 70 return 0; 71 }
2965 -- The Pilots Brothers' refrigerator