web123456

Data structure--Black and White Chess

  • //Black and White Chess.cpp: This file contains"main"function. Program execution will begin and end here.
  • //Given a chessboard, N?, M columns, where3=<M<=203=<N<=100, the board is filled with chess and chess, if a
  • //?? are all chess, we call this a path. Assuming that the chessboard can be flipped in columns (the chess is inversely) gives a K
  • //value,3 = < K <=M, Flip the board K times (must be flipped K times, you can choose to flip any column, or you can flip a column multiple times) to solve the problem.
  • //After K flips, at most there are paths? , if there is no? path, output - 1
  • #include <iostream>
  • using namespace std;
  • int N, M, k, minn = 0, q = 0;
  • int board[101][21], boardCopy[101][21];
  • int col[101], book[101];
  • int path[21];
  • //Calculate how many channels there are
  • int road(int path[21]) {
  • //Number of rows
  • int co=N ;
  • //copy
  • for (int i = 1;i <= N;i++)
  • for (int j = 1;j <= M;j++)
  • boardCopy[i][j] = board[i][j];
  • //Flip
  • for (int i = 1;i <= N;i++)
  • for (int j = 1;j <= k;j++) {
  • if (boardCopy[i][path[j]] == 0)
  • boardCopy[i][path[j]] = 1;
  • else
  • boardCopy[i][path[j]] = 0;
  • }
  • //Find a way
  • for (int i = 1;i <= N;i++)
  • for (int j = 1;j <= M;j++) {
  • if (boardCopy[i][j] == 0) {
  • co=co-1;
  • break;
  • }
  • }
  • return co;
  • }
  • void init() {
  • for (int i = 1;i <= M;i++)
  • col[i] = i;
  • }
  • void dfs(int nowk, int step) {
  • int nextk;
  • //If you have turned it k times, then determine how many paths are currently there and find the most
  • if (step == k) {
  • int maxroad = road(path);
  • if (maxroad > minn){
  • minn = maxroad;
  • //cout << minn;
  • }
  • return;
  • }
  • for (int i = nowk;i <= M;i++) {
  • path[step + 1] = i;
  • dfs(i, step + 1);
  • }
  • return;
  • }
  • int main()
  • {
  • int T;
  • cin >> T;
  • for (int i = 1;i <= T;i++) {
  • cin >> N >> M >> k;
  • for (int p = 1;p <= N;p++)
  • for (int q = 1;q <= M;q++)
  • cin >> board[p][q];
  • init();
  • for (int i = 1;i <= M;i++) {
  • path[1] = i;
  • dfs(i, 1);
  • }
  • cout<<minn<<endl;
  • minn = 0;
  • }
  • return 0;
  • }
  • /*
  • 1
  • 4 4 3
  • 1 0 1 1
  • 1 1 0 1
  • 0 1 1 1
  • 1 1 0 1
  • 7
  • 9 8 3
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 9 8 4
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 9 8 5
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 9 8 6
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 12 8 3
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 12 8 4
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 12 8 5
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 0 0 0 0 0 0 0 0
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 1 1 0 0 0 0 0
  • 1 1 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • 1 0 1 1 1 1 1 1
  • */