1 条题解

  • -1
    @ 2026-9-17 15:57:24

    #include <bits/stdc++.h>

    using namespace std;

    const int INF = 1000000;

    char g[10][10];

    int dx[] = {0, -1, 0, 1, 0};

    int dy[] = {0, 0, 1, 0, - 1};

    void turn(int x, int y) {

    for (int i = 0; i < 5; i++) {

    int nx = x + dx[i];
    	
    int ny = y + dy[i];
    	
    if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5)
    	
    		g[nx][ny] = '0' + ('1' - g[nx][ny]);
    

    }

    }

    int work() {

    int ans = INF;

    for (int k = 0; k < 1 << 5 ; k++) {

    int res = 0 ;
    
    char backup[10][10];
    
    memcpy(backup, g, sizeof(g));
    	
    for (int j = 0; j < 5; j++) {
    		
      if (k >> j & 1) {
    		
        res++;  
    			
        turn(0, j);
    	
      }
    
    }
    	
    for (int i = 0; i < 4; i++)
    		
      for (int j = 0; j < 5; j++)
    			
        if (g[i][j] == '0') {
    				
          res++;
    				
          turn(i + 1, j ); 
    			
        }
    
    bool flg = 1;
    	
    for (int j = 0; j < 5; j++)
    		
      if (g[4][j] == '0') {
    			
        flg = 0;
    		
        break;
    		
      }
    
    if (flg) ans = min(ans, res);
    
    memcpy(g, backup, sizeof(g));
    

    }

    if (ans > 6) return -1;

    return ans;

    }

    int main() {

    int T;

    cin >> T;

    while (T--) {

    for (int i = 0; i < 5; i++)
    	
      cin >> g[i];
    	
    cout << work() << endl;
    

    }

    return 0;

    }

信息

ID
15336
时间
1000ms
内存
512MiB
难度
6
标签
递交数
78
已通过
29
上传者