fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <bitset>
  5. using namespace std;
  6.  
  7. int lim = 1 << 16;
  8. int mat[1 << 16];
  9. //int arr[] = {-1,-4,0,1,4};
  10.  
  11. void swi(int &n) {
  12. if(n == 0) n = 1;
  13. if(n == 1) n = 0;
  14. }
  15.  
  16. int soloUno(int x,int i) {
  17. x ^= (1 << i);
  18. return x;
  19. }
  20.  
  21. bool potenciaDeDos(int n) {
  22. if(n == 0 or n == 1) return true;
  23. if(n % 2 != 0) return false;
  24. return potenciaDeDos(n >> 1);
  25. }
  26.  
  27. int todos(int x,int i) {
  28. x ^= (1 << i);
  29. int aux = i - 1;
  30. if(aux >= 0) x ^= (1 << aux);
  31. aux = i - 4;
  32. if(aux >= 0) x ^= (1 << aux);
  33. aux = i + 1;
  34. if(aux < 16) x ^= (1 << aux);
  35. aux = i + 4;
  36. if(aux < 16) x ^= (1 << aux);
  37. return x;
  38. }
  39.  
  40. int dp(int n) {
  41. if(n == 0) return 0;
  42. int &r = mat[n];
  43. if(potenciaDeDos(n)) r = 2;
  44. if(r == -1) {
  45. r = 100000;
  46. for(int i = 0; i < 16; ++i) {
  47. if(r == 1) return 1;
  48. r = min(r,min(2 + dp(soloUno(n,i)),1 + dp(todos(n,i))));
  49. }
  50. }
  51. return r;
  52. }
  53.  
  54. int main() {
  55. //freopen("EcualizadorDJ.in","r",stdin);
  56. //freopen("EcualizadorDJ.out","w,",stdout);
  57. memset(mat,-1,sizeof mat);
  58. mat[0] = 0;
  59. //for(int i = 3; i < lim; ++i) dp(i);
  60. int T;
  61. cin >> T;
  62. string s,total;
  63. while(T--) {
  64. total = "";
  65. for(int i = 0; i < 4; ++i) {
  66. cin >> s;
  67. total += s;
  68. }
  69. bitset<20> bs(total);
  70. cout << total << endl;
  71. cout << dp(bs.to_ulong()) << endl;
  72. }
  73. }
  74.  
Success #stdin #stdout 0.07s 6060KB
stdin
45
1100
1000
0000
0000
0100
1110
0100
0000
1000
0000
0000
0000
1100
1000
0000
0001
1011
1010
0000
0000
0010
0111
0110
0111
1111
0010
0001
0100
1001
0110
0110
1001
1010
0111
1010
0110
1101
1101
0100
0100
0110
1100
0001
0111
0011
1001
0110
0011
1000
1111
0010
0011
1100
0001
0111
0101
0100
1111
1110
0101
0110
0000
1100
0100
0110
1100
0100
0001
1110
0001
0000
0100
0001
1010
0101
1101
0101
0101
1001
0100
0110
1001
1001
0110
0000
0110
0110
0000
0110
1111
1111
0110
0110
0111
1111
1100
1110
0011
1101
1101
1010
1101
0110
1101
0000
1111
1001
1111
1011
1101
0100
1010
0101
1101
1011
1001
1111
1111
1111
1111
1011
0100
0101
1010
1111
1100
0011
1111
1010
0101
1010
0101
0101
1010
0101
1010
0100
1000
1101
0110
1001
0000
0000
1001
1011
1010
0000
0000
0001
0101
1111
1011
1111
1011
0110
1101
1010
1100
0101
0011
0100
1010
0100
0000
1111
1011
1101
1111
0110
1001
0110
0000
1111
1101
1011
1111
0100
1000
0100
0000
stdout
1100100000000000
1
0100111001000000
1
1000000000000000
2
1100100000000001
3
1011101000000000
2
0010011101100111
6
1111001000010100
7
1001011001101001
4
1010011110100110
7
1101110101000100
6
0110110000010111
6
0011100101100011
7
1000111100100011
5
1100000101110101
4
0100111111100101
3
0110000011000100
8
0110110001000001
7
1110000100000100
6
0001101001011101
6
0101010110010100
100000
0110100110010110
8
0000011001100000
6
0110111111110110
4
0110011111111100
6
1110001111011101
5
1010110101101101
7
0000111110011111
6
1011110101001010
8
0101110110111001
8
1111111111111111
7
1011010001011010
8
1111110000111111
5
1010010110100101
5
0101101001011010
6
0100100011010110
3
1001000000001001
5
1011101000000000
2
0001010111111011
6
1111101101101101
7
1010110001010011
6
0100101001000000
3
1111101111011111
7
0110100101100000
2
1111110110111111
7
0100100001000000
5